【发布时间】:2015-03-14 21:32:42
【问题描述】:
为了将数据传输到其他应用程序,我一直在使用隐式意图,如下例所示:
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 中不再推荐这种行为
http://developer.android.com/about/versions/android-5.0-changes.html
绑定到服务
Context.bindService() 方法现在需要显式 Intent,并抛出 如果给出隐含意图,则例外。为确保您的应用安全,请使用 启动或绑定服务时的明确意图,并且不声明意图 服务的过滤器。
来自android源代码更准确地说是“ContextImpl”类:
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));
}
}
}
我该如何处理?
【问题讨论】:
-
更改只影响 bindService,从您发布的代码来看,您正在执行 sendBroadcast,AFAIK 它不应该影响您。
-
它说“在启动或绑定您的服务时”
-
根据您问题中的代码,您没有服务。
-
我明白了这里的重点,这些只是您可以用 startservice() 替换 sendbroadcast() 的示例。重要的部分是意图的构建。
标签: java android android-intent service intentfilter