使用 BroadcastReceiver 从 LAST INSTALLED 应用程序中获取包名称、通用名称和图标
Getting the package name, common name, and icon from LAST INSTALLED app using BroadcastReceiver
我正在尝试编写一段代码,使我能够从 phone 上最后安装的应用程序中获取常用名称、包名称和图标。目前我所拥有的是如何从此源 (get application name from package name) 获取包名称和通用名称,但这对我不起作用。
第一个错误是 "Cannot resolve method getApplicationContext and getPackageName"。这是有道理的,因为这些方法是 "Activity" 而不是 "BroadcastReceiver" 的原生方法(我不知道其他人是如何让它工作的)。
然后我创建了一个私有上下文,以便我可以使用 getApplicationContext 和 getPackageName。我的代码现在看起来像我在下面发布的 gradle 版本,但是当我在我的 phone 上安装另一个应用程序时我的应用程序崩溃并出现错误:
can't instantiate class com.example.natalievold.applistener.NewInstallReceiver; no empty constructor
我读到我可以通过删除我添加的 "private context" 部分来解决此错误,但我需要它来使用 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)");
}
}
我的清单:
<?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>
这是我开始工作的最终代码! (这只是安装应用程序的部分,而不是卸载)。而且我没有更改清单中的任何内容。
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()
,以获取已广播的 Intent
的 Uri
中 package:
方案之后的内容。
然后,要获取有关该包的其他信息,您可以在 PackageManager
中查找它。在 PackageManager
上调用 getApplicationInfo()
,传入包名称。这将为您提供一个 ApplicationInfo
对象。将其传递给 PackageManager
上的 getApplicationLabel()
和 getApplicationIcon()
以分别获取标签和图标。
我正在尝试编写一段代码,使我能够从 phone 上最后安装的应用程序中获取常用名称、包名称和图标。目前我所拥有的是如何从此源 (get application name from package name) 获取包名称和通用名称,但这对我不起作用。
第一个错误是 "Cannot resolve method getApplicationContext and getPackageName"。这是有道理的,因为这些方法是 "Activity" 而不是 "BroadcastReceiver" 的原生方法(我不知道其他人是如何让它工作的)。
然后我创建了一个私有上下文,以便我可以使用 getApplicationContext 和 getPackageName。我的代码现在看起来像我在下面发布的 gradle 版本,但是当我在我的 phone 上安装另一个应用程序时我的应用程序崩溃并出现错误:
can't instantiate class com.example.natalievold.applistener.NewInstallReceiver; no empty constructor
我读到我可以通过删除我添加的 "private context" 部分来解决此错误,但我需要它来使用 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)");
}
}
我的清单:
<?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>
这是我开始工作的最终代码! (这只是安装应用程序的部分,而不是卸载)。而且我没有更改清单中的任何内容。
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()
,以获取已广播的 Intent
的 Uri
中 package:
方案之后的内容。
然后,要获取有关该包的其他信息,您可以在 PackageManager
中查找它。在 PackageManager
上调用 getApplicationInfo()
,传入包名称。这将为您提供一个 ApplicationInfo
对象。将其传递给 PackageManager
上的 getApplicationLabel()
和 getApplicationIcon()
以分别获取标签和图标。