Android 8.1静态广播的使用以及添加权限
程序员文章站
2022-08-07 16:14:27
为了发送广播的时候只让特定的某一个应用程序APP接收到数据,而不想其他应用程序接收到数据的时候,需要配置权限。一、广播接收者设置权限:在AndroidManifest.xml中添加权限和定义静态广播
在Android 8.1后我使用静态广播时会发现接收不到广播,下面有两种方法:
1、动态广播替换静态广播
2、保留原来的静态广播,使用setComponent参数
Intent intent = new Intent();
intent.setAction("com.wq.package.silentinstaller");
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
intent.setComponent(new ComponentName("com.android.packageinstaller",
"com.android.packageinstaller.MyReceiver"));
sendBroadcast(intent);
为了发送广播的时候只让特定的某一个应用程序APP接收到数据,而不想其他应用程序接收到数据的时候,需要配置权限。
一、广播接收者设置权限:
在AndroidManifest.xml中添加权限和定义静态广播
<permission
android:name="zqc.permission.SILENT_INSTALL_PERMISSIONS"
android:protectionLevel="normal" />
<receiver android:name="com.android.packageinstaller.MyReceiver"
android:permission="wq.permission.SILENT_INSTALL_PERMISSIONS"
android:exported="true">
<intent-filter android:priority="1">
<action android:name="com.wq.package.silentinstaller" />
</intent-filter>
</receiver>
创建一个静态广播MyReceiver.java类
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "SilentInstallReceiver";
private Context mContext;
private static final String ACTION_SILENCE_INSTALL = "com.wq.package.silentinstaller";
private static final String KEY_FILE_PATH = "file_path";
private static final String KEY_PACKAGE_NAME = "package_name";
private static final int MSG_SILENT_INSTALL = 1;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_SILENT_INSTALL:
Intent intent = (Intent) msg.obj;
String filePath = intent.getStringExtra(KEY_FILE_PATH);
String packageName = intent.getStringExtra(KEY_PACKAGE_NAME);
Log.i(TAG, "---MSG_SILENT_INSTALL filePath=" + filePath + ",packageName=" + packageName);
SilentInstallManager.getInstance(mContext).startInstall(filePath);
break;
default:
break;
}
};
};
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "onReceive() action="+action);
if (ACTION_SILENCE_INSTALL.equals(action)) {
mContext = context;
Message msg = Message.obtain(mHandler);
msg.what = MSG_SILENT_INSTALL;
msg.obj = intent;
mHandler.sendMessage(msg);
}
}
/**
* installPackageName : The package name of apk you want to install.
* apkFilePath : The path of the apk file.
*
private void install(String installPackageName, String apkFilePath){
Intent intent = new Intent();
intent.setAction("com.wq.package.silentinstaller");
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
intent.setComponent(new ComponentName("com.android.packageinstaller",
"com.android.packageinstaller.MyReceiver"));
intent.putExtra("package_name", installPackageName);
intent.putExtra("file_path", apkFilePath);
mContext.sendBroadcast(intent);
}
*/
}
二、广播发送者
发送广播的应用装备该权限
<uses-permission android:name="wq.permission.SILENT_INSTALL_PERMISSIONS" />
添加一个按钮发送广播
Intent intent = new Intent();
intent.setAction("com.wq.package.silentinstaller");
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
intent.setComponent(new ComponentName("com.android.packageinstaller",
"com.android.packageinstaller.MyReceiver"));
sendBroadcast(intent);
本文地址:https://blog.csdn.net/wq892373445/article/details/107204569