RunTime Permissions Marshmallow Android

时间:2017-08-09 08:04:28

标签: android android-6.0-marshmallow android-permissions

如果在打开应用程序时撤消权限会怎样?我已经尝试过,发现应用程序的进程突然终止了。应用程序内的所有内容都停止了。当应用程序打开时,本地保存的Apputil值在我关闭权限时变为空白。如果有人知道原因,请建议我以及如何解决此问题

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我希望......我不迟到....

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

https://github.com/hotchemi/PermissionsDispatcher

我使用了上面的库。它最好用java中的注释。