APK广播通过系统启动APK
需求:
双屏异显,通过副屏APK_A 打开一个APK_B 到主屏。
实现:
副屏APK发送一个广播到SystemUI,通过SystemUI启动APK,这样就不是副屏APK_A打开的APK_B,避开权限问题。
frameworks/base/packages/SystemUI/AndroidManifest.xml 设置广播监听:
@@ -516,5 +516,15 @@
</intent-filter>
</receiver>
+ <receiver android:name="com.android.systemui.usb.AppTest"
+ android:exported="true">
+ <intent-filter>
+ <action android:name="com.example.a1701.MainActivity" />
+ </intent-filter>
+ </receiver>
+
</application>
</manifest>
添加广播监听处理:frameworks/base/packages/SystemUI/src/com/android/systemui/usb/AppTest.java:
package com.android.systemui.usb;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.os.BuildCompat;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.IPowerManager;
import android.util.Log;
import com.android.internal.view.RotationPolicy;
import android.content.IntentFilter;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
import java.util.Map;
import android.provider.Settings;
public class AppTest extends BroadcastReceiver{
private static final boolean DEBUG = true;
private static final String TAG = "DICKE";
private static final String TEST = "com.example.a1701.MainActivity";
@Override
public void onReceive(Context context, Intent intent) {
if (DEBUG) Log.d(TAG, " Broadcast intent received:" + intent);
if(TEST.equals(intent.getAction())){
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("包名");
context.startActivity(launchIntent);
}
}
}
APK_A 添加广播发送:
Intent intent = new Intent("com.example.a1701.MainActivity"); sendBroadcast(intent, null);
这样APK_A发送广播,系统就会监听到,并启动APK_B。
本文地址:https://blog.csdn.net/dickezuo/article/details/107186608