如何获取已安装或已删除应用程序的名称?
How to get name of installed or removed application?
我编写了一个广播接收器来检测安装和删除应用程序。
但我也想获取已安装或已删除应用程序的名称。
我该怎么做?
这是我的广播接收器:
public class PackageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch(intent.getAction())
{
case Intent.ACTION_PACKAGE_ADDED:
String replaced = "";
if(intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
{
replaced = "replaced";
}
Log.e("application", "installed " + replaced);
break;
case Intent.ACTION_PACKAGE_REMOVED:
if(!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
{
Log.e("application", "removed");
}
break;
}
}
}
清单中:
<receiver
android:name=".receivers.PackageReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
结合在 android documentation and this answer 中发现的堆栈溢出信息,我得出以下结论。您正在使用的 Intent 可能有 EXTRA_UID extra,其中包含修改后的应用程序的 uid。使用 uid 您可以获得应用程序名称。但是你只能在 ACTION_PACKAGE_ADDED 意图上这样做,因为在 ACTION_PACKAGE_REMOVED 中应用程序已经被删除并且你无法获取它的名称(你仍然可以获得 uid)。
检查此示例:
int uid = intent.getIntegerExtra(Intent.EXTRA_UID);
String appName = context.getPackageManager().getNameForUid(uid);
所以在你的情况下会是:
public class PackageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch(intent.getAction())
{
case Intent.ACTION_PACKAGE_ADDED:
String replaced = "";
String appName = "";
int uid = -1;
if(intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
{
replaced = "replaced";
}
uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
if(uid != -1){
appName = context.getPackageManager().getNameForUid(uid);
}
Log.e("application", "installed " + replaced + " uid " + uid + " appname " + appName);
break;
case Intent.ACTION_PACKAGE_REMOVED:
if(!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
{
Log.e("application", "removed");
}
break;
}
}
}
通过 Google 安装 Google 地球后,我在 logcat 中看到了这段代码 播放:
installed uid 10404 appname com.google.earth
我编写了一个广播接收器来检测安装和删除应用程序。 但我也想获取已安装或已删除应用程序的名称。 我该怎么做?
这是我的广播接收器:
public class PackageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch(intent.getAction())
{
case Intent.ACTION_PACKAGE_ADDED:
String replaced = "";
if(intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
{
replaced = "replaced";
}
Log.e("application", "installed " + replaced);
break;
case Intent.ACTION_PACKAGE_REMOVED:
if(!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
{
Log.e("application", "removed");
}
break;
}
}
}
清单中:
<receiver
android:name=".receivers.PackageReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
结合在 android documentation and this answer 中发现的堆栈溢出信息,我得出以下结论。您正在使用的 Intent 可能有 EXTRA_UID extra,其中包含修改后的应用程序的 uid。使用 uid 您可以获得应用程序名称。但是你只能在 ACTION_PACKAGE_ADDED 意图上这样做,因为在 ACTION_PACKAGE_REMOVED 中应用程序已经被删除并且你无法获取它的名称(你仍然可以获得 uid)。
检查此示例:
int uid = intent.getIntegerExtra(Intent.EXTRA_UID);
String appName = context.getPackageManager().getNameForUid(uid);
所以在你的情况下会是:
public class PackageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch(intent.getAction())
{
case Intent.ACTION_PACKAGE_ADDED:
String replaced = "";
String appName = "";
int uid = -1;
if(intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
{
replaced = "replaced";
}
uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
if(uid != -1){
appName = context.getPackageManager().getNameForUid(uid);
}
Log.e("application", "installed " + replaced + " uid " + uid + " appname " + appName);
break;
case Intent.ACTION_PACKAGE_REMOVED:
if(!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
{
Log.e("application", "removed");
}
break;
}
}
}
通过 Google 安装 Google 地球后,我在 logcat 中看到了这段代码 播放:
installed uid 10404 appname com.google.earth