Android -Starting Service at Boot Time

后端 未结 9 1664
一个人的身影
一个人的身影 2020-11-22 16:12

I need to start a service at boot time. I searched a lot. They are talking about Broadcastreceiver. As I am new to android development, I didn\'t get a clear picture about s

相关标签:
9条回答
  • 2020-11-22 16:20

    Most the solutions posted here are missing an important piece: doing it without a wake lock runs the risk of your Service getting killed before it is finished processing. Saw this solution in another thread, answering here as well.

    Since WakefulBroadcastReceiver is deprecated in api 26 it is recommended for API Levels below 26

    You need to obtain a wake lock . Luckily, the Support library gives us a class to do this:

    public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // This is the Intent to deliver to our service.
            Intent service = new Intent(context, SimpleWakefulService.class);
    
            // Start the service, keeping the device awake while it is launching.
            Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
            startWakefulService(context, service);
        }
    }
    

    then, in your Service, make sure to release the wake lock:

        @Override
        protected void onHandleIntent(Intent intent) {
            // At this point SimpleWakefulReceiver is still holding a wake lock
            // for us.  We can do whatever we need to here and then tell it that
            // it can release the wakelock.
    
    ...
            Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
            SimpleWakefulReceiver.completeWakefulIntent(intent);
        }
    

    Don't forget to add the WAKE_LOCK permission and register your receiver in the manifest:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    ...
    
    <service android:name=".SimpleWakefulReceiver">
        <intent-filter>
            <action android:name="com.example.SimpleWakefulReceiver"/>
        </intent-filter>
    </service>
    
    0 讨论(0)
  • 2020-11-22 16:24

    Your receiver:

    public class MyReceiver extends BroadcastReceiver {   
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
         Intent myIntent = new Intent(context, YourService.class);
         context.startService(myIntent);
    
        }
    }
    

    Your AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.broadcast.receiver.example"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    
            <activity android:name=".BR_Example"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
        <!-- Declaring broadcast receiver for BOOT_COMPLETED event. -->
            <receiver android:name=".MyReceiver" android:enabled="true" android:exported="false">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                </intent-filter>
            </receiver>
    
        </application>
    
        <!-- Adding the permission -->
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    </manifest>
    
    0 讨论(0)
  • 2020-11-22 16:26

    you should register for BOOT_COMPLETE as well as REBOOT

    <receiver android:name=".Services.BootComplete">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.REBOOT"/>
            </intent-filter>
        </receiver> 
    
    0 讨论(0)
  • 2020-11-22 16:37

    Create a BroadcastReceiver and register it to receive ACTION_BOOT_COMPLETED. You also need RECEIVE_BOOT_COMPLETED permission.

    Read: Listening For and Broadcasting Global Messages, and Setting Alarms

    0 讨论(0)
  • 2020-11-22 16:37

    Also register your created service in the Manifest and uses-permission as

    <application ...>
       <service android:name=".MyBroadcastReceiver">
            <intent-filter>
                <action android:name="com.example.MyBroadcastReciver"/>
            </intent-filter>
       </service>
    </application>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    

    and then in braod cast Reciever call your service

    public class MyBroadcastReceiver extends BroadcastReceiver 
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            Intent myIntent = new Intent(context, MyService.class);
            context.startService(myIntent);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 16:39

    To Restart service in Android O or more ie OS >28 Use this code KOTLIN VERSION 1) Add permission in manifest

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

    2) Create a Class and extend it with BroadcastReceiver

    import android.content.BroadcastReceiver
    import android.content.Context
    import android.content.Intent
    import android.os.Build
    import android.util.Log
    import androidx.core.content.ContextCompat
    
    
    
    class BootCompletedReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, arg1: Intent?) {
            Log.d("BootCompletedReceiver", "starting service...")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                ContextCompat.startForegroundService(context, Intent(context, YourServiceClass::class.java))
            } else {
                context.startService(Intent(context, YourServiceClass::class.java))
            }
        }
    }
    

    3) Declare in Manifest file like this under application tag

    <receiver android:name=".utils.BootCompletedReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>
    
    0 讨论(0)
提交回复
热议问题