Dealing with implicit intent future deprecation in Lollipop

前端 未结 3 431
清歌不尽
清歌不尽 2021-02-04 10:06

To transmit data to other applications I\'ve been using implicit intents as in examples below:

Intent intent = new Intent();
intent.setAction(\"com.example.OpenU         


        
3条回答
  •  灰色年华
    2021-02-04 10:54

    Yes, when running in a device with Android 5.0 this code will either show a warning (if your app's targetSdkVersion is < 21) or crash outright (if targeting Lollipop itself). Check the source code for validateServiceIntent() in ContextImpl.java:

    This code is dangerous as it might allow a malicious receiver to register itself and intercept (or alter) these calls.

    The most reasonable alternative is probably to specify the package name of the app you want to call, by using Intent.setPackage(). When done this way, the intent is no longer implicit, and it will work. For example:

       intent.setPackage("com.example.app");
    

    Of course, if the receiver is inside your app, there are easier alternatives (but that does not seem to be the case, from your description of the issue).

提交回复
热议问题