iMO*_*DEV 6 service android uninstall
首先,我已经研究了很多关于我的问题,但我找不到合适的解决方案,所以我在这里发布我的查询.希望能够更好地解决这个问题:
在用户从设置或任何其他应用程序(如MyAppSharer)删除我的应用程序之前,我需要向用户请求密码.我找到了一个解决方案,当用户点击卸载按钮时,我可以成功地调用我的活动.我在这里申请了技巧,并致电服务.在服务中,我运行计时器,每1秒运行一次,在那一秒,它检查运行任务的最顶层活动.这按预期完美运行.
现在,我的问题是,此活动适用于每个应用程序用户尝试卸载.我需要我调用的活动应该仅在用户尝试卸载我的应用程序时出现在我的应用程序中.
这是我的代码:
public static final String PACKAGE_INSTALLER = "com.android.packageinstaller";
public static final String PACKAGE_INSTALLER_UNINSTALL_ACTIVITY = "com.android.packageinstaller.UninstallerActivity";
alarmTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);;
ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;
final String packageName = topActivity.getPackageName();
String className = topActivity.getClassName();
Log.v(TAG, "packageName:" + packageName);
Log.v(TAG, "className:" + className);
if (PACKAGE_INSTALLER.equals(packageName)
&& PACKAGE_INSTALLER_UNINSTALL_ACTIVITY.equals(className)) {
//Here I need to apply one condition where package name received to be matched with my package name. But I am not sure how to fetch package name of selected application for uninstalling
//To Cancel Existing UninstallerActivity and redirect user to home.
Intent homeIntent = new Intent();
homeIntent.setAction(Intent.ACTION_MAIN);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
homeIntent.addCategory(Intent.CATEGORY_HOME);
startActivity(homeIntent);
//To open my activity
Intent loginActivity = new Intent(UninstallService.this, Act_Login.class);
loginActivity.putExtra(Constants.KEY_IS_FROM_SERVICE, true);
loginActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(loginActivity);
}
}
}, 0, 1000);
Run Code Online (Sandbox Code Playgroud)
小智 5
你应该尝试类似以下的东西:
1st - 在Manifest文件中声明你的广播接收者,它将收听QUERY_PACKAGE_RESTART:
<receiver android:name=".UninstallReceiver">
<intent-filter android:priority="999999">
<action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
第二个 - 你的UnunstallIntentReceiver java类如下:
public class UninstallReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// fetching package names from extras
String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");
if(packageNames!=null){
for(String packageName: packageNames){
if(packageName!=null && packageName.equals("application_package")){
// start your activity here and ask the user for the password
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请给我一些反馈
希望有所帮助.
如果这是公司要求(如果您想阻止普通用户卸载您的应用程序,没有机会,感谢 Google 保护我们免受不良开发人员的侵害),您应该创建一个设备管理员应用程序。这样,尽管用户仍然可以删除该应用程序,但如果您想防止意外删除,则需要执行一个额外步骤。
\n\n在删除您的应用程序之前,如果以设备管理员身份启用该应用程序,则用户必须首先以管理员身份禁用该应用程序,然后该应用程序会收到此广播。
\n\n在您的 XML 中,输入
\n\n<activity android:name=".app.DeviceAdminSample"\n android:label="@string/activity_sample_device_admin">\n <intent-filter>\n <action android:name="android.intent.action.MAIN" />\n <category android:name="android.intent.category.SAMPLE_CODE" />\n </intent-filter>\n</activity>\n<receiver android:name=".app.DeviceAdminSample$DeviceAdminSampleReceiver"\n android:label="@string/sample_device_admin"\n android:description="@string/sample_device_admin_description"\n android:permission="android.permission.BIND_DEVICE_ADMIN">\n <meta-data android:name="android.app.device_admin"\n android:resource="@xml/device_admin_sample" />\n <intent-filter>\n <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />\n </intent-filter>\n</receiver>\nRun Code Online (Sandbox Code Playgroud)\n\n在接收器中,至少有两个方法值得注意:
\n\n@Override\npublic CharSequence onDisableRequested(Context context, Intent intent) {\n \xe2\x80\xa6\n}\n\n@Override\npublic void onDisabled(Context context, Intent intent) {\n \xe2\x80\xa6\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这样您就知道用户可能会删除您的应用程序。
\n\n设备管理的完整指南位于https://developer.android.com/guide/topics/admin/device-admin.html
\n| 归档时间: |
|
| 查看次数: |
6347 次 |
| 最近记录: |