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>"時,就啟動卸載提示頁面。

 

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

 

1.package lab.sodino.uninstallhint;

 

2.

 

3.import android.app.Activity;

 

4.import android.content.Intent;

 

5.import android.os.Bundle;

 

6.import android.os.Handler;

 

7.import android.os.Message;

 

8.import android.view.View;

 

9.import android.widget.Button;

 

10.import android.widget.TextView;

 

11.

 

12./**

 

13.* @author Sodino E-mail:sodinoopen@hotmail.com

 

14.* @version Time:2011-1-12 上午10:09:59

 

15.*/

 

16.public class MainActivity extends Activity implements LogcatObserver {

 

17. private TextView txtInfo;

 

18. private Handler handler;

 

19.

 

20. public void onCreate(Bundle savedInstanceState) {

 

21. super.onCreate(savedInstanceState);

 

22. setContentView(R.layout.main);

 

23. Button btnScannerLogcat = (Button) findViewById(R.id.btnScanLogcat);

 

24. btnScannerLogcat.setOnClickListener(new Button.OnClickListener() {

 

25. public void onClick(View view) {

 

26. // 開啟Logcat流監聽

 

27. LogcatScanner.startScanLogcatInfo(MainActivity.this);

 

28. }

 

29. });

 

30. Button btnUninstallMe = (Button) findViewById(R.id.btnUninstallMe);

 

31. btnUninstallMe.setOnClickListener(new Button.OnClickListener() {

 

32. public void onClick(View view) {

 

33. // 調用應用程式資訊

 

34. Intent intent = new Intent(Intent.ACTION_VIEW);

 

35. // com.android.settings/.InstalledAppDetails

 

36. intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");

 

37. intent.putExtra("pkg", "lab.sodino.uninstallhint");

 

38. startActivity(intent);

 

39. }

 

40. });

 

41. txtInfo = (TextView) findViewById(R.id.txtInfo);

 

42. handler = new Handler() {

 

43. public void handleMessage(Message msg) {

 

44. txtInfo.append(String.valueOf(msg.obj) + "\n");

 

45. }

 

46. };

 

47. }

 

48.

 

49. public void handleNewLine(String info) {

 

50. Message msg = new Message();

 

51. msg.obj = info;

 

52. handler.sendMessage(msg);

 

53. if (info.contains("android.intent.action.DELETE") && info.contains(getPackageName())) {

 

54. // 啟動刪除提示

 

55. Intent intent = new Intent();

 

56. intent.setClass(this, UninstallWarningActivity.class);

 

57. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

 

58. startActivity(intent);

 

59. }

 

60. }

 

61.}
複製代碼

 

1.package lab.sodino.uninstallhint;

 

2.

 

3.import java.io.DataInputStream;

 

4.import java.io.IOException;

 

5.import java.io.InputStream;

 

6.

 

7./**

 

8.*@author Sodino Email:sodinoopen@hotmail<br/>

 

9.*@version 2011-1-22 上午11:10:56

 

10.*/

 

11.public class LogcatScanner {

 

12. private static AndroidLogcatScanner scannerThead;

 

13.

 

14. public final static void startScanLogcatInfo(LogcatObserver observer) {

 

15. if (scannerThead == null) {

 

16. scannerThead = new AndroidLogcatScanner(observer);

 

17. scannerThead.start();

 

18. LogOut.out(LogcatScanner.class, "scannerThread.start()");

 

19. }

 

20. }

 

21.

 

22. static class AndroidLogcatScanner extends Thread {

 

23. private LogcatObserver observer;

 

24.

 

25. public AndroidLogcatScanner(LogcatObserver observer) {

 

26. this.observer = observer;

 

27. }

 

28.

 

29. public void run() {

 

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

 

31. String shellCmd = "logcat";

 

32. Process process = null;

 

33. InputStream is = null;
34.
DataInputStream dis = null;

35. String line = "";

36. Runtime runtime = Runtime.getRuntime();

37. try {

38. observer.handleNewLine(line);

39. int waitValue;

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

41. observer.handleNewLine("waitValue=" + waitValue + "\n Has do Clear logcat cache.");

42. process = runtime.exec(shellCmd);

43. is = process.getInputStream();

44. dis = new DataInputStream(is);

45. while ((line = dis.readLine()) != null) {

46. observer.handleNewLine(line);

47. }

48. } catch (InterruptedException e) {

49. e.printStackTrace();

50. } catch (IOException ie) {

51. ie.printStackTrace();

52. } finally {

53. try {

54. if (dis != null) {

55. dis.close();

56. }

57. if (is != null) {

58. is.close();

59. }

60. if (process != null) {

61. process.destroy();

62. }

63. } catch (Exception e) {

64. e.printStackTrace();

65. }

66. }

67. }

68. }

69.}
複製代碼


1.package lab.sodino.uninstallhint;

2.

3./**

4.* @author Sodino E-mail:sodinoopen@hotmail.com

5.* @version Time:2011-1-22 下午03:00:54

6.*/

7.public interface LogcatObserver {

8. public void handleNewLine(String line);

9.}
複製代碼


1.package lab.sodino.uninstallhint;

2.

3.import android.app.Activity;

4.import android.app.ActivityManager;

5.import android.content.Context;

6.import android.content.Intent;

7.import android.os.Bundle;

8.import android.view.View;

9.import android.widget.Button;

10.

11./**

12.* @author Sodino E-mail:sodinoopen@hotmail.com

13.* @version Time:2011-1-12 上午10:26:09

14.*/

15.public class UninstallWarningActivity extends Activity {

16. public void onCreate(Bundle savedInstanceState) {

17. super.onCreate(savedInstanceState);

18. setContentView(R.layout.uninstall_warning);

19. Button btnContinue = (Button) findViewById(R.id.btnContinue);

20. btnContinue.setOnClickListener(new Button.OnClickListener() {

21. public void onClick(View view) {

22. UninstallWarningActivity.this.finish();

23. }

24. });

25. Button btnCancel = (Button) findViewById(R.id.btnCancel);

26. btnCancel.setOnClickListener(new Button.OnClickListener() {

27. public void onClick(View view) {

28. UninstallWarningActivity.this.finish();

29. ActivityManager actMag = (ActivityManager) UninstallWarningActivity.this

30. .getSystemService(Context.ACTIVITY_SERVICE);

31. //殺掉系統的打包安裝程式。

32. if (android.os.Build.VERSION.SDK_INT < 8) {

33. actMag.restartPackage("com.android.packageinstaller");

34. } else {

35. actMag.killBackgroundProcesses("com.android.packageinstaller");

36. }

37. Intent i = new Intent(Intent.ACTION_MAIN);

38. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

39. i.addCategory(Intent.CATEGORY_HOME);

40. startActivity(i);

41. }

42. });

43. }

44.}

最後再添加上許可權:
1. <uses-permission android:name="android.permission.READ_LOGS" />

2. <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

3. <uses-permission android:name="android.permission.RESTART_PACKAGES"/>
 
 
來源:http://blog.csdn.net/sodino/article/details/6158659
arrow
arrow
    全站熱搜

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