Android settings prohibit "activity mapping"

Most actions Actions (used to run various settings actions) in the Settings class are cautious:

In some cases, the corresponding activity may not exist, so make sure you protect it.

So how can I protect myself from this?

try { final Intent i = new Intent(Settings. ACTION_WIRELESS_SETTINGS); // say i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // not sure if needed startActivity(i); } catch (Exception e) { // what should I catch here // I would hate to catch Throwable, but should I ? } 

If I read this correctly, for example, a runtime exception (NPE) is selected. I would like to use something more specific, though an ActivityNotFoundException - but is that enough?

+6
source share
1 answer

If I read it correctly, for example, runtime exception (NPE) is thrown

No, this is some other problem. Intent worked clearly because the crash comes from the settings themselves, and not from the application called startActivity() .

I would like to use something more specific, although like an ActivityNotFoundException - but is that enough?

It should be.

If you are worried about this or prefer to be active rather than just calling startActivity() , first use the PackageManager and resolveActivity() . If this returns null , there is no activity that matches the Intent , and you should try something else.

+4
source

Source: https://habr.com/ru/post/958197/


All Articles