我编写了相同的程序,但是当我单击卸载按钮时,我无法获得任何日志信息.
我在下面附上我的代码.有谁知道这段代码中有什么问题?
我想在使用点击卸载按钮时做一些事情.也许喜欢打开浏览器等
有人可以帮我一把吗?
这个问题困扰了我很长一段时间.
我的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{@OverrIDepublic 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的原因是您的应用程序仍处于“停止状态”.您的应用程序没有任何活动组件.这意味着在安装应用程序后,用户无法启动它.用户从未启动的应用程序保持“停止状态”.处于“停止状态”的应用程序将不会接收广播意图.
请参阅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(Settings应用程序的一部分)中的代码如下所示:
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,Activity.RESulT_CANCELED,null); } }
因此,如果显示的应用程序是设备管理员,则“强制停止”按钮将被禁用.如果显示的应用程序未停止,则将启用“强制停止”按钮.否则,将广播Intent.ACTION_query_PACKAGE_RESTART.
这意味着只会为已停止的非设备管理应用程序广播Intent.
我用你的代码在4.0设备上测试了这个.如果您安装了应用程序,那么您转到Settings-> Apps并选择已被“强制停止”的其他应用程序(不是您的应用程序),使用Intent.ACTION_query_PACKAGE_RESTART调用onReceive().
我意识到这可能没什么帮助,但它至少解释了你所看到的行为.
对不起,解决这个问题花了这么长时间,并感谢您的挑战:-)
总结以上是内存溢出为你收集整理的android – 卸载应用时无法获取接收器全部内容,希望文章能够帮你解决android – 卸载应用时无法获取接收器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)