How to handle SecurityException thrown back from s

2019-06-27 04:07发布

问题:

I'm using

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 can I send emails from my Android application?

But, there are some applications, if chosen by the user, that will crash the application with a SecurityException, as the current application does not have enough privileges to send the intend:

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:587)
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.java:3683)
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:597)
02-05 23:11:33.417: E/AndroidRuntime(20255):    at dalvik.system.NativeStart.main(Native Method)

As you can see, the exception is not touching any of my code directly. I don't want to add the missing permission to the manifest (I don't know which other applications will show up in the chooser), but simply handle the SecurityException and notify the user. Where would I do so?

I'm on Android 2.3.6 in this case.

Thanks in advance!

回答1:

But, there are some applications, if chosen by the user, that will crash the application with a SecurityException, as the current application does not have enough privileges to send the intend

That's a bug in the other app. No app should support a generic Intent action like ACTION_SEND and require a custom permission. I really recommend you write this up and post it to http://b.android.com, with whatever other information you can supply to reproduce this error. I'm not sure what app it is you ran into (the package suggests perhaps Gmail), but it's clearly a Google-produced app.

I don't want to add the missing permission to the manifest (I don't know which other applications will show up in the chooser)

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

but simply handle the SecurityException and notify the user. Where would I do so?

The key question is: is this exception actually occurring in your process?

If the answer is yes, then Thread and setDefaultUncaughtExceptionHandler() will be where you find out about the problem. You may already using this to tie in a crash reporter (e.g., ACRA).

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