android - 如何在android中使用广播接收器获取应用程序的包名称?

标签 android android-intent service broadcastreceiver android-broadcast

我想获取用户试图卸载的应用程序包名称。我正在使用以下代码:

List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1); 

Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());

ComponentName componentInfo = taskInfo.get(0).topActivity;
Log.d("topActivity", "CURRENT Package ::" + componentInfo.getPackageName());

但它给出了当前正在运行的 Activity 的包名称。我想获取该特定应用程序的包名称。任何人都知道请帮助我。

最佳答案

  • 创建接收器

    <receiver android:name=".PackageReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED"/> <action android:name="android.intent.action.PACKAGE_CHANGED"/> <action android:name="android.intent.action.PACKAGE_REMOVED"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="package"/> </intent-filter> </receiver>

  • 在 OnReceive() 中 通过 intent.getAction() 采取行动
    通过 intent.getData().getSchemeSpecificPart() 获取包裹

关于android - 如何在android中使用广播接收器获取应用程序的包名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29648931/

相关文章:

android - Robolectric 是否支持 API 级别?

java - 如何在 Android 上 MediaStore.Images.Media 选择图像后调整图像大小

android - 从服务传递数据

.net - 我如何使用 OpenCover 连接到服务?

android - file.delete() 不工作

android - 可信执行环境(TEE)中的客户端应用程序和可信应用程序

java - Android,ListView中的交替行布局,可以用ArrayAdapter来完成吗?

Android:打开短信 Intent

android - FLAG_ACTIVITY_CLEAR_TOP 和 onActivityResult

java - j2ee服务层自动化测试用例