Browse Source

1.添加应用安装卸载的广播监听器

master
wangwanlei 11 months ago
parent
commit
1a5ad20d10

+ 33
- 0
app/src/main/java/com/xhly/manageapp/broadcastreceiver/AppInstallReceiver.kt View File

@@ -0,0 +1,33 @@
1
+package com.xhly.manageapp.broadcastreceiver
2
+
3
+import android.content.BroadcastReceiver
4
+import android.content.Context
5
+import android.content.Intent
6
+import android.text.TextUtils
7
+import android.util.Log
8
+import android.widget.Toast
9
+import com.xhly.corelib.Const
10
+import com.xhly.corelib.eventbus.UIEvent
11
+
12
+//(自定义广播类)  --自己命名的 XXXXReceiver
13
+class AppInstallReceiver : BroadcastReceiver() {
14
+    private val TAG = this.javaClass.simpleName
15
+    override fun onReceive(context: Context, intent: Intent) {
16
+        val pm = context.packageManager
17
+        Log.i("执行监听", "--------应用监听" + intent.data!!.schemeSpecificPart)
18
+        if (TextUtils.equals(intent.action, Intent.ACTION_PACKAGE_ADDED)) {
19
+            val packageName = intent.data!!.schemeSpecificPart
20
+            Log.i(TAG, "--------安装成功$packageName")
21
+            Toast.makeText(context, "安装成功$packageName", Toast.LENGTH_LONG).show()
22
+            UIEvent(Const.CODE2002.toString()).post()
23
+        } else if (TextUtils.equals(intent.action, Intent.ACTION_PACKAGE_REPLACED)) {
24
+            val packageName = intent.data!!.schemeSpecificPart
25
+            Log.i(TAG, "--------替换成功$packageName")
26
+            Toast.makeText(context, "替换成功$packageName", Toast.LENGTH_LONG).show()
27
+        } else if (TextUtils.equals(intent.action, Intent.ACTION_PACKAGE_REMOVED)) {
28
+            val packageName = intent.data!!.schemeSpecificPart
29
+            Log.i(TAG, "--------卸载成功$packageName")
30
+            Toast.makeText(context, "卸载成功$packageName", Toast.LENGTH_LONG).show()
31
+        }
32
+    }
33
+}

Loading…
Cancel
Save