我正在尝试编写一段代码,让我能够从手机上最后安装的应用程序中获取常用名称、包名称和图标。目前我所拥有的是如何从此来源 ( get application name from package name ) 获取包名称和通用名称,但这对我不起作用。
第一个错误是“无法解析方法 getApplicationContext 和 getPackageName”。这是有道理的,因为这些方法是“Activity”而不是“BroadcastReceiver”的原生方法(我不知道其他人是如何让它工作的)。
然后我创建了一个私有(private)上下文,以便我可以使用 getApplicationContext 和 getPackageName。我的代码现在看起来像我在下面发布的那样,gradle 构建,但是当我在手机上安装另一个应用程序时我的应用程序崩溃并出现错误:
can't instantiate class com.example.natalievold.applistener.NewInstallReceiver; no empty constructor
我读到我可以通过删除我添加的“私有(private)上下文”部分来解决此错误,但我需要它来使用 getApplicationContext 和 getPackageName。有谁知道我可以这样做的另一种方法?我也不确定如何获取上次安装的应用程序的图标。
public class NewInstallReceiver extends BroadcastReceiver
{
private Context mContext;
public NewInstallReceiver(Context context) {
mContext = context;
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals("android.intent.action.PACKAGE_ADDED")) {
Logger.getLogger("DATA:" + intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REMOVED")){
Logger.getLogger("DATA:" + intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REPLACED")){
Logger.getLogger("DATA:" + intent.getData().toString());
}
final PackageManager pm = mContext.getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( this.mContext.getPackageName(), 0);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
}
}
我的 list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.natalievold.applistener">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name="com.example.natalievold.applistener.NewInstallReceiver">
<intent-filter android:priority="100">
<action
android:name="android.intent.action.PACKAGE_INSTALL"/>
<action
android:name="android.intent.action.PACKAGE_ADDED"/>
<action
android:name="android.intent.action.PACKAGE_DATA_CLEARED"/>
<action
android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我要工作的最终代码! (这只是安装应用程序的部分,而不是卸载)。而且我没有更改 list 中的任何内容。
public class NewInstallReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Log.d("NewInstallReceiver", "Intent: " + intent.getAction());
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( intent.getData().getSchemeSpecificPart(), 0);
Log.d("PACKAGE NAME","Intent" + ai);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
Log.d("Application NAME", "Intent: " + applicationName);
Drawable icon = context.getPackageManager().getApplicationIcon(ai);
Log.d("Application ICON", "Intent: " + icon);
}
}
最佳答案
I read that I can solve this error by removing the "private context" section that I added, but I need that to use getApplicationContext and getPackageName.
不,你没有。
Does anyone know another way I can do this?
首先,使用作为第一个参数传递给您的onReceive()
方法的Context
。这将允许您替换它:
final PackageManager pm = mContext.getApplicationContext().getPackageManager();
用这个:
final PackageManager pm = context.getPackageManager();
其次,getPackageName()
在您自己的 Context
上调用,为您提供您的 包名称。您似乎不想要自己的包名称。相反,您需要已安装应用程序的包名称。为此,请调用 intent.getData().getSchemeSpecificPart()
,以在 Uri
中获取 package:
方案之后的内容广播的 Intent
。
然后,要获取有关该包的其他信息,您可以在 PackageManager
中查找它。在 PackageManager
上调用 getApplicationInfo()
,传入包名称。这将为您提供一个 ApplicationInfo
对象。将其传递给 PackageManager
上的 getApplicationLabel()
和 getApplicationIcon()
以分别获取标签和图标。
关于java - 使用 BroadcastReceiver 从 LAST INSTALLED 应用程序中获取包名称、通用名称和图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36658249/