【问题标题】:how to get Package name of app with broadcast receiver in android?如何在android中使用广播接收器获取应用程序的包名称?
【发布时间】:2015-04-15 11:24:28
【问题描述】:

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

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());

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

【问题讨论】:

  • 你想要你的应用或者其他应用的包名
  • 用户点击的任何应用程序

标签: android android-intent service broadcastreceiver android-broadcast


【解决方案1】:
  • 创建接收器

    <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()获取包裹

【讨论】:

    【解决方案2】:

    包名在你从BroadcasReceiver得到的Intent中,使用“getData()”函数,有安装/卸载包的ComponentName

    【讨论】:

      【解决方案3】:

      首先以您获取的方式获取ComponentName。之后获取 ApplicationInfo 实例

      PackageManager pm = context.getPackageManager();
      ApplicationInfo appInfo = pm.getApplicationInfo(componentName.getPackageName(), 0);
      

      现在获取包名称为 appInfo.packageName。 check here

      【讨论】:

      • 此处出现错误 ApplicationInfo appInfo = pm.getApplicationInfo(componentInfo.getPackageName(), 0);
      • 给出当前运行活动的包名:|我想要应用程序的名称
      • 你的问题是获取包名
      • yes 用户点击的那个应用的包名
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多