4

I've read some tutorial on launch service on boot. What I've done is:

In manifest:

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

<receiver android:name="my.package.ServiceStartup" >
   <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />
   </intent-filter>
</receiver>

CODE:

public class ServiceStartup extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
           @Override
           public void run() {
               Intent dialogIntent = new Intent(getBaseContext(), MyActivity.class);
               dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               getApplication().startActivity(dialogIntent);
           }
        }, 10000);
    }
}

In this way, if i reboot my device and go to setting in active applications, my service is not launched. What can I do? Where I make error? thanks!!

G M Ramesh
  • 3,420
  • 9
  • 37
  • 53
Jayyrus
  • 12,961
  • 41
  • 132
  • 214

2 Answers2

5

You want to start activity or service. In case of service, you will have to call startService(). Like:

getApplication().startService(new Intent(this, MyService.class));

harshit
  • 3,788
  • 3
  • 31
  • 54
  • i want to start service "ServiceStartup" that after 10 seconds launch an activity.... – Jayyrus Sep 27 '12 at 06:38
  • I assume you are not doing much i during those 10 seconds. Not getting a user input atleast. You may want to call `Thread.sleep(10000)` and then start the activity. Or else in you onCreate() of activity you will have to wait for 10seconds and then start it there. – harshit Sep 27 '12 at 06:40
  • ok.. but i don't think the problem was there cause in my active application ( on settings ) i don't see my service running – Jayyrus Sep 27 '12 at 06:43
  • because you have not started it yet. You only started the activity. If you want more insight, please paste your actvity code, where you are starting the activity. – harshit Sep 27 '12 at 06:45
  • witch service i have to start in postDelayed? :) – Jayyrus Sep 27 '12 at 06:55
  • the service you want to start. To make sure, are you confused between a service and an activity? – harshit Sep 27 '12 at 07:04
  • i'm not confused.. i want to start activity from my boot service.. u say me to start a service in postdelayed not activity – Jayyrus Sep 27 '12 at 07:24
1

Did you run your app? Refer to this tutorial

If you application is installed on the SD card, then it is not available after the android.intent.action.BOOT_COMPLETED event. Register yourself in this case for the android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE event.


Also note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.

Ziem
  • 6,579
  • 8
  • 53
  • 86
jaredzhang
  • 1,158
  • 8
  • 12