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
相关推荐
风和先行21 分钟前
adb 命令查看设备存储占用情况
android·adb
AaVictory.1 小时前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list
似霰2 小时前
安卓智能指针sp、wp、RefBase浅析
android·c++·binder
大风起兮云飞扬丶2 小时前
Android——网络请求
android
干一行,爱一行2 小时前
android camera data -> surface 显示
android
断墨先生2 小时前
uniapp—android原生插件开发(3Android真机调试)
android·uni-app
无极程序员4 小时前
PHP常量
android·ide·android studio
萌面小侠Plus5 小时前
Android笔记(三十三):封装设备性能级别判断工具——低端机还是高端机
android·性能优化·kotlin·工具类·低端机
慢慢成长的码农5 小时前
Android Profiler 内存分析
android
大风起兮云飞扬丶5 小时前
Android——多线程、线程通信、handler机制
android