0

私はアプリを開発しており、次のコードを使用してユーザーの現在の座標を取得しています(このライブラリから):

   ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
          locationProvider.getLastKnownLocation().subscribe(new Action1<Location>() {
                  @Override
                  public void call(Location location) {
                  currentLatDouble = location.getLatitude();
                  currentLngDouble = location.getLongitude();
                  Toast.makeText(getBaseContext(), "User's location retrieved using library for android M in btnRetryGpsEvents()", Toast.LENGTH_SHORT).show();
                }
          });

問題は、すべての条件が true になった後でも、このコードが実行されない場合があることです。以下のコードを参照してください。

    if (currentLatDouble != null || currentLngDouble != null) {
            retrieveHWrapper();
            progressBarLoadingRequests.setVisibility(View.VISIBLE);
            btnRetryGps.setVisibility(View.INVISIBLE);
            gps_off.setVisibility(View.INVISIBLE);
            Toast.makeText(getBaseContext(), "Loading hrequests...", Toast.LENGTH_LONG).show();
         } else {
            gps_off.setVisibility(View.VISIBLE);
            progressBarLoadingRequests.setVisibility(View.INVISIBLE);
            btnRetryGps.setVisibility(View.VISIBLE);
            btnRetryGps.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                btnRetryGpsEvents();
                int hasWriteContactsPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_SETTINGS);
                                    if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
                                        if (ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
                                            // TODO: Consider calling
                                            //    ActivityCompat#requestPermissions
                                            // here to request the missing permissions, and then overriding
                                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                            //                                          int[] grantResults)
                                            // to handle the case where the user grants the permission. See the documentation
                                            // for ActivityCompat#requestPermissions for more details.
                                            if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_SETTINGS)) {
                                                showMessageOKCancel("You need to allow access to few permissions so that the app can work as expected.",
                                                        new DialogInterface.OnClickListener() {
                                                            @Override
                                                            public void onClick(DialogInterface dialog, int which) {
                                                                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_SETTINGS},
                                                                        REQUEST_RUNTIME_PERMISSION);
                                                            }
                                                        });
                                                return;
                                            }
                                            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_SETTINGS},
                                                    REQUEST_RUNTIME_PERMISSION);
                                            return;
                                        }
                                    }

                                    ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
                                    locationProvider.getLastKnownLocation()
                                            .subscribe(new Action1<Location>() {
                                                @Override
                                                public void call(Location location) {
                                                    currentLatDouble = location.getLatitude();
                                                    currentLngDouble = location.getLongitude();
                                                    Toast.makeText(getBaseContext(), "User's location retrieved using library for android M in btnRetryGpsEvents()", Toast.LENGTH_SHORT).show();
                                                }
                                            });
                                    Toast.makeText(getBaseContext(), "called from btnRetryGpsEvents() - GPS available", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

上記のコードを実行すると、このコードによるトーストが表示されます。Toast.makeText(getBaseContext(), "called from btnRetryGpsEvents() - GPS available", Toast.LENGTH_SHORT).show();これは、これが実行されていることを意味しますが、そのすぐ上のトーストがToast.makeText(getBaseContext(), "User's location retrieved using library for android M in btnRetryGpsEvents()", Toast.LENGTH_SHORT).show();表示されていないため、実行されていません!

これはどのように起こりますか?ここで何がうまくいかないのですか????

私にお知らせください。

PS: この問題は Android M 以降でのみ発生します。

4

1 に答える 1

0

ReactiveLocationProvider 内のコードを確認してください。LocationListener を実装していると思います。getLastLocation が内部で呼び出されたと推測します。最後の場所が利用可能な場合にのみ場所を提供します。または、確認できるようにコードを提供してください。

于 2016-08-11T11:53:25.920 に答える