ServiceTool.java 1.8 KB

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