-3

I am trying to automatically start an Android service when the device reboots. I have tried to accomplish this using Receive_Boot_Complete permission, BroadcastReceiver with Boot_Complete intent action with no success. I'm very well aware that after Android 3.0 apps are placed in a stopped state on reboot and therefore no receivers are able to run. However, there are several mobile security apps such as Lookout that are run services and processes on reboot. How are they able to accomplish this?

<!-- Listen for the device starting up -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <receiver android:name="com.tpss.beacon.BootCompleteReceiver">
            <intent-filter>
                      <action android:name="android.intent.action.BOOT_COMPLETED"/>
                      <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
    </receiver>     


  public class BootCompleteReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

    context.startService(new Intent(context, UpdateGeofenceService.class));  
   }
}   
4

1 回答 1

0

当系统启动时,它会发送一个广播 BOOT_COMPLETED 意图。因此,您必须创建一个 BroadcastReceiver 来捕捉该意图:

public class BReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, final Intent intent)
{
    Log.i(TAG, "onReveive BOOT_COMPLETED");

            // start your service
            context.startService(new Intent("your_service"));
    }
}

然后修改清单:

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

不要忘记添加权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
于 2014-05-28T15:25:37.163 回答