Trying to start a service on boot on Android

后端 未结 16 1503
慢半拍i
慢半拍i 2020-11-21 06:41

I\'ve been trying to start a service when a device boots up on android, but I cannot get it to work. I\'ve looked at a number of links online but none of the code works. Am

相关标签:
16条回答
  • 2020-11-21 07:18

    I think your manifest needs to add:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    0 讨论(0)
  • 2020-11-21 07:20

    The other answers look good, but I thought I'd wrap everything up into one complete answer.

    You need the following in your AndroidManifest.xml file:

    1. In your <manifest> element:

      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
      
    2. In your <application> element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):

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

      (you don't need the android:enabled, exported, etc., attributes: the Android defaults are correct)

      In MyBroadcastReceiver.java:

      package com.example;
      
      public class MyBroadcastReceiver extends BroadcastReceiver {
          @Override
          public void onReceive(Context context, Intent intent) {
              Intent startServiceIntent = new Intent(context, MyService.class);
              context.startService(startServiceIntent);
          }
      }
      

    From the original question:

    • it's not clear if the <receiver> element was in the <application> element
    • it's not clear if the correct fully-qualified (or relative) class name for the BroadcastReceiver was specified
    • there was a typo in the <intent-filter>
    0 讨论(0)
  • 2020-11-21 07:21

    Along with

    <action android:name="android.intent.action.BOOT_COMPLETED" />  
    

    also use,

    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    

    HTC devices dont seem to catch BOOT_COMPLETED

    0 讨论(0)
  • 2020-11-21 07:24

    Before mounting external storage BOOT_COMPLETE is sent execute.if your app is installed to external storage it won't receive BOOT_COMPLETE broadcast message. To prevent this you can install your application in internal storage. you can do this just adding this line in menifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="internalOnly"
    ... >
    

    Some HTC devices can enable a "fast boot" feature that is more like a deep hibernation and not a real reboot and therefore should not give the BOOT_COMPLETE intent. To recover this you can add this intent filter inside your receiver:

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