2017-07-19 40 views
1

我想知道如何在管理器上設置值?以下是我如何初始化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) { 

        } 

這裏是許可管理類的代碼,但我有我想要一個空值消除

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

回答

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

嗨RS_Mob,我更新了上面的代碼。我在這裏得到一個錯誤'private PermissionManagerInterface mManagerInterface;''mManagerInterface.onPermissionGranted(「\'」+ packageStrippedPermissionName +「\':permission granted」,requestCode);'謝謝。 – user8256287

+0

您是否初始化了mManagerInterface? –

+0

在onStart中添加manager.getManagerInterface(YourActivity.this),如果您在您的活動中實施了mManagerInterface –