首先,我已經研究了很多關於我的問題,但找不到合適的解決方案,因此我在此處發佈我的查詢。希望能夠得到更好的解決方案:在卸載應用程序之前詢問密碼
我有一個需求,在用戶從設置或其他任何應用程序(如MyAppSharer)中刪除我的應用程序之前,需要向用戶請求密碼。我找到了一個解決方案,當用戶單擊卸載按鈕時,我可以成功地調用我的活動。我在這裏應用了技巧,然後打電話給服務。在服務中,我運行每1秒運行一次的計時器,並在那一秒鐘內檢查運行任務的最頂級活動。這按預期完美運行。
現在,我的問題是,這個活動apppears每個應用程序用戶試圖卸載。我需要的是我調用的活動,應該只在用戶嘗試卸載我的應用程序時出現在我的應用程序中。
這裏是我的代碼:
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);
我不確定你背後的意圖,但從用戶的角度來看,我不會喜歡一個應用程序,當我卸載它的時候會問這個應用程序。事實上,我卸載應用程序意味着要麼我不滿意,要麼我有更好的選擇,或者可能是別的。不管是什麼原因,在這個時候要求一些東西可能會讓我更加煩惱。所以要小心。我希望你的意圖是好的。 –
@AtolOHolic我認爲他們正在試圖製造病毒? :) – Deeps
是否記錄了軟件包名稱,並確定您獲取軟件包名稱的方式是否正確? –