运行时权限Marshmallow Android

运行时权限Marshmallow Android,android,android-6.0-marshmallow,android-permissions,Android,Android 6.0 Marshmallow,Android Permissions,当应用程序打开时,如果权限被撤销,会发生什么情况?我已经尝试过了,发现应用程序的进程突然终止了。应用程序中的一切都只是简单地停止了。当我在应用打开时关闭权限时,本地保存的Apputil值将变为null。如果有人知道原因,请建议我以及如何解决此问题 // I am extending drawer activity to get the saved card number here @Override protected void onCreate(Bundle savedInstanceSta

当应用程序打开时,如果权限被撤销,会发生什么情况?我已经尝试过了,发现应用程序的进程突然终止了。应用程序中的一切都只是简单地停止了。当我在应用打开时关闭权限时,本地保存的Apputil值将变为null。如果有人知道原因,请建议我以及如何解决此问题

// I am extending drawer activity to get the saved card number here
 @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View contentView = inflater.inflate(R.layout.activity_reward_yourself,
            null, false);
    titleTextView.setText(R.string.reward_yourself_title);
    frameLayout.addView(contentView, 0);
    rewardsSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.rewardsSwipeContainer);
    grid = (GridView) findViewById(R.id.gridReward);
    tvPointsValue = (TextView) findViewById(R.id.tvPointsValue);
    tvFirstText = (TextView) findViewById(R.id.tvFirstText);
    rewardsSwipeLayout.setOnRefreshListener(this);
    rewardsSwipeLayout.setColorScheme(android.R.color.black,
            android.R.color.white, android.R.color.darker_gray,
            android.R.color.black);
    initialUiSetUp();
    program = new Program();
     String str= AppUtil.fxs.getCardNumber(); //becomes null when location is off when app is opened

    checkPermission(MyActivity.this);
    mAccInfo = new Account();
    mTask = new GetAccount().execute();
}

您应该始终明确检查系统是否授予了权限。例如,在我的情况下,我正在访问我的应用程序中的位置。所有位置任务都将由函数onLocationPermissionAllowed执行,因此在调用之前,我将检查位置权限,如下所示

         if (!PermissionRequestManager.checkIfAlreadyhavePermission(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION)) {
                PermissionRequestManager.requestPermissionAccessFineLocation(getActivity());
            } else onLocationPermissionGranted();

我希望……我没有迟到

最好的解决方案是使用第三方库…以最简单的方式实现运行时权限


我用过上面的图书馆。最好使用java注释。

代码在哪里?每当调用需要权限的方法时,必须定期检查权限。这样,当权限被撤销时,您可以控制要执行的操作,不是让应用程序终止。@Piyush我附加了部分代码。感谢您的ReplyTanks回复ebin。.我在这里扩展抽屉活动以从Draweractivity.class获取卡号,但是如果我关闭了位置权限,则在Draweractivity.class中的Apputil.savecardnumber将变为Null,其中Apputil类保存卡号?其抽屉内活动