當前位置:首頁 » 安卓系統 » android監聽應用卸載

android監聽應用卸載

發布時間: 2024-03-08 22:58:12

㈠ android程序如何監聽到自己被卸載這個事件。

手段略曲折

首先給你的程序注冊讀取log許可權,

<uses-permissionandroid:name="android.permission.READ_LOGS"/>

然後在你的程序里開一個後台線程,不停的讀取log,當你的應用(包括其他任何應用)被卸載時,系統的ActivityManager會列印出一行log,大概是removing:你的包名。這個時機是在卸載界面點擊確定後的一瞬間觸發的,如下圖


但是不能保證用戶就會點確定真把你的卸載了。。所以自己權衡吧。

這是我在網上找到的唯一方法

代碼如下:

privatevoidListenLog(){

Threadt=newThread(newRunnable(){

publicvoidrun(){

//TODOAuto-generatedmethodstub

Log.v("Fuck","Startlisteninglog");

String[]cmds={"logcat","-c"};

StringshellCmd="logcat";

Processprocess=null;

InputStreamis=null;

DataInputStreamdis=null;

Stringline="";

Runtimeruntime=Runtime.getRuntime();

try{

intwaitValue;

waitValue=runtime.exec(cmds).waitFor();

process=runtime.exec(shellCmd);

is=process.getInputStream();

dis=newDataInputStream(is);

while((line=dis.readLine())!=null&&mKeepListenFlag){

if(!line.contains("Fuck")){

Log.v("Fuck",line);

//這里只是把每個log都列印了一遍,可以再此判斷line里是否有removing字樣,然後做些處理

}

}

Log.v("Fuck","finishedlisten");

}catch(InterruptedExceptione){

e.printStackTrace();

}catch(IOExceptionie){

ie.printStackTrace();

}finally{

try{

if(dis!=null){

dis.close();

}

if(is!=null){

is.close();

}

if(process!=null){

process.destroy();

}

}catch(Exceptione){

e.printStackTrace();

}

}

}

});

//mKeepListenFlag是個成員變數,是為了讓程序結束時終止線程的,否則可能產生程序多次啟動,然後這個線程就啟動了多個。Android線程可不會因為Activity的退出而終止。

mKeepListenFlag=true;

t.start();

}

㈡ Android:adb卸載系統應用軟技能

首先,通過adb卸載應用,需要Android系統允許adb調試,正常的手機通過多次點擊版本號,都可以進入開發者模式,在開發者模式允許adb調試。在某些Android機頂盒系統,系統是禁用adb調試的。

在Windows或者Mac系統下,打開命令行終端(命令提示符)。

這里以Android機頂盒和Windows系統為例,電腦和機頂盒連接在同個區域網,並知道機頂盒的IP地址。

注意: Android系統原生並不支持 adb root 。以及一些手機Android系統定製廠商並沒有提供支持,如果需要root,可能需要向手機廠商申請,例如某米手機。如果是自己公司定製,可以將root文件加到系統中,提供定製,方便應用層的夥伴調試。

上面的2、3、4步驟也可以換成下面步驟,原理是一致。

沒有root許可權下,卸載系統應用APK,需要獲取應用包名。

由於現在手機廠商和機頂盒定製較高,底層adb支持的力度都不一樣。上面的方式可能會失效。但值得一試。

覺得本文章對你有用就點個贊吧,鐵子

㈢ 如何完全手動地卸載掉Android的某app

清楚應用數據和卸載應用,直接執行命令:
* String cmd = "pm clear " + packageName; 或者 * String cmd = "pm clear " + packageName + " HERE";

* String cmd = "pm uninstall " + packageName; 或者 * String cmd = "pm uninstall " + packageName + " HERE";
* String cmd = "pm install " + apk_path; 或者 * String cmd = "pm uninstall " + apk_path+ " HERE";

且在清楚數據之後應用本身的Service和Alarm定時會自動取消掉,進程也隨之結束, 以此類推adb所有命令都可以這樣執行。

package com.app.exit.demo;

import java.io.IOException;

import android.util.Log;

/**
* 運行命令管理器
* 等同於在PC端adb shell之後執行命令,pm clear packageName
* 但是只能回自己操作,不能清除其他應用的數據;
* 卸載其他應用(限root設備或者廠商提供介面)
* @author hulk
*
*/
public class RuntimeCmdManager {

private static final String TAG = "RuntimeCmdManager";

/**
* 清除應用緩存的用戶數據,同時停止所有服務和Alarm定時task
* String cmd = "pm clear " + packageName;
* String cmd = "pm clear " + packageName + " HERE";
* Runtime.getRuntime().exec(cmd)
* @param packageName
* @return
*/
public static Process clearAppUserData(String packageName) {
Process p = execRuntimeProcess("pm clear " + packageName);
if (p == null) {
LogTag.log("Clear app data packageName:" + packageName
+ ", FAILED !");
} else {
LogTag.log("Clear app data packageName:" + packageName
+ ", SUCCESS !");
}
return p;
}

/**
* 卸載應用,只能對root設備有效
* String cmd = "pm uninstall " + packageName;
* Runtime.getRuntime().exec("pm uninstall " + packageName)
* @param packageName
* @return
*/
public static Process uninstallApp(String packageName) {
Process p = execRuntimeProcess("pm uninstall " + packageName);
if (p == null) {
LogTag.log("Uninstall app packageName:" + packageName + ", FAILED !");
} else {
LogTag.log("Uninstall app packageName:" + packageName + ", SUCCESS !");
}
return p;
}

public static Process execRuntimeProcess(String commond) {
Process p = null;
try {
p = Runtime.getRuntime().exec(commond);
} catch (IOException e) {
LogTag.log("exec Runtime commond:" + commond + ", IOException" + e);
e.printStackTrace();
}
LogTag.log("exec Runtime commond:" + commond + ", Process:" + p);
return p;
}
}

徹底退出應用,不清除數據的方法:
停止應用中所有Service。調用ActivityManager.killBackgroundProcesses(packageName);
示例:
package com.app.exit.demo;

import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

private static final String TAG = "MainActivity";
public static final int ALARM_INTERVAL_MILLIS = 10 * 1000;

TextView mMsgTv;
Button exitAppBtn;

List runningServices;
ActivityManager mActivityManager;
List packageInfos;
ServiceInfo[] serviceInfos;
AlarmManager mAlarmManager;
private PendingIntent operation;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMsgTv = (TextView) findViewById(R.id.msg_tv);
exitAppBtn = (Button) findViewById(R.id.btn_exit_app);
exitAppBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
LogTag.i("" + this);
mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mActivityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
startServices();
runningServices = mActivityManager.getRunningServices(10000);
// proccessRunningServices(false);
packageInfos = getPackageManager().getInstalledPackages(
PackageManager.GET_SERVICES);
processPackageInfos(false);
setRepeating();
}

/**
* 定時啟動SecondActivity,清除自身數據或者卸載
*/
private void setRepeating() {
if (operation == null) {
operation = getSecondPendingIntent();
}
long startTime = System.currentTimeMillis() + ALARM_INTERVAL_MILLIS;
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startTime,
ALARM_INTERVAL_MILLIS, operation);
}

private PendingIntent getSecondPendingIntent() {
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
PendingIntent operation = PendingIntent.getActivity(
getApplicationContext(), 0, intent, 0);
return operation;
}

private void startServices() {
startService(new Intent(this, MyService1.class));
startService(new Intent(this, MyService2.class));
}

private void processPackageInfos(boolean isStop) {
for (PackageInfo pinfo : packageInfos) {
if (getPackageName().equals(pinfo.packageName)) {
serviceInfos = pinfo.services;
for (ServiceInfo sinfo : serviceInfos) {
if (isStop) {
boolean result = stopPackageService(this,
getPackageName(), sinfo.name);
Log.w(TAG, "Stop sinfo.name: " + sinfo.name
+ ", result=" + result);
} else {
Log.d(TAG, "sinfo.name: " + sinfo.name);
}
}
}
}
}

private void proccessRunningServices(boolean isStop) {
if (runningServices == null) {
Log.w(TAG, "runningServices is null !! ");
return;
}

for (RunningServiceInfo runningServiceInfo : runningServices) {
String serviceName = runningServiceInfo.service.getClassName();
String process = runningServiceInfo.process;
if ("com.app.exit.demo".equals(process)) {
if (isStop) {
boolean result = stopPackageService(getApplication(),
getPackageName(), serviceName);
Log.w(TAG, "stopService " + serviceName + ", result= "
+ result);
} else {
Log.i(TAG, "runningServiceInfo.process=" + process
+ ",runningServiceInfo.pid="
+ runningServiceInfo.pid
+ ", runningServiceInfo.getClass=" + serviceName);
}
}
}
}

private boolean stopPackageService(Context context, String packageName,
String serviceName) {
Intent service = new Intent();
service.setClassName(packageName, serviceName);
return context.stopService(service);
}

@Override
protected void onDestroy() {
super.onDestroy();
killProcess();
LogTag.i("onDestroy: " + this);
}

private void killProcess() {
// proccessRunningServices(true);
processPackageInfos(true);
String packageName = getPackageName();
mActivityManager.killBackgroundProcesses(packageName);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

注意Manifest配置:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.app.exit.demo"
android:versionCode="1"
android:versionName="1.0" >

android:minSdkVersion="8"
android:targetSdkVersion="21" />

android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/app_name" >

android:name=".SecondActivity"
android:label="SecondActivity"
android:launchMode="singleTask" >

㈣ android程序怎樣監聽自身程序被安裝和卸載

這個我想到的就一種方法,讀取Log信息,然後判斷是否被安裝,其實系統安裝成功之後,會發廣播的,很詳細,但是這種廣播最好是第三方軟體堅定為好,自己就沒法監聽,我前一陣子做升級,就像用這個,但是沒用成,最後只是下載apk成功之後,發送給伺服器日誌信息,感覺自己監聽自己,功能實現上好像有些矛盾,個人意見..

㈤ android應用是如何監聽自己是否被卸載的呢

Activity啟動時fork出C端進程輪詢目錄:

1 package main.activity;
2
3 import pym.test.uninstalledmoniter.R;
4 import android.app.Activity;
5 import android.os.Bundle;
6 import android.util.Log;
7
8 /**
9 * @author pengyiming
10 * @note 監聽此應用是否被卸載,若被卸載則彈出卸載反饋
11 *
12 */
13
14 public class UninstalledMoniterActivity extends Activity
15 {
16 /* 數據段begin */
17 private static final String TAG = "UninstalledMoniterActivity";
18 /* 數據段end */
19
20 /* 函數段begin */
21 private native void init();
22 static
23 {
24 Log.d(TAG, "load libuninstalled_moniter");
25 System.loadLibrary("uninstalled_moniter");
26 }
27
28 @Override
29 public void onCreate(Bundle savedInstanceState)
30 {
31 super.onCreate(savedInstanceState);
32 Log.d(TAG, "onCreate");
33
34 setContentView(R.layout.uninstalled_moniter_layout);
35
36 init();
37 }
38 /* 函數段end */
39 }

核心——native方法頭文件:

1 /* 頭文件begin */
2 #include <jni.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <android/log.h>
6 #include <unistd.h>
7 /* 頭文件end */
8
9 /* 宏定義begin */
10 //清0宏
11 #define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize)
12
13 //LOG宏定義
14 #define LOG_INFO(tag, msg) __android_log_write(ANDROID_LOG_INFO, tag, msg)
15 #define LOG_DEBUG(tag, msg) __android_log_write(ANDROID_LOG_DEBUG, tag, msg)
16 #define LOG_WARN(tag, msg) __android_log_write(ANDROID_LOG_WARN, tag, msg)
17 #define LOG_ERROR(tag, msg) __android_log_write(ANDROID_LOG_ERROR, tag, msg)
18 /* 宏定義end */
19
20 #ifndef _Included_main_activity_UninstalledMoniterActivity
21 #define _Included_main_activity_UninstalledMoniterActivity
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 #undef main_activity_UninstalledMoniterActivity_MODE_PRIVATE
27 #define main_activity_UninstalledMoniterActivity_MODE_PRIVATE 0L
28 #undef main_activity_UninstalledMoniterActivity_MODE_WORLD_READABLE
29 #define main_activity_UninstalledMoniterActivity_MODE_WORLD_READABLE 1L
30 #undef main_activity_UninstalledMoniterActivity_MODE_WORLD_WRITEABLE
31 #define main_activity_UninstalledMoniterActivity_MODE_WORLD_WRITEABLE 2L
32 #undef main_activity_UninstalledMoniterActivity_MODE_APPEND
33 #define main_activity_UninstalledMoniterActivity_MODE_APPEND 32768L
34 #undef main_activity_UninstalledMoniterActivity_MODE_MULTI_PROCESS
35 #define main_activity_UninstalledMoniterActivity_MODE_MULTI_PROCESS 4L
36 #undef main_activity_UninstalledMoniterActivity_BIND_AUTO_CREATE
37 #define main_activity_UninstalledMoniterActivity_BIND_AUTO_CREATE 1L
38 #undef main_activity_UninstalledMoniterActivity_BIND_DEBUG_UNBIND
39 #define main_activity_UninstalledMoniterActivity_BIND_DEBUG_UNBIND 2L
40 #undef main_activity_UninstalledMoniterActivity_BIND_NOT_FOREGROUND
41 #define main_activity_UninstalledMoniterActivity_BIND_NOT_FOREGROUND 4L
42 #undef main_activity_UninstalledMoniterActivity_BIND_ABOVE_CLIENT
43 #define main_activity_UninstalledMoniterActivity_BIND_ABOVE_CLIENT 8L
44 #undef main_activity_UninstalledMoniterActivity_BIND_ALLOW_OOM_MANAGEMENT
45 #define main_activity_UninstalledMoniterActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
46 #undef main_activity_UninstalledMoniterActivity_BIND_WAIVE_PRIORITY
47 #define main_activity_UninstalledMoniterActivity_BIND_WAIVE_PRIORITY 32L
48 #undef main_activity_UninstalledMoniterActivity_BIND_IMPORTANT
49 #define main_activity_UninstalledMoniterActivity_BIND_IMPORTANT 64L
50 #undef main_activity_UninstalledMoniterActivity_BIND_ADJUST_WITH_ACTIVITY
51 #define main_activity_UninstalledMoniterActivity_BIND_ADJUST_WITH_ACTIVITY 128L
52 #undef main_activity_UninstalledMoniterActivity_CONTEXT_INCLUDE_CODE
53 #define main_activity_UninstalledMoniterActivity_CONTEXT_INCLUDE_CODE 1L
54 #undef main_activity_UninstalledMoniterActivity_CONTEXT_IGNORE_SECURITY
55 #define main_activity_UninstalledMoniterActivity_CONTEXT_IGNORE_SECURITY 2L
56 #undef main_activity_UninstalledMoniterActivity_CONTEXT_RESTRICTED
57 #define main_activity_UninstalledMoniterActivity_CONTEXT_RESTRICTED 4L
58 #undef main_activity_UninstalledMoniterActivity_RESULT_CANCELED
59 #define main_activity_UninstalledMoniterActivity_RESULT_CANCELED 0L
60 #undef main_activity_UninstalledMoniterActivity_RESULT_OK
61 #define main_activity_UninstalledMoniterActivity_RESULT_OK -1L
62 #undef main_activity_UninstalledMoniterActivity_RESULT_FIRST_USER
63 #define main_activity_UninstalledMoniterActivity_RESULT_FIRST_USER 1L
64 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DISABLE
65 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DISABLE 0L
66 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DIALER
67 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DIALER 1L
68 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SHORTCUT
69 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SHORTCUT 2L
70 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_LOCAL
71 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
72 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_GLOBAL
73 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
74
75 /*
76 * Class: main_activity_UninstalledMoniterActivity
77 * Method: init
78 * Signature: ()V
79 */
80 JNIEXPORT void JNICALL Java_main_activity_UninstalledMoniterActivity_init(JNIEnv *, jobject);
81
82 #ifdef __cplusplus
83 }
84 #endif
85 #endif

核心——native方法實現:

1 /* 頭文件begin */
2 #include "main_activity_UninstalledMoniterActivity.h"
3 /* 頭文件end */
4
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8
9 /* 內全局變數begin */
10 static char c_TAG[] = "UninstalledMoniterActivity.init";
11 static jboolean b_IS_COPY = JNI_TRUE;
12 /* 內全局變數 */
13
14 /*
15 * Class: main_activity_UninstalledMoniterActivity
16 * Method: init
17 * Signature: ()V
18 */
19 JNIEXPORT void JNICALL Java_main_activity_UninstalledMoniterActivity_init(JNIEnv *env, jobject obj)
20 {
21 jstring tag = (*env)->NewStringUTF(env, c_TAG);
22
23 //初始化log
24 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
25 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "init OK"), &b_IS_COPY));
26
27 //fork子進程,以執行輪詢任務
28 pid_t pid = fork();
29 if (pid < 0)
30 {
31 //出錯log
32 LOG_ERROR((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
33 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "fork error !!!"), &b_IS_COPY));
34 }
35 else if (pid == 0)
36 {
37 //子進程輪詢"/data/data/pym.test.uninstalledmoniter"目錄是否存在,若不存在則說明已被卸載
38 while (1)
39 {
40 FILE *p_file = fopen("/data/data/pym.test.uninstalledmoniter", "r");
41 if (p_file != NULL)
42 {
43 fclose(p_file);
44
45 //目錄存在log
46 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
47 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "I'm OK !!!"), &b_IS_COPY));
48
49 sleep(1);
50 }
51 else
52 {
53 //目錄不存在log
54 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
55 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "I'm NOT OK !!!"), &b_IS_COPY));
56
57 //執行命令am start -a android.intent.action.VIEW -d http://shouji.360.cn/web/uninstall/uninstall.html
58 execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://shouji.360.cn/web/uninstall/uninstall.html", (char *)NULL);
59 }
60 }
61 }
62 else
63 {
64 //父進程直接退出,使子進程被init進程領養,以避免子進程僵死
65 }
66 }
67
68 #ifdef __cplusplus
69 }
70 #endif

注1:為了調試方便,包含<android/log.h>,使得so在執行過程中也可以像Java端一樣方便得打出log。相應的mk文件需要加上以下兩句聲明
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog

熱點內容
預編譯查詢 發布:2024-11-28 05:32:35 瀏覽:312
山東青島iptv設置密碼是多少 發布:2024-11-28 05:30:26 瀏覽:315
小鵬的解壓 發布:2024-11-28 05:10:07 瀏覽:21
ibm存儲售後 發布:2024-11-28 05:00:39 瀏覽:32
python監控進程腳本 發布:2024-11-28 04:54:47 瀏覽:180
android載入html頁面 發布:2024-11-28 04:54:38 瀏覽:901
噻苯隆如何配置 發布:2024-11-28 04:53:56 瀏覽:395
普通電腦改伺服器風道風罩 發布:2024-11-28 04:52:28 瀏覽:454
什麼安卓手機像8p一樣 發布:2024-11-28 04:43:17 瀏覽:225
連接資料庫參數 發布:2024-11-28 04:43:15 瀏覽:808