Android12通过发送广播方式实现一键清除

本文介绍了如何在Android系统中,通过在SystemUI应用中添加接收器和发送广播的方式,实现在其他APP中触发一键清除功能的过程。重点描述了在SystemUI的AndroidManifest.xml和RecentsReceiver.java文件中的相关代码修改。
摘要由CSDN通过智能技术生成

在公司实际项目中需要增加一个一键清除的功能。基于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")
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值