Reputation: 1
I'm developing an app which stores info about Calls and Messages. I don't want the user to uninstall the app without entering a password. I want to prevent the user to do that. I've looked into these links too, but I couldn't get a clue: Ask for password before uninstalling application
here's what I've wrote:
Android Manifest
<receiver android:name=".DetectRemoved" >
<intent-filter android:priority="999999">
<action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Java Code
public class DetectRemoved 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("activity_log.pargansystem.com.activity_log")){
Toast.makeText(context, "your message", Toast.LENGTH_SHORT).show();
// start your activity here and ask the user for the password
}
}
}
}
}
Upvotes: 0
Views: 397
Reputation: 1857
How it Works
In manifest.xml
add permission:
<uses-permission android:name="android.permission.GET_TASKS"/>
and broadcast receiver:
<receiver android:name=".UninstallIntentReceiver">
<intent-filter android:priority="0">
<action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
<data android:scheme="package" />
</intent-filter>
UninstallIntentReceiver.java (broadcast receiver class)
public class UninstallIntentReceiver 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("YOUR_APPLICATION_PACKAGE_NAME")){
// User has selected our application under the Manage Apps settings
// now initiating background thread to watch for activity
new ListenActivities(context).start();
}
}
}
}
}
ListenActivities class - for monitoring the foreground activities
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){
// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(MAX_PRIORITY);
String activityName = taskInfo.get(0).topActivity.getClassName();
Log.d("topActivity", "CURRENT Activity ::"
+ activityName);
if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
// User has clicked on the Uninstall button under the Manage Apps settings
//do whatever pre-uninstallation task you want to perform here
// show dialogue or start another activity or database operations etc..etc..
// context.startActivity(new Intent(context, MyPreUninstallationMsgActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
exit = true;
Toast.makeText(context, "Done with preuninstallation tasks... Exiting Now", Toast.LENGTH_SHORT).show();
} else if(activityName.equals("com.android.settings.ManageApplications")) {
// back button was pressed and the user has been taken back to Manage Applications window
// we should close the activity monitoring now
exit=true;
}
}
Looper.loop();
}
}
This is the code as i found in some link Hope this helpful to you.
Upvotes: 1
Reputation: 75629
I don't want the user to uninstall the app without entering a password
This is not possible. Your app will not be notified nor asked for permission for doing so.
Upvotes: 1