How to handle a SecurityException thrown from startActivity with a select?

I use

final Intent notice = new Intent(); notice.setType("text/plain"); notice.putExtra(Intent.EXTRA_SUBJECT, "My Subject"); notice.putExtra(Intent.EXTRA_TEXT, "My Text"); try { getContext().startActivity(Intent.createChooser(notice, "Send...")); } catch(final android.content.ActivityNotFoundException ex) { Toast.makeText(getContext(), "There are no email clients installed.", Toast.LENGTH_SHORT).show(); } catch(final SecurityException ex) { Toast.makeText(getContext(), "Sorry, application does not have permissions to send to this destination.", Toast.LENGTH_SHORT).show(); } // try/catch 

as indicated in How to send email from an Android app?

But there are some applications if they are selected by the user, which will lead to the failure of the application using SecurityException , since the current application does not have sufficient privileges to send the intent:

  02-05 23: 11: 33.417: E / AndroidRuntime (20255): java.lang.SecurityException: Permission Denial: starting Intent {typ = text / plain flg = 0x3000000 cmp = com.google.android.gm / .AutoSendActivity (has extras)} from ProcessRecord {4084ca60 20255: com.example.myapp / 10065} (pid = 20255, uid = 10065) requires com.google.android.gm.permission.AUTO_SEND
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.os.Parcel.readException (Parcel.java:1322)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.os.Parcel.readException (Parcel.java:1276)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.app.ActivityManagerProxy.startActivity (ActivityManagerNative.java:1351)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.app.Instrumentation.execStartActivity (Instrumentation.java:1374)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.app.Activity.startActivityForResult (Activity.java:2827)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.app.Activity.startActivity (Activity.java:2933)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at com.android.internal.app.ResolverActivity.onIntentSelected (ResolverActivity.java:203)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at com.android.internal.app.ResolverActivity.onClick (ResolverActivity.java:117)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at com.android.internal.app.AlertController $ AlertParams $ 3.onItemClick (AlertController.java:873)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.widget.AdapterView.performItemClick (AdapterView.java:284)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.widget.ListView.performItemClick (ListView.java:3513)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.widget.AbsListView $ PerformClick.run (AbsListView.java:1812)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.os.Handler.handleCallback (Handler.java∗87)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.os.Handler.dispatchMessage (Handler.java:92)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.os.Looper.loop (Looper.java:130)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at android.app.ActivityThread.main (ActivityThread.javahaps683)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at java.lang.reflect.Method.invokeNative (Native Method)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at java.lang.reflect.Method.invoke (Method.java:507)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:839)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java∗97)
 02-05 23: 11: 33.417: E / AndroidRuntime (20255): at dalvik.system.NativeStart.main (Native Method)

As you can see, the exception does not apply directly to my code. I don’t want to add the missing manifest permission (I don’t know what other applications will be displayed in the selection element), but just handle a SecurityException and notify the user. Where can i do this?

In this case, I'm on Android 2.3.6.

Thanks in advance!

+4
source share
1 answer

But there are some applications if they are selected by the user, which will cause the application to crash using SecurityException, because the current application does not have sufficient privileges to send intend

This is a bug in another application. No application should support a general Intent action, such as ACTION_SEND , and requires special permission. I really recommend that you write this and send it to http://b.android.com with any other information you can provide to reproduce this error. I'm not sure which application you came across (the package offers perhaps Gmail), but this is clearly an application created by Google.

I don’t want to add the missing manifest permission (I don’t know what other applications will be displayed in the selection)

Not to mention that this permission is not in the SDK.

but just throw a SecurityException and notify the user. Where can i do this?

The key question is: is this exception really happening in your process?

If the answer is yes, then Thread and setDefaultUncaughtExceptionHandler() will encounter problems. You may already be using this to link a failure reporter (e.g. ACRA).

If the answer is no, then you are out of luck.

+4
source

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


All Articles