处理Lollipop中的隐含意图未来弃用 [英] Dealing with implicit intent future deprecation in Lollipop

查看:204
本文介绍了处理Lollipop中的隐含意图未来弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要将数据传输到其他应用程序,我一直在使用隐式意图,如下例所示:

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

Intent intent = new Intent();
intent.setAction("com.example.OpenURL");
intent.putExtra("URL_TO_OPEN", url_string);
sendOrderedBroadcastAsUser(intent);

Intent intent = new Intent();
intent.setAction("com.example.CreateUser");
intent.putExtra("Username", uname_string);
intent.putExtra("Password", pw_string);
sendBroadcast(intent);

Intent intent = new Intent();
intent.setAction("com.example.BackupUserData");
intent.setData(file_uri);
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
sendBroadcast(intent);

但在Android 5.0中不再推荐此行为

But this behavior is not recommended anymore in Android 5.0

http://developer.android.com/about/versions/android-5.0- changes.html


绑定到服务

Context.bindService()方法现在需要一个显式的Intent,并且如果给出一个隐式intent,则抛出
异常。为确保您的应用是安全的,请在启动或绑定服务时使用
显式意图,并且不要为服务声明意图
过滤器。

The Context.bindService() method now requires an explicit Intent, and throws an exception if given an implicit intent. To ensure your app is secure, use an explicit intent when starting or binding your Service, and do not declare intent filters for the service.

从android源代码更准确地说ContextImpl类:

From android source code more precisely "ContextImpl" class :

private void validateServiceIntent(Intent service) {
        if (service.getComponent() == null && service.getPackage() == null) {
            if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
                IllegalArgumentException ex = new IllegalArgumentException(
                        "Service Intent must be explicit: " + service);
                throw ex;
            } else {
                Log.w(TAG, "Implicit intents with startService are not safe: " + service
                        + " " + Debug.getCallers(2, 3));
            }
        }
    }

我该如何处理这个问题? ?

How can I deal with this ?

推荐答案

是的,在使用Android 5.0的设备上运行时,此代码会显示警告(如果您的应用为<$ c) $ c> targetSdkVersion 是< 21)或彻底崩溃(如果定位Lollipop本身)。查看 的源代码> validateServiceIntent() in ContextImpl.java

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.

最合理的替代方案可能是指定应用程序的包名称想通过 <$ c来致电$ C> Intent.setPackage() 。当这样做时,意图不再是隐含的,它将起作用。例如:

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).

这篇关于处理Lollipop中的隐含意图未来弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆