启动时的Android请求权限

时间:2017-05-15 04:41:35

标签: android permissions

我有一个旧的应用程序运行正常,但新的更新。它现在需要一个请求权限。我能够显示许可。但问题是," TestNavigationDrawer已停止"在启动时也会出现与允许请求一起出现的情况。我不知道在哪里提出请求权限,或者我放了正确的权限。请帮我解决一下这个。先感谢您。我还提供了一些屏幕截图来澄清这一点(" TestNavigationDrawer已经停止"还会在启动时出现以及允许请求。)

Click here to see the screenshot

这是我的代码。我甚至将权限请求放在onCreate的最顶层以避免错误。

protected void onCreate(Bundle savedInstanceState) {
    if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == 
PackageManager.PERMISSION_GRANTED){
        System.out.println("Permission Granted");
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this);
    } else{
        String[] permissionRequested={Manifest.permission.ACCESS_FINE_LOCATION};
        requestPermissions(permissionRequested, ACCESS_FINE_LOCATION_REQUEST_CODE);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   /* HomeFragment fragment = new HomeFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction =
            getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    fragmentTransaction.commit();*/

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

     navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == ACCESS_FINE_LOCATION_REQUEST_CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        } else {
            Toast.makeText(this, "PERMISSION NOT GRANTED", Toast.LENGTH_LONG).show();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

实际上,您需要在授予权限时调用权限请求,(LocationManager)等以下的所有内容。 将所有代码放在一个单独的函数中,并在onRequestPermissionResult中调用该函数..

答案 1 :(得分:0)

用下面的代码替换

protected void onCreate(Bundle savedInstanceState) {
    if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
    System.out.println("Permission Granted");


    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    } else{
        String[] permissionRequested={Manifest.permission.ACCESS_FINE_LOCATION};
        requestPermissions(permissionRequested, ACCESS_FINE_LOCATION_REQUEST_CODE);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}

因为如果没有给出权限的条件语句意味着允许位置管理器代码,并且您仍在访问导致崩溃的位置,那么如果您将位置管理器代码保留在一边。在给出权限后使用位置,即您的代码应该传递if语句 希望有所帮助