2016-09-14 21 views
0

我正在構建一個應用程序,我正在使用位置。我已經宣佈在明顯的權限:當我在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(); 
     } 
    } 

哪裏是我的錯?

+0

您必須添加對話框,提示用戶對GPS如果手機GPS關閉。 –

+0

請參閱編輯的問題。 – Atenica

+0

是的,如果targetSdkVersion i等於或大於23,並且手機使用Android M或更高版本 - 請點擊此處查看 - https://inthecheesefactory.com/blog/things-you-need-to-know-about -android-m-permission-developer-edition/en - 否則,當您在Android M下面下載低於23的應用程序時,它會要求您在安裝應用程序之前授予許可權 – Tasos

回答

0

是的。這確保您的應用程序不會中斷運行。在應用程序啓動之前檢查互聯網連接和gps。

0

您需要檢查用戶是否打開了GPS,如果沒有,那麼您必須顯示提示對話框。

但是,請注意,從Android 6.0(API 23)開始,您必須手動要求用戶授予權限,因爲ACCESS_FINE_LOCATION被歸類爲危險。在權限組此處瞭解詳情:https://developer.android.com/guide/topics/security/permissions.html

0

Yes you need to runtime permission for Android 6.0 or greater follow that

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; 
     } 
    } 
0

使用許可專爲android version > L

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); 
} 
}