In actual development, you often need to monitor whether the application is uninstalled or similar. I haven't seen the actual practice for a long time on the Internet. I can give a idea at most. I can capture system logs to check whether the application is uninstalled and then perform related operations.
By listening to the Intent. ACTION_PACKAGE_REMOVED Intent, you can only monitor whether other applications are uninstalled and cannot monitor yourself!
In this example, the system logs are monitored to determine whether the application is uninstalled.
LogcatObserver. java
public interface LogcatObserver { public void handleLog(String info); } public interface LogcatObserver { public void handleLog(String info);}LogcatScannerService.java
import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class LogcatScannerService extends Service implements LogcatObserver { @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); new AndroidLogcatScannerThread(this).start(); } @Override public void handleLog(String info) { if (info.contains("android.intent.action.DELETE") && info.contains(getPackageName())) { //do something yourself } } private class AndroidLogcatScannerThread extends Thread { private LogcatObserver mObserver; public AndroidLogcatScannerThread(LogcatObserver observer) { mObserver = observer; } @Override public void run() { String[] cmds = { "logcat", "-c" }; String shellCmd = "logcat"; Process process = null; InputStream is = null; DataInputStream dis = null; String line = ""; Runtime runtime = Runtime.getRuntime(); try { mObserver.handleLog(line); int waitValue; waitValue = runtime.exec(cmds).waitFor(); mObserver.handleLog("waitValue=" + waitValue + "\n Has do Clear logcat cache."); process = runtime.exec(shellCmd); is = process.getInputStream(); dis = new DataInputStream(is); while ((line = dis.readLine()) != null) { if (mObserver != null) mObserver.handleLog(line); } } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException ie) { } finally { try { if (dis != null) { dis.close(); } if (is != null) { is.close(); } if (process != null) { process.destroy(); } } catch (Exception e) { } } } } @Override public IBinder onBind(Intent intent) { return null; } } import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class LogcatScannerService extends Service implements LogcatObserver { @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); new AndroidLogcatScannerThread(this).start(); } @Override public void handleLog(String info) { if (info.contains("android.intent.action.DELETE") && info.contains(getPackageName())) { //do something yourself } } private class AndroidLogcatScannerThread extends Thread { private LogcatObserver mObserver; public AndroidLogcatScannerThread(LogcatObserver observer) { mObserver = observer; } @Override public void run() { String[] cmds = { "logcat", "-c" }; String shellCmd = "logcat"; Process process = null; InputStream is = null; DataInputStream dis = null; String line = ""; Runtime runtime = Runtime.getRuntime(); try { mObserver.handleLog(line); int waitValue; waitValue = runtime.exec(cmds).waitFor(); mObserver.handleLog("waitValue=" + waitValue + "\n Has do Clear logcat cache."); process = runtime.exec(shellCmd); is = process.getInputStream(); dis = new DataInputStream(is); while ((line = dis.readLine()) != null) { if (mObserver != null) mObserver.handleLog(line); } } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException ie) { } finally { try { if (dis != null) { dis.close(); } if (is != null) { is.close(); } if (process != null) { process.destroy(); } } catch (Exception e) { } } } } @Override public IBinder onBind(Intent intent) { return null; }}
You only need to call startService (this, LogcatScannerService. class.
The example itself is tested and determined to be able to listen to the action of the application being uninstalled. However, in the handleLog () method, I can only perform some time-saving operations. In the butler main test, I can send text messages, however, http sending cannot be completed!
The example itself monitors whether the application itself is uninstalled by constantly reading logs, so it is very good for electricity and performance! If the majority of users have better ideas, we can discuss them together!