gpt4 book ai didi

android - 应用程序卸载时无法获取接收器

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:24 25 4
gpt4 key购买 nike

我指的是 this信息。

我写了同样的程序,但是当我点击卸载按钮时,我无法获得任何日志信息。

我在下面附上我的代码。有谁知道这段代码有什么问题吗?

我想在使用单击卸载按钮时做一些事情。可能像打开浏览器之类的。

有没有人可以帮我一把?

这个问题困扰了我很久。

我的 AndroidManifest:

...

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.RESTART_PACKAGES"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".UninstallIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
<action android:name = "android.intent.action.PACKAGE_REMOVED" />
<action android:name = "android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"></data>
</intent-filter>
</receiver>
</application>

...

我的 BroadcastReceiver 代码如下:

public class UninstallIntentReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

String [] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");
if(packageNames!=null){
for(String packageName: packageNames){
if(packageName!=null && packageName.equals("yu.idv.uninstalldetected")){
Log.i("info", "00000000");
new ListenActivities(context).start();
Log.i("info", "11111111");

}
}
}
}

}

class ListenActivities extends Thread{
boolean exit = false;
ActivityManager am = null;
Context context = null;

public ListenActivities(Context con){
context = con;
am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}

public void run(){

Looper.prepare();

while(!exit){

List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(MAX_PRIORITY);

String activityName = taskInfo.get(0).topActivity.getClassName();


Log.i("info", "======CURRENT Activity =======::"
+ activityName);

if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
exit = true;
Log.i("info", "2222222222");
Toast.makeText(context, "Done with preuninstallation tasks... Exiting Now", Toast.LENGTH_SHORT).show();
} else if(activityName.equals("com.android.settings.ManageApplications")) {
exit=true;
Log.i("info", "33333333333");
}
}
Looper.loop();
}

最佳答案

BroadcastReceiver 未被调用的原因是您的应用程序仍处于“停止状态”。您的应用程序没有任何 Activity 组件。这意味着应用程序安装后,用户无法启动它。用户从未启动过的应用程序停留在“停止状态”。处于“停止状态”的应用程序不会收到广播 Intents

请参阅 http://developer.android.com/about/versions/android-3.1.html 中有关“在已停止的应用程序上启动控件”的部分


编辑:在 Android 4.x 中添加有关 Intent.ACTION_QUERY_PACKAGE_RESTART 的更多详细信息

似乎这种行为自 Android 2.3 以来发生了变化(我不确定 Android 3.x)。在 Android 4.0 及更高版本中,InstalledAppDetails(设置应用程序的一部分)中的代码如下所示:

 private void checkForceStop() {
if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
// User can't force stop device admin.
updateForceStopButton(false);
} else if ((mAppEntry.info.flags&ApplicationInfo.FLAG_STOPPED) == 0) {
// If the app isn't explicitly stopped, then always show the
// force stop button.
updateForceStopButton(true);
} else {
Intent intent = new Intent(Intent.ACTION_QUERY_PACKAGE_RESTART,
Uri.fromParts("package", mAppEntry.info.packageName, null));
intent.putExtra(Intent.EXTRA_PACKAGES, new String[] { mAppEntry.info.packageName });
intent.putExtra(Intent.EXTRA_UID, mAppEntry.info.uid);
getActivity().sendOrderedBroadcast(intent, null, mCheckKillProcessesReceiver, null,
Activity.RESULT_CANCELED, null, null);
}
}

因此,如果显示的应用程序是设备管理员,“强制停止”按钮将被禁用。如果显示的应用程序未停止,则“强制停止”按钮将启用。 否则,将广播Intent.ACTION_QUERY_PACKAGE_RESTART

这意味着 Intent 将只为已经停止的非设备管理应用程序广播。

我使用您的代码在 4.0 设备上对此进行了测试。如果您安装了您的应用程序,那么您转到设置->应用程序并选择已被“强制停止”的另一个应用程序(不是您的),您的onReceive( ) 通过 Intent.ACTION_QUERY_PACKAGE_RESTART 调用。

我意识到这可能没有多大帮助,但它至少解释了您所看到的行为。

抱歉花了这么长时间才解决这个问题,感谢您的挑战:-)

关于android - 应用程序卸载时无法获取接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20699640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com