Android M onRequestPermissionsResult in inactivity

I have an application that should find the location user, and the location is selected in different classes, so I wrote a separate class (not the Activity class) that selects the user's location using location services , it works fine under Android M , but requires time resolution run in Android M , I want to check permissions in my Location class, and I know how to check them, but I cannot use the onRequestPermissionsResult method in the Location class because my Location class does not apply to any actions.

So what should I do to achieve this? any help / tip appreciated Thanks in advance

+8
java android android-6.0-marshmallow
Jul 20 '16 at 11:57
source share
4 answers

You can call checkSelfPermission() from a class other than the UI, as this only requires Context .

However, you must call requestPermissions() for some activity or fragment. You are redefining onRequestPermissionsResult() to the same activity or fragment. This is no different from calling startActivityForResult() and implementing onActivityResult() .

The idea is that you request permission before doing anything that is related to your non-user interfaces that deal with locations.

+5
Jul 20 '16 at 12:16
source share

You cannot override it. This method is only available for Activity and Fragments. But you can create a static method inside your Location class and call it from the onRequestPermissionResult overridden action / fragment.

I made the usual implementation for location combined with resolution. You can also use a library called Let for permissions.

+3
Jul 20 '16 at 12:18
source share

Since public void onRequestPermissionsResult (int requestCode, String permissions [], int [] grantResults) is an abstract method of the ActivityCompat.OnRequestPermissionsResultCallback interface. See documentation

Just implement this interface in the required class, and it is done. for example

 class location implements ActivityCompat.OnRequestPermissionsResultCallback{ } 

Now just override onRequestPermissionsResult ()

  @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { // case Statements } } 
+3
Oct 09 '17 at 10:51 on
source share

1- create transparent activity

 <activity android:name=".activity.activity.CheckStoragePermissionsActivity" android:theme="@style/Theme.Transparent"> <style name="Theme.Transparent" parent="Theme.AppCompat"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:backgroundDimEnabled">false</item> </style> 

2- configure your activity

 public class CheckStoragePermissionsActivity extends AppCompatActivity { private String[] permissions; private int pCode = 12321; public static PermissionListener permissionListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); checkPermissions(); } private void checkPermissions() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; boolean flag = false; for (String s : permissions) if (checkSelfPermission(s) != PackageManager.PERMISSION_GRANTED) flag = true; if (flag) { requestPermissions(permissions, pCode); } else { permissionListener.permissionResult(true); finish(); } }else finish(); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == pCode) { boolean flag = true; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) for (int i = 0, len = permissions.length; i < len; i++) if (grantResults[i] != PackageManager.PERMISSION_GRANTED) flag = false; if (flag) { if (permissionListener != null) permissionListener.permissionResult(true); } else if (permissionListener != null) permissionListener.permissionResult(false); finish(); } } } 

3- permissionListener is a static interface and can be set just before context.startActivity (...) or use your plan to get permission.

 public interface PermissionListener extends Serializable { void permissionResult(boolean hasPermission); } 

4- finally call context.startActivity(new Intent(context, CheckStoragePermissionsActivity.class));

CheckStoragePermissionsActivity do everything you need and finish after the user allows or cancels the action.

+2
Nov 19 '16 at 15:14
source share



All Articles