我正在尝试在卸载应用程序之前向应用程序广播意图。为此,我在 SO 中研究并遵循了一些答案。我能够在安装时获得结果,但是当我从我的应用程序回到家时,由于 onStop 说,应用程序强制关闭
java.lang.RuntimeException: Unable to stop activity {com.xxx.xxxx}: java.lang.IllegalArgumentException: Receiver not registered: com.xxxx.xxxx.xxxxxx$1@4149f7a8
我所做的是,
public class AndroidTabLayoutActivity extends TabActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addDataScheme("package");
registerReceiver(NewPackageReceiver, intentFilter);
}
private BroadcastReceiver NewPackageReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("DEBUG", " test for application install/uninstall");
Toast.makeText(AndroidTabLayoutActivity.this,
"App Install/Uninstall", Toast.LENGTH_LONG).show();
}
};
@Override
protected void onStop()
{
super.onStop();
unregisterReceiver(NewPackageReceiver);
}
}
我的清单是
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.SET_TIME_ZONE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED"/>
<application
android:icon="@drawable/locksettings"
android:label="Locksettings" >
<receiver android:name=".NewPackageReceiver">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
在启动应用程序之前,我收到了安装日志消息。当我从应用程序回家时,应用程序正在强制关闭。任何人都可以指出我哪里出错了吗?