Android M request permission non activity

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

My widget makes calls to secure permissions outside of an Activity scope. Is it possible to request permissions for Android M outside of an Activity?

回答1:

If you want to request the permission from outside the activity you can create different class and extend the class from Activity. Use constructer to get and set Context for the class.

public class Permissions extends Activity {  private static final int MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE = 1; private Context context;  public Permissions(Context context) {     this.context = context; } public void checkWriteExternalStoragePermission() {     final int MY_PERMISSIONS_REQUEST_PHONE_CALL = 1;     ContextCompat.checkSelfPermission(context,      Manifest.permission.WRITE_EXTERNAL_STORAGE);     if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {         ActivityCompat.requestPermissions((Activity) context,                 new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},                 MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);     } }  @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {     switch (requestCode) {          case MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE: {             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                 Toast.makeText(context, "Permission Granted", Toast.LENGTH_SHORT).show();             } else {                 Toast.makeText(context, "Permission Denied", Toast.LENGTH_SHORT).show();             }             return;          }         // other 'case' lines to check for other         // permissions this app might request     } } } 

Now, create create object and pass the context to constructor wherever you need to request permissions.

Permissions permissions = new Permissions(SplashScreen.this);     permissions.checkWriteExternalStoragePermission(); 


回答2:

No, it's not possible. What you can do is to send a notification where the user can tap and then use an activity to request/manage the permission (maybe with dialog theme).



回答3:

I think it's possible to request a permission outside of an Activity, as long as you use the method

ActivityCompat.requestPermissions (Activity activity, String[] permissions, int requestCode)

from the support library and pass the Activity as a parameter of the method.

For example:

ActivityCompat.requestPermissions(targetActivity, new String[] {Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE); 

where targetActivity is the Activity that should implement the method:

onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults)

That's the method that will handle de permission request result.



回答4:

You can only request permission either from Activity or from fragment.

Figure out a point in your Activity or Fragment where you feel App will require a permission, then call requestPermission method. sending notification will not work because you wanna your code processing un-till you get that requested permissions, and then resume your functionality from onRequestPermissionResult() method.



回答5:

You could use the Easy Permissions library.

Android requires that these request come from an Activity. With Easy Permissions this is no longer an issue, you may request permission from anywhere as long as you provide Context. In addition, if you request a permission that is already granted the user will not be prompted.

Full disclosure, our company manages and develops this free to use library. That being said, we are confident that it is a useful tool and we would not share it otherwise.



回答6:

It's possible to call requestPermissions() from outside of your activity, but in your class you should have the context of that activity.

ActivityCompat.requestPermissions((Activity)context, new String[]{permission.READ_PHONE_STATE}, REQUEST_CODE); 

In order to work, you should implement below method in your activity

onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults) 

So that this override method get execute when user tap on allow or deny button on the pop up



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!