在公司实际项目中需要增加一个一键清除的功能。基于Android系统权限的日益收紧,发现以广播的方式是相对简单的。
主要思路如下:
1.在SystemUI中实现接收广播,并执行一键清除的动作
2.在其他APP实现发送广播
1.在SystemUI中实现接收广播,并执行一键清除的动作
[Broadcast][clear]Adding broadcasts for one click clear
diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml b/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml
index 1b5fb8fd043..186ee9d1702 100644
--- a/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml
+++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml
@@ -895,6 +895,14 @@
<action android:name="com.android.systemui.action.DISMISS_MEDIA_OUTPUT_DIALOG" />
</intent-filter>
</receiver>
+
+ <receiver
+ android:name=".broadcast.RecentsReceiver"
+ android:exported="true">
+ <intent-filter>
+ <action android:name="launcher.intent.action.RECENTS_DEL" />
+ </intent-filter>
+ </receiver>
</application>
</manifest>
diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/broadcast/RecentsReceiver.java b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/broadcast/RecentsReceiver.java
new file mode 100755
index 00000000000..2fc51c21e7a
--- /dev/null
+++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/broadcast/RecentsReceiver.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.systemui.broadcast;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+import com.android.systemui.shared.system.ActivityManagerWrapper;
+
+/**
+ * Receiver for the Keyboard Shortcuts Helper.
+ */
+public class RecentsReceiver extends BroadcastReceiver {
+
+ private static final String TAG = "RecentsView";
+ private static final String ACTION_RECENTS_AXB = "launcher.intent.action.RECENTS_DEL";
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ Log.d(TAG, "Recents action = " + action);
+ if (ACTION_RECENTS_AXB.equals(action)) {
+ //Bundle bundle = intent.getExtras();
+ Log.d(TAG, "removeAllRecentTasks = before");
+ ActivityManagerWrapper.getInstance().removeAllRecentTasks();
+ Log.d(TAG, "removeAllRecentTasks = after");
+ }
+ }
+}
2.在其他APP实现发送广播
private fun broadcastInvoke() {
val intent: Intent = Intent()
intent.putExtra("broadcast", "success send")
intent.action = "launcher.intent.action.RECENTS_DEL"
intent.setPackage("com.android.systemui")
sendBroadcast(intent)
Log.d(TAG, "send: 111111111111111111")
}