0

Iam developing one android app, for that app, i need to start the my application at device start up.Can any one tell me how to do it? Thanks for any help.

joe
  • 221
  • 2
  • 5
  • 13

4 Answers4

1

you should first add a permission for that

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

and then define a receiver in your manifest also,

    <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

and then define the receiver in your code

public class BootUpReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
            Intent i = new Intent(context, MyActivity.class);  
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);  
    }

}

Onur A.
  • 3,007
  • 3
  • 22
  • 37
  • I really hope the OP doesn't want to start an activity on boot, that would be really annoying. – Simon Forsberg Jul 20 '13 at 12:18
  • for your context doesnt startActivity() respond http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent) and OP actually allows this :) – Onur A. Jul 20 '13 at 12:19
1

You need to follow steps such as

1) register a broadCast Receiver with action "android.intent.action.BOOT_COMPLETED"

2) then in the onReceive method of receiver you can start your activity/service or take any action as par your requirement. Here you already be in your application.

Dont forget to add respective permission in manifest file

Here is the same question/conversation

Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71
0

Use a Broadcast Receiver having android.intent.action.BOOT_COMPLETED as the intent-filter action, registered in the AndroidManifest then on the onReceive method, start your desired app component.

Frederick Nyawaya
  • 2,285
  • 1
  • 15
  • 19
0

You should register a BroadcastReceiver and then listen for ACTION_BOOT_COMPLETED event in that receiver.

public class yourBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context ctxt, Intent intent) {
        // TODO Auto-generated method stub
        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent selfIntent = new Intent(Intent.ACTION_MAIN);
            selfIntent.setClass(ctxt,  DestActivty.class);
            selfIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ctxt.startActivity(selfIntent);                         
        }
    }

}

You will need to modify your AndroidManifest file too and register this receiver in the Application node

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

And this too

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Rajeev
  • 1,374
  • 1
  • 11
  • 18
  • Thanks for the reply.But iam getting error message "the application has stopped unexpectedly" – joe Jul 20 '13 at 12:54
  • can you show some code? the activity which you are trying to spawn, your manifest file, and your logcat. – Rajeev Jul 20 '13 at 12:56
  • My application is installed on external memory.Is that the problem with the external storage? – joe Jul 22 '13 at 04:58
  • public class BootUpReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { /* do your stuff here, mostly just copy&paste from other places */ Intent i = new Intent(context, DownloadActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } – joe Jul 22 '13 at 05:26
  • looks like you have not used the `uses-permission` correctly. Remove this `android:permission="android.permission.RECEIVE_BOOT_COMPLETED` from inside of `receiver` node and put `` outside `application` node (put it before application node starts). I hope you have mentioned your activities properly in the manifest file – Rajeev Jul 22 '13 at 05:48
  • Thanks Rajeev.The problem is Solved.Thanks again. – joe Jul 22 '13 at 06:20