How to determine Device startup event in android

前端 未结 2 1598
萌比男神i
萌比男神i 2021-01-22 14:15

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 appl

相关标签:
2条回答
  • 2021-01-22 14:50

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-22 15:11

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题