1

I am taking user to Notification center to control their push notifications preferences and read it from my app, and set to a switch toggle. I have different action for Lollipop and Oreo, but currently application is crashing for Android Pie 9.0. How can I fix that issue?

public void onPushSwitchChanged(View v){
    if(v.isPressed()){
        Intent intent = new Intent();
        if(android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1){
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", mContext.getPackageName());
        }else if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", mContext.getPackageName());
            intent.putExtra("app_uid", mContext.getApplicationInfo().uid);
        }else {
            intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setData(Uri.parse("package:" + mContext.getPackageName()));
        }
        mContext.startActivity(intent);
    }
}

And the error is

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.
mcece
  • 31
  • 8
  • Show stacktrace and part of the code where it is happening. – Raskilas Feb 16 '19 at 02:45
  • Can you also share the [stack trace](https://stackoverflow.com/q/23353173/2745495) and the [codes you used](https://stackoverflow.com/help/mcve) that resulted in the crash? We can't see your code, your IDE, and your phone so it's impossible for anyone here to know what the problem is. – Gino Mempin Feb 16 '19 at 02:46

1 Answers1

1

Update,

I fixed the issue by adding,

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

in my first if block.

mcece
  • 31
  • 8