ServiceTool.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package me.yoqi.mobile.tool;
  2. import android.annotation.TargetApi;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.app.Service;
  7. import android.content.Intent;
  8. import android.os.Build;
  9. import android.os.IBinder;
  10. import me.yoqi.mobile.safe.MainActivity;
  11. import me.yoqi.mobile.safe.R;
  12. public class ServiceTool extends Service {
  13. private NotificationManager mNF;
  14. public static String AUTO_START = "com.ldci.android.t56.mobile.safe.AUTO_START";
  15. @Override
  16. public IBinder onBind(Intent arg0) {
  17. return null;
  18. }
  19. @Override
  20. public void onCreate() {
  21. super.onCreate();
  22. }
  23. // public static int serviceInt = 0;
  24. @Override
  25. public void onStart(Intent intent, int startId) {
  26. super.onStart(intent, startId);
  27. // Toast.makeText(this, "开启服务,发出通知", Toast.LENGTH_LONG).show();
  28. // new Thread(new Runnable(){
  29. // public void run(){
  30. // while(true){
  31. // try {
  32. // Thread.sleep(1000);
  33. // serviceInt = 0;
  34. // } catch (InterruptedException e) {
  35. // e.printStackTrace();
  36. // }
  37. // }
  38. // }
  39. // }).start();
  40. autoStartNotification();
  41. }
  42. @Override
  43. public void onDestroy() {
  44. super.onDestroy();
  45. // Toast.makeText(this, "停止服务,取消通知", Toast.LENGTH_LONG).show();
  46. mNF.cancel(R.string.app_name);
  47. }
  48. @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  49. private void autoStartNotification() {
  50. Notification mNotification = new Notification(R.drawable.app_logo, "365手机秘书", System.currentTimeMillis());
  51. Intent intent = new Intent(this, MainActivity.class);
  52. intent.setAction(AUTO_START);
  53. intent.putExtra("auto_start", "boot_completed");
  54. PendingIntent mPI = PendingIntent.getActivity(this, 0, intent, 0);
  55. // mNotification.setLatestEventInfo(this, "365手机秘书", "成功开机启动", mPI);
  56. // Notification.Builder builder = new Notification.Builder(MainActivity.this);
  57. // builder.setAutoCancel(true);
  58. if (null == mNF) {
  59. mNF = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  60. }
  61. mNF.notify(R.string.app_name, mNotification);
  62. }
  63. }