7303_1295685624c09D.gif  

轉自:http://blog.csdn.net/sodino/article/details/6158659

Android上能不能實現卸載時提示呢,比如卸載某某軟體時,做個使用者調查卸載的原因。
我以前想著是的不行的,以前的想法是:
Windows上卸載時能實現此功能是因為有些程式的卸載是自己實現的,非系統操作。
但android上目前來說還不支援,系統卸載時,還沒發現有啥介面可以和目標卸載程式交互。

呵呵,今天鼓搗LogCat,發現還是可以的。
實現基礎是:
1.通過註冊許可權,能夠獲取LogCat的輸出流的輸出資訊。
2.進入系統的卸載介面時,"打包安裝程式(com.android.packageinstaller)"會輸出如下資訊

01-22 16:29:15.250: INFO/ActivityManager(147): Starting activity: Intent { act=android.intent.action.DELETE dat=package:lab.sodino.uninstallhint cmp=com.android.packageinstaller/.UninstallerActivity }

好了,有這句話就足夠了。截取輸出流資訊,當獲取字串中包含"android.intent.action.DELETE"和"<you_package>"時,就啟動卸載提示頁面。

話就這麼多了。接下來看效果圖,上代碼。





view plain01.package lab.sodino.uninstallhint;
02.import android.app.Activity;
03.import android.content.Intent;
04.import android.os.Bundle;
05.import android.os.Handler;
06.import android.os.Message;
07.import android.view.View;
08.import android.widget.Button;
09.import android.widget.TextView;
10./**
11. * @author Sodino E-mail:sodinoopen@hotmail.com
12. * @version Time:2011-1-12 上午10:09:59
13. */
14.public class MainActivity extends Activity implements LogcatObserver {
15. private TextView txtInfo;
16. private Handler handler;
17. public void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.main);
20. Button btnScannerLogcat = (Button) findViewById(R.id.btnScanLogcat);
21. btnScannerLogcat.setOnClickListener(new Button.OnClickListener() {
22. public void onClick(View view) {
23. // 開啟Logcat流監聽
24. LogcatScanner.startScanLogcatInfo(MainActivity.this);
25. }
26. });
27. Button btnUninstallMe = (Button) findViewById(R.id.btnUninstallMe);
28. btnUninstallMe.setOnClickListener(new Button.OnClickListener() {
29. public void onClick(View view) {
30. // 調用應用程式資訊
31. Intent intent = new Intent(Intent.ACTION_VIEW);
32. // com.android.settings/.InstalledAppDetails
33. intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
34. intent.putExtra("pkg", "lab.sodino.uninstallhint");
35. startActivity(intent);
36. }
37. });
38. txtInfo = (TextView) findViewById(R.id.txtInfo);
39. handler = new Handler() {
40. public void handleMessage(Message msg) {
41. txtInfo.append(String.valueOf(msg.obj) + "/n");
42. }
43. };
44. }
45. public void handleNewLine(String info) {
46. Message msg = new Message();
47. msg.obj = info;
48. handler.sendMessage(msg);
49. if (info.contains("android.intent.action.DELETE") && info.contains(getPackageName())) {
50. // 啟動刪除提示
51. Intent intent = new Intent();
52. intent.setClass(this, UninstallWarningActivity.class);
53. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
54. startActivity(intent);
55. }
56. }
57.}




view plain01.package lab.sodino.uninstallhint;
02.import java.io.DataInputStream;
03.import java.io.IOException;
04.import java.io.InputStream;
05./**
06. *@author Sodino Email:sodinoopen@hotmail<br/>
07. *@version 2011-1-22 上午11:10:56
08. */
09.public class LogcatScanner {
10. private static AndroidLogcatScanner scannerThead;
11. public final static void startScanLogcatInfo(LogcatObserver observer) {
12. if (scannerThead == null) {
13. scannerThead = new AndroidLogcatScanner(observer);
14. scannerThead.start();
15. LogOut.out(LogcatScanner.class, "scannerThread.start()");
16. }
17. }
18. static class AndroidLogcatScanner extends Thread {
19. private LogcatObserver observer;
20. public AndroidLogcatScanner(LogcatObserver observer) {
21. this.observer = observer;
22. }
23. public void run() {
24. String[] cmds = { "logcat", "-c" };
25. String shellCmd = "logcat";
26. Process process = null;
27. InputStream is = null;
28. DataInputStream dis = null;
29. String line = "";
30. Runtime runtime = Runtime.getRuntime();
31. try {
32. observer.handleNewLine(line);
33. int waitValue;
34. waitValue = runtime.exec(cmds).waitFor();
35. observer.handleNewLine("waitValue=" + waitValue + "/n Has do Clear logcat cache.");
36. process = runtime.exec(shellCmd);
37. is = process.getInputStream();
38. dis = new DataInputStream(is);
39. while ((line = dis.readLine()) != null) {
40. observer.handleNewLine(line);
41. }
42. } catch (InterruptedException e) {
43. e.printStackTrace();
44. } catch (IOException ie) {
45. ie.printStackTrace();
46. } finally {
47. try {
48. if (dis != null) {
49. dis.close();
50. }
51. if (is != null) {
52. is.close();
53. }
54. if (process != null) {
55. process.destroy();
56. }
57. } catch (Exception e) {
58. e.printStackTrace();
59. }
60. }
61. }
62. }
63.}




view plain01.package lab.sodino.uninstallhint;
02./**
03. * @author Sodino E-mail:sodinoopen@hotmail.com
04. * @version Time:2011-1-22 下午03:00:54
05. */
06.public interface LogcatObserver {
07. public void handleNewLine(String line);
08.}




view plain01.package lab.sodino.uninstallhint;
02.import android.app.Activity;
03.import android.app.ActivityManager;
04.import android.content.Context;
05.import android.content.Intent;
06.import android.os.Bundle;
07.import android.view.View;
08.import android.widget.Button;
09./**
10. * @author Sodino E-mail:sodinoopen@hotmail.com
11. * @version Time:2011-1-12 上午10:26:09
12. */
13.public class UninstallWarningActivity extends Activity {
14. public void onCreate(Bundle savedInstanceState) {
15. super.onCreate(savedInstanceState);
16. setContentView(R.layout.uninstall_warning);
17. Button btnContinue = (Button) findViewById(R.id.btnContinue);
18. btnContinue.setOnClickListener(new Button.OnClickListener() {
19. public void onClick(View view) {
20. UninstallWarningActivity.this.finish();
21. }
22. });
23. Button btnCancel = (Button) findViewById(R.id.btnCancel);
24. btnCancel.setOnClickListener(new Button.OnClickListener() {
25. public void onClick(View view) {
26. UninstallWarningActivity.this.finish();
27. ActivityManager actMag = (ActivityManager) UninstallWarningActivity.this
28. .getSystemService(Context.ACTIVITY_SERVICE);
29. //殺掉系統的打包安裝程式。
30. if (android.os.Build.VERSION.SDK_INT < 8) {
31. actMag.restartPackage("com.android.packageinstaller");
32. } else {
33. actMag.killBackgroundProcesses("com.android.packageinstaller");
34. }
35. Intent i = new Intent(Intent.ACTION_MAIN);
36. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
37. i.addCategory(Intent.CATEGORY_HOME);
38. startActivity(i);
39. }
40. });
41. }
42.}





最後在AndroidManifest.xml中添加上許可權。


view plain01.<uses-permission android:name="android.permission.READ_LOGS" />
02.<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
03.<uses-permission android:name="android.permission.RESTART_PACKAGES"/>
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()