Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. Intent service = new Intent(getApplicationContext(), MyService.class);
  2. getApplicationContext().startService(service);
  3.  
  4. public class MyService extends Service {
  5.  
  6. @Override
  7. public int onStartCommand(Intent intent, int flags, int startId) {
  8. // TODO do something useful
  9. HFLAG = true;
  10. //smsHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 1000);
  11. return Service.START_NOT_STICKY;
  12. }
  13.  
  14. @Override
  15. public IBinder onBind(Intent intent) {
  16. // TODO for communication return IBinder implementation
  17. return null;
  18. }
  19. }
  20.  
  21. <service
  22. android:name=".MyService"
  23. android:icon="@drawable/ic_launcher"
  24. android:label="@string/app_name" >
  25. </service>
  26.  
  27. public int onStartCommand(Intent intent, int flags, int startId) {
  28. return START_STICKY;
  29. }
  30.  
  31. Intent intent = new Intent(this, PowerMeterService.class);
  32. startService(intent);
  33.  
  34. public class MyBinder extends Binder {
  35. public MyService getService() {
  36. return MyService.this;
  37. }
  38. }
  39.  
  40. private ServiceConnection m_serviceConnection = new ServiceConnection() {
  41. public void onServiceConnected(ComponentName className, IBinder service) {
  42. m_service = ((MyService.MyBinder)service).getService();
  43. }
  44.  
  45. public void onServiceDisconnected(ComponentName className) {
  46. m_service = null;
  47. }
  48. };
  49.  
  50. Intent intent = new Intent(this, MyService.class);
  51. bindService(intent, m_serviceConnection, BIND_AUTO_CREATE);
  52.  
  53. private void addNotification() {
  54. // create the notification
  55. Notification.Builder m_notificationBuilder = new Notification.Builder(this)
  56. .setContentTitle(getText(R.string.service_name))
  57. .setContentText(getResources().getText(R.string.service_status_monitor))
  58. .setSmallIcon(R.drawable.notification_small_icon);
  59.  
  60. // create the pending intent and add to the notification
  61. Intent intent = new Intent(this, MyService.class);
  62. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  63. m_notificationBuilder.setContentIntent(pendingIntent);
  64.  
  65. // send the notification
  66. m_notificationManager.notify(NOTIFICATION_ID, m_notificationBuilder.build());
  67. }
  68.  
  69. android:launchMode="singleTop"
  70.  
  71. startForeground(NOTIFICATION_ID, m_notificationBuilder.build());
  72.  
  73. <service
  74. android:name="WordService"
  75. android:process=":my_process"
  76. android:icon="@drawable/icon"
  77. android:label="@string/service_name"
  78. >
  79. </service>
  80.  
  81. public int onStartCommand(Intent intent, int flags, int startId) {
  82. return START_STICKY;
  83. }
  84.  
  85. android:launchMode="singleTop"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement