Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. package com.example.service_m;
  2.  
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.os.IBinder;
  6. import android.widget.Toast;
  7.  
  8. public class ServiceCode extends Service{
  9.  
  10. @Override
  11. public IBinder onBind(Intent intent) {
  12. // TODO Auto-generated method stub
  13. return null;
  14. }
  15. @Override
  16. public int onStartCommand(Intent intent, int flags, int startId) {
  17. //TODO do something useful
  18.  
  19. for(int i=0;i<=10;i++){
  20. Toast.makeText(getApplicationContext(), "hello world",
  21. Toast.LENGTH_LONG).show();
  22. try {
  23. Thread.sleep(1000);
  24. } catch (InterruptedException e) {
  25. // TODO Auto-generated catch block
  26. e.printStackTrace();
  27. }
  28. }
  29.  
  30. return Service.START_NOT_STICKY;
  31. }
  32.  
  33. }
  34.  
  35. package com.example.service_m;
  36.  
  37. import android.content.BroadcastReceiver;
  38. import android.content.Context;
  39. import android.content.Intent;
  40.  
  41. public class ServiceStarter extends BroadcastReceiver{
  42.  
  43. @Override
  44. public void onReceive(Context _context, Intent _intent) {
  45.  
  46. Intent i = new Intent("com.example.service_m");
  47. i.setClass(_context, ServiceCode.class);
  48. _context.startService(i);
  49. }
  50.  
  51.  
  52. }
  53.  
  54. <application
  55. android:allowBackup="true"
  56. android:icon="@drawable/ic_launcher"
  57. android:label="@string/app_name"
  58. android:theme="@style/AppTheme" >
  59. <activity
  60. android:name="com.example.service_m.MainActivity"
  61. android:label="@string/app_name" >
  62. <intent-filter>
  63. <action android:name="android.intent.action.MAIN" />
  64. </intent-filter>
  65. </activity>
  66. <receiver android:name="com.example.service_m.ServiceStarter" >
  67. <intent-filter >
  68. <action android:name="android.intent.action.BOOT_COMPLETED" />
  69. </intent-filter>
  70. </receiver>
  71. </application>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement