android:exported属性认识
最近工作中在开发一个模块,其中带有创建桌面快捷方式功能的时候,发现同事一款vivo国产手机创建不了,日志如下:
BroadcastQueue: Permission Denial: broadcasting Intent { act=com.android.launcher.action.INSTALL_SHORTCUT flg=0x10 (has extras) } from com.yongche.android (pid=12696, uid=10146) is not exported from uid 10124 due to receiver com.sina.weibo/.gowidget.GoWidgetProvider
其中看到了关键字“exported”,之前也看到过,只知道是对其它进程暴露的意思,在四大组件中都可以进行声明,但是木有做过实验,所以利用闲暇之余对其进行学习一下。
在学习之前,首先需要对它要有一个概念上的认识,当然从网上去扒喽,可以看看该博主的:http://blog.csdn.net/watermusicyes/article/details/46460347
比较容易理解,可以先读读,下面用代码来实践一下,就以广播为例:
新建两个工程,一个里面注册一个自定义广播,另外一个用来发送广播,具体如下:


MyReceiver.java:
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent arg1) { Toast.makeText(arg0, "ExportedTestA BroadcastReceiver invoked~~", Toast.LENGTH_SHORT).show(); } }
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exportedtesta"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver">
<intent-filter>
<action android:name="com.example.exportedtesta.action.MyReceiver" />
</intent-filter>
</receiver>
</application>
</manifest>
很简单,就是注册一个广播,接着第二个应用去调它:

下面分三个情况来观察:
情况一:广播注册时没有配置android:exported属性:
目前就是这种,所以运行看结果:

也就说明默认情况下也就是android:exported="true",如博主说的。
情况二:广播注册时配置android:exported="true"属性:
这种情况可想而知,跟情况一一样,正常可以收到广播。
情况三:广播注册时配置android:exported="false"属性:

这时再运行,居然任何提示都木有,看样子木调用成功,看日志:
这不就是在工作中出现的类似日志么?所以如果当android:exported="false"之后,跨进程就无法访问了,但是同一进程是没问题的。
浙公网安备 33010602011771号