当我在清单中具有位置权限时,是否需要警报对话框来访问位置?为什么我在警告对话框中收到此错误?

时间:2016-09-14 10:45:01

标签: android android-manifest

我正在构建一个我正在使用位置的应用。我已经在清单中声明了许可:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

我现在是否需要建立警告对话框以使该用户在其设备上启用位置权限,或者我已在清单中使用此权限执行此操作? 当我打开GPS打开对话框时,我收到了这个错误:

09-14 12:18:12.614 28114-28114/com.telnet.asp E/WindowManager: Activity com.telnet.asp.presentation.view.activities.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42b29910 V.E..... R.....ID 0,0-720,351} that was originally added here
                                                               android.view.WindowLeaked: Activity com.telnet.asp.presentation.view.activities.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42b29910 V.E..... R.....ID 0,0-720,351} that was originally added here
                                                                   at android.view.ViewRootImpl.<init>(ViewRootImpl.java)
                                                                   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java)
                                                                   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java)
                                                                   at android.app.Dialog.show(Dialog.java)
                                                                   at com.telnet.asp.presentation.view.activities.MainActivity.showDialogForAllowingLocation(MainActivity.java:380)
                                                                   at com.telnet.asp.presentation.view.activities.MainActivity.onCreate(MainActivity.java:63)
                                                                   at android.app.Activity.performCreate(Activity.java)
                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
                                                                   at android.app.ActivityThread.access$700(ActivityThread.java)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
                                                                   at android.os.Handler.dispatchMessage(Handler.java)
                                                                   at android.os.Looper.loop(Looper.java)
                                                                   at android.app.ActivityThread.main(ActivityThread.java)
                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:525)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
                                                                   at dalvik.system.NativeStart.main(Native Method)

这是我的警告对话框:

 private void showDialogForAllowingLocation() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(false);
        builder.setTitle(getResources().getString(R.string.enable_permission));
        builder.setMessage(getResources().getString(R.string.enable_access));
        builder.setInverseBackgroundForced(true);
        builder.setPositiveButton(getResources().getString(R.string.enable), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                startActivity(
                        new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            }
        });
        builder.setNegativeButton(getResources().getString(R.string.ignore), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
    private void EnableGPSIfPossible() {
        final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            showDialogForAllowingLocation();
        }
    }

我的错误在哪里?

4 个答案:

答案 0 :(得分:0)

是。这样可确保您的应用无需中断即可运行。在应用程序启动之前检查Internet连接和gps。

答案 1 :(得分:0)

您需要检查用户是否已打开GPS,否则您必须显示提示对话框。

但是,请注意,从Android 6.0(API 23)开始,您必须手动要求用户授予权限,因为ACCESS_FINE_LOCATION被归类为危险。有关权限组的更多信息,请访问:https://developer.android.com/guide/topics/security/permissions.html

答案 2 :(得分:0)

  

是的,您需要获得Android 6.0或更高版本的运行时权限

 private static final int REQUEST_RUNTIME_PERMISSION = 123;


    if (CheckPermission(demo.this, Manifest.permission.ACCESS_FINE_LOCATION)) {


        showDialogForAllowingLocation();

    } else {
        // you do not have permission go request runtime permissions
        RequestPermission(demo.this, Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_RUNTIME_PERMISSION);
    }



public void RequestPermission(Activity thisActivity, String Permission, int Code) {
        if (ContextCompat.checkSelfPermission(thisActivity,
                Permission)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                    Permission)) {
                showDialogForAllowingLocation();

            } else {
                ActivityCompat.requestPermissions(thisActivity,
                        new String[]{Permission},
                        Code);
            }
        }
    }

    public boolean CheckPermission(Activity context, String Permission) {
        if (ContextCompat.checkSelfPermission(context,
                Permission) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

答案 3 :(得分:0)

使用专为if (ActivityCompat.checkSelfPermission (this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION }, 1); }

设计的权限
 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull   String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {

    case 1:
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
            //do something
        }
    default:
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 }
}

许可处理:

{{1}}