8

My application crash when I do this :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (Settings.System.canWrite(ListOfTerminalsActivity.this)) {
         // Do stuff here
     } else {
         Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
         intent.setData(Uri.parse("package:" + getPackageName()));
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
     }
}

It happend on API 23 in ZUK Z2 this is logs ;

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.action.MANAGE_WRITE_SETTINGS dat=package:pl.teminalmobile flg=0x10000000 }
01-24 12:33:56.123 30159 30159 E AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
01-24 12:33:56.123 30159 30159 E AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2536)
01-24 12:33:56.123 30159 30159 E AndroidRuntime:  at android.app.ActivityThread.access$900(ActivityThread.java:159)
01-24 12:33:56.123 30159 30159 E AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
01-24 12:33:56.123 30159 30159 E AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102)
01-24 12:33:56.123 30159 30159 E AndroidRuntime:  at android.os.Looper.loop(Looper.java:148)
01-24 12:33:56.123 30159 30159 E AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5504)
01-24 12:33:56.123 30159 30159 E AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)
01-24 12:33:56.123 30159 30159 E AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-24 12:33:56.123 30159 30159 E AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-24 12:33:56.123 30159 30159 E AndroidRuntime: Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.action.MANAGE_WRITE_SETTINGS

It doesn't happend and my application not crash for example on device samsung s5 API 23, samsung s7 API 24

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Krzysztof Pokrywka
  • 1,356
  • 4
  • 27
  • 50

1 Answers1

12

Quoting the documentation for ACTION_MANAGE_WRITE_SETTINGS:

In some cases, a matching Activity may not exist, so ensure you safeguard against this.

So, wrap your startActivity() call in a try/catch block, and if you get an ActivityNotFoundException, do something else. For example, you could try the same Intent action but not include the package Uri.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • when I don't include this package I see this same error . I find this : https://issuetracker.google.com/issues/37079055 – Krzysztof Pokrywka Jan 24 '18 at 12:23
  • @KrzysztofPokrywka: It is within the manufacturer's rights to not include a direct means of launching this activity. And, this is documented behavior. If you cannot launch the activity, tell the user that you cannot direct them to this specific activity, and perhaps try to take them to the main Settings screen. – CommonsWare Jan 24 '18 at 12:27