5

Possible Duplicates:
how to create startup application in android?
How to Autostart an Android Application?

Hi,

I am trying in one of my application when i am going to start i mean power on my google android g1 my application automatically will start but i am unable to understand how can i do that please help......

Community
  • 1
  • 1
  • IIRC, android apps are either 'screen' or 'service'. What you want to do is install a service on your phone. Search down that path. – KevinDTimm Jun 04 '10 at 12:57
  • It is also a duplicate of http://stackoverflow.com/questions/1056570/how-to-autostart-an-android-application – CommonsWare Jun 04 '10 at 13:59

1 Answers1

10

Here's a basic example of what I think you're trying to do. You'll need to do a few things. First, grant your application permission to listen for a "Boot completed" signal. In your AndroidManifest.xml, add this line in the manifest section:

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

Under your application section, add and specify a broadcast receiver for this intent:

<receiver android:name=".MyBroadcastReceiver">
    <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

You also need a broadcast receiver which will respond to this intent and start your service at boot. Create MyBroadcastReceiver.java in your project:

package com.mypackage.myapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context aContext, Intent aIntent) {

        // This is where you start your service
        aContext.startService(new Intent(aContext, MyService.class));
    }
}
Peter Warrington
  • 654
  • 10
  • 32
Scott Ferguson
  • 869
  • 1
  • 9
  • 17
  • : The method startService(Intent) is undefined for the type MyBroadcastReceiver , it also underlines MyService. – eawedat Oct 30 '13 at 12:08