3

I want to mantain a log in my android application , log will contain the Device Started (Bootup) and Device Stop Times. Any Idea how to do this ?

I have to start my application on Bootup , But how to determine that application is started on Bootup ?

I have searched but could not find a better solution.

Hammad Shahid
  • 2,216
  • 5
  • 32
  • 60
  • possible duplicate of [Android - Start service on boot](http://stackoverflow.com/questions/7690350/android-start-service-on-boot) – Lalit Poptani Jul 02 '13 at 05:22

2 Answers2

10

Use BroadCastReceiver to receive BOOT_COMPLETED broadcast. This broadcast is thrown in device startup

The receiver will be like

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

You will need to use the following persmission

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

now in code write a BroadCastReceiver class like

public class ReceiverName extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
          // do startup tasks or start your luncher activity
    }
}
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
stinepike
  • 54,068
  • 14
  • 92
  • 112
5

You can use BroadcastReceiver component for this purpose. Using this you can detect various events of your device like booting.

To Detect Booting process you need to give permission in AndroidManifest.xml as below,

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

Then you need to create a BrodacastReceiver which will handle this,

In the onReceive() method the corresponding BroadcastReceiver would then start the event,

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, WordService.class);
        context.startService(service);
    }
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143