如何在Permission Manager上添加值

时间:2017-07-19 09:21:33

标签: android android-permissions

我想知道如何为经理设定价值?以下是我初始化PermissionManager管理器或PermissionManager manager = new?

的方法
public PermissionManager manager = new PermissionManager(PermissionManager.mActivity, PermissionManager.mContext);


protected void onStart() {
    super.onStart();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        /*String[] list = new String[]{PermissionConsts.PHONE_STATE, PermissionConsts.READ_CONTACTS, PermissionConsts.READ_EXTERNAL_STORAGE,
                PermissionConsts.READ_SMS};*/
        manager.requestPermission(new String[]{Manifest.permission.ACCESS_FINE_LOCATION});
    }
    Intent serviceIntent = new Intent(this, LocationFetcherService.class);
    startService(serviceIntent);
}

这里不起作用:

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

            if (id == R.id.nav_camera) {
                initLocationFetching(this);
    }
    }

这是BaseActivityLocation类

public class BaseActivityLocation extends AppCompatActivity implements LocationManagerInterface {

    public SmartLocationManager mLocationManager;
    public PermissionManager manager = new PermissionManager(BaseActivityLocation.this, BaseActivityLocation.this);
    public LocationFetcherService mLocationFetcherService;
    private static final int REQUEST_FINE_LOCATION = 1;
    private Activity mCurrentActivity;
    private int count = 0;


    @Override
    public void locationFetched(Location mLocation, Location oldLocation, String time, String locationProvider) {
        // storing it on application level
        GetAccurateLocationApplication.mCurrentLocation = mLocation;
        GetAccurateLocationApplication.oldLocation = oldLocation;
        GetAccurateLocationApplication.locationProvider = locationProvider;
        GetAccurateLocationApplication.locationTime = time;
    }

    public void instatntiate(Activity mActivity){
        /*3rd Parametr*/
        //For One time Gps Usage: SmartLocationManager.USE_ONE_TIME_GPS;
        //For every 10 seconds Gps Usage: SmartLocationManager.USE_UPDATE_TIME_GPS;

        /*4th parameter*/
        //For All netwrok Provider: SmartLocationManager.ALL_PROVIDERS;
        //For Network Provider only: SmartLocationManager.NETWORK_PROVIDER;
        //For GPS Provider only: SmartLocationManager.GPS_PROVIDER;

        /*Last Parameter*/
        //For using Any Api location: SmartLocationManager.ANY_API; //The priority will be google api.
        //For using google Api location: SmartLocationManager.ONLY_GOOGLE_API;
        //For using android Api Location: SmartLocationManager.ONLY_ANDROID_API;
        mLocationManager = new SmartLocationManager(getApplicationContext(), mActivity, SmartLocationManager.USE_ONE_TIME_GPS, this, SmartLocationManager.ALL_PROVIDERS, LocationRequest.PRIORITY_HIGH_ACCURACY, 10 * 1000, 1 * 1000, SmartLocationManager.LOCATION_PROVIDER_RESTRICTION_NONE, SmartLocationManager.ANY_API); // init location manager
    }

    public void initLocationFetching(Activity mActivity) {
        mCurrentActivity = mActivity;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                manager = new PermissionManager(mCurrentActivity, getApplicationContext());

                PermissionManagerInterface permissionManagerInterface = new PermissionManagerInterface() {
                    @Override
                    public void onPermissionGranted(String message, int requestCode) {

                    }

以下是Permission Manager类的代码,但我想要消除

的空值
public class PermissionManager implements RequestPermissionsResultInterface {

    public static Activity mActivity;
    public static Context mContext;
    public String[] permissionList;
    public boolean flag;

    private PermissionManagerInterface mManagerInterface = new PermissionManagerInterface() {


    @Override
    public void onPermissionGranted(String message, int requestCode) {

    }

    @Override
    public void onPermissionDenied(String message, int requestCode) {

    }

    @Override
    public void isAllGranted(boolean flag) {

    }
};

    public PermissionManager(Activity mActivity, Context mContext) {
        this.mActivity = mActivity;
        this.mContext = mContext;
    }

    public void requestPermission(String[] mList){
        permissionList = mList;
        ActivityCompat.requestPermissions(mActivity, mList, REQ_CODE);
    }

    public void getManagerInterface(PermissionManagerInterface mInterface){
        mManagerInterface = mInterface;
    }
 @RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    if(requestCode == REQ_CODE){
        for (int i = 0, len = permissions.length; i < len; i++) {
            String permission = permissions[i];
            String packageStrippedPermissionName =  permissions[i].substring(permissions[0].lastIndexOf(".")+1);
            if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                // user rejected the permission
                boolean showRationale = mActivity.shouldShowRequestPermissionRationale( permission );
                if (! showRationale) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                    builder.setTitle(permission + " Required");
                    builder.setMessage("You have to grant this permission , otherwise app will not be able to continue, Click Yes to go to Settings else app will not resume!");
                    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", mActivity.getPackageName(), null);
                            intent.setData(uri);
                            mActivity.startActivityForResult(intent, REQ_CODE);
                        }
                    });
                    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(mContext, "App will not Start without this Permission", Toast.LENGTH_SHORT).show();
                            mActivity.finish();
                        }
                    });
                    builder.setCancelable(false);
                    builder.show();
                    // user also CHECKED "never ask again"
                    // you can either enable some fall back,
                    // disable features of your app
                    // or open another dialog explaining
                    // again the permission and directing to
                    // the app setting
                }else{
                    mManagerInterface.onPermissionDenied("\'" + packageStrippedPermissionName + "': permission denied", requestCode);
                    Alert(mActivity, permissions[i]);
                }
            }else if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
                mManagerInterface.onPermissionGranted("\'" + packageStrippedPermissionName + "\': permission granted", requestCode);
            }
        }
        ArrayList<String> list = new ArrayList<>();
        for(int k=0; k<grantResults.length; k++){
            if(grantResults[k] == PackageManager.PERMISSION_GRANTED){
                list.add(permissions[k]);
            }
        }
        if(list.size() == permissions.length){
            flag = true;
            mManagerInterface.isAllGranted(flag);
        }else{
            flag = false;
            mManagerInterface.isAllGranted(flag);
        }
    }

}

BaseActivityLocation java类:

BaseActivityLocation extends AppCompatActivity implements LocationManagerInterface {

public SmartLocationManager mLocationManager;
public PermissionManager manager = null;
public LocationFetcherService mLocationFetcherService;
private static final int REQUEST_FINE_LOCATION = 1;
private Activity mCurrentActivity;
private int count = 0;


@Override
public void locationFetched(Location mLocation, Location oldLocation, String time, String locationProvider) {
    // storing it on application level
    GetAccurateLocationApplication.mCurrentLocation = mLocation;
    GetAccurateLocationApplication.oldLocation = oldLocation;
    GetAccurateLocationApplication.locationProvider = locationProvider;
    GetAccurateLocationApplication.locationTime = time;
}

protected void onStart() {
    super.onStart();
    manager = new PermissionManager(BaseActivityLocation.this, BaseActivityLocation.this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    /*String[] list = new String[]{PermissionConsts.PHONE_STATE, PermissionConsts.READ_CONTACTS, PermissionConsts.READ_EXTERNAL_STORAGE,
            PermissionConsts.READ_SMS};*/
        manager.requestPermission(new String[]{Manifest.permission.ACCESS_FINE_LOCATION});
    }
    Intent serviceIntent = new Intent(this, LocationFetcherService.class);
    startService(serviceIntent);
}

protected void onResume(){
    super.onResume();
    //showLocationPermission();
}

权限管理器界面

public interface PermissionManagerInterface extends Serializable
{
    String TAG = PermissionManagerInterface.class.getSimpleName();

    void onPermissionGranted(String message, int requestCode);

    void onPermissionDenied(String message, int requestCode);

    void isAllGranted(boolean flag);
}

1 个答案:

答案 0 :(得分:0)

如果这是在活动中,请尝试此操作。 YourActivity是您的活动名称:

public PermissionManager manager = null;

protected void onStart() {
    super.onStart();
    manager = new PermissionManager(YourActivity.this, YourActivity.this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        /*String[] list = new String[]{PermissionConsts.PHONE_STATE, PermissionConsts.READ_CONTACTS, PermissionConsts.READ_EXTERNAL_STORAGE,
                PermissionConsts.READ_SMS};*/
        manager.requestPermission(new String[]{Manifest.permission.ACCESS_FINE_LOCATION});
    }
    Intent serviceIntent = new Intent(this, LocationFetcherService.class);
    startService(serviceIntent);
}