Android-Framework 应用间跳转时,提供 Android Broadcast 通知

一、环境

高通865 Android 10

二、情景

应用跳转时,通过广播发送源app的包名和目标app的包名

三、代码实现

frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java
java 复制代码
@@ -132,6 +132,14 @@ import java.io.PrintWriter;
 import java.text.DateFormat;
 import java.util.Date;
 
+//added begin
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.os.Parcelable;
+import android.os.HandlerThread;
+//added end
+
 /**
  * Controller for interpreting how and then launching an activity.
  *
@@ -242,6 +250,39 @@ class ActivityStarter {
         void recycle(ActivityStarter starter);
     }
 
+    private Handler mHandler;
+    private static final int DO_SEND_ACTIVITY_LAUNCHES_MSG = 1;
+
+    //added  begin
+    private final class pkgHandler extends Handler {
+        public pkgHandler(Looper looper) {
+            super(looper, null, true);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch(msg.what) {
+                case DO_SEND_ACTIVITY_LAUNCHES_MSG:
+                   android.util.Log.d("qxy","ActivityStartController --> handleMessage --> DO_SEND_ACTIVITY_LAUNCHES_MSG");
+                   android.util.Log.d("qxy","Thread.currentThread().getId() = " + Thread.currentThread().getId()
+                     + "\n" + "getMainLooper().getThread().getId() = " + mService.mContext.getMainLooper().getThread().getId());
+                    Bundle bundle = msg.getData();
+                    String pksFrom = bundle.getString("pksFrom");
+                    String pksTo = bundle.getString("pksTo");
+                    android.util.Log.d("qxy","pksFrom :" + pksFrom + " pksTo = " + pksTo);
+                    Intent mIntent = new Intent();
+                    mIntent.setAction("com.sc.app.launcher.jump");
+                    mIntent.putExtra("pksFrom",pksFrom);
+                    mIntent.putExtra("pksTo",pksTo);
+                    mService.mContext.sendBroadcast(mIntent);
+                    break;
+            }
+        }
+    }
+    //added  end
+
+
+
     /**
      * Default implementation of {@link StarterFactory}.
      */
@@ -1228,6 +1269,27 @@ class ActivityStarter {
         // Collect information about the target of the Intent.
         ActivityInfo aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags, profilerInfo);
 
+        //add begin
+         /*
+          From -> callingPackage;
+          To -> intent.getComponent().getPackageName();
+         */
+       if(callingPackage != null && !callingPackage.equals(intent.getComponent().getPackageName())){
+            HandlerThread handlerThread = new HandlerThread("handler_thread");
+            handlerThread.start();
+            mHandler = new pkgHandler(handlerThread.getLooper());
+            mHandler.removeMessages(DO_SEND_ACTIVITY_LAUNCHES_MSG);
+            Message msg = mHandler.obtainMessage(DO_SEND_ACTIVITY_LAUNCHES_MSG);
+            Bundle mBundle = new Bundle();
+            mBundle.putString("pksFrom", callingPackage);
+            mBundle.putString("pksTo", intent.getComponent().getPackageName());
+            msg.setData(mBundle);
+            mHandler.sendMessage(msg);
+        }add end
+
+
+
         synchronized (mService.mGlobalLock) {
             final ActivityStack stack = mRootActivityContainer.getTopDisplayFocusedStack();
             stack.mConfigWillChange = globalConfig != null
相关推荐
张风捷特烈7 小时前
Flutter 伪3D绘制#03 | 轴测投影原理分析
android·flutter·canvas
omegayy10 小时前
Unity 2022.3.x部分Android设备播放视频黑屏问题
android·unity·视频播放·黑屏
mingqian_chu10 小时前
ubuntu中使用安卓模拟器
android·linux·ubuntu
自动花钱机10 小时前
Kotlin问题汇总
android·开发语言·kotlin
行墨13 小时前
Kotlin 主构造函数
android
前行的小黑炭13 小时前
Android从传统的XML转到Compose的变化:mutableStateOf、MutableStateFlow;有的使用by有的使用by remember
android·kotlin
_一条咸鱼_13 小时前
Android Compose 框架尺寸与密度深入剖析(五十五)
android
在狂风暴雨中奔跑13 小时前
使用AI开发Android界面
android·人工智能
行墨13 小时前
Kotlin 定义类与field关键
android
信徒_14 小时前
Mysql 在什么样的情况下会产生死锁?
android·数据库·mysql