Ali Azaz Alam
Ali Azaz Alam

Reputation: 1868

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag? nabinbhandari/Android-Permissions library

While using

nabinbhandari/Android-Permissions library: Link

Getting error when I'm running app on Android device API Level > 20

It's non-activity class. MainApp.getmContext() return the Application context:

String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
    Permissions.check(MainApp.getmContext(), permissions, null/*rationale*/, null/*options*/, new PermissionHandler() {
        @Override
        public void onGranted() {
            // do your task.
            Toast.makeText(MainApp.getmContext(), "Granted", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDenied(Context context, ArrayList<String> deniedPermissions) {
            Toast.makeText(MainApp.getmContext(), "Denied", Toast.LENGTH_SHORT).show();
        }
    });

The error is coming on Permissions.check line.

Upvotes: 1

Views: 178

Answers (3)

Nabin Bhandari
Nabin Bhandari

Reputation: 16409

In some devices, while starting new activity from service context, it is required to use Intent.FLAG_ACTIVITY_NEW_TASK in the Intent. In order to do so, use the following object as the options parameter.

Options options = new Options().setCreateNewTask();

For more information, see this issue and this pull request.

Upvotes: 0

Ali Azaz Alam
Ali Azaz Alam

Reputation: 1868

We've to pass the Activity [on which you want to show the permissions] Context in permission argument:

public static void permissionGrant(Context mContext){
    String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
    Permissions.check(mContext, permissions, null/*rationale*/, null/*options*/, new PermissionHandler() {
        @Override
        public void onGranted() {
            // do your task.
            Toast.makeText(MainApp.getmContext(), "Granted", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDenied(Context context, ArrayList<String> deniedPermissions) {
            Toast.makeText(MainApp.getmContext(), "Denied", Toast.LENGTH_SHORT).show();
        }
    });
}

Upvotes: 2

Loren Rogers
Loren Rogers

Reputation: 359

I believe it's because of your MainApp.getmContext. That is actually the application context. You need an activity context.

Upvotes: 1

Related Questions