必須照一些步驟來作, 步驟如下:
1.在 manifest加上 permission: android.permission.RECEIVE_BOOT_COMPLETED 2.<receiver android:name="com.example.MyBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> 3.加上BroadcastReciver package com.example; public class MyBroadcastreceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { Intent startServiceIntent = new Intent(context, MyService.class); context.startService(startServiceIntent); } }
PS. 如果你是HTC手機, 可能要加上 <action android:name="android.intent.action.QUICKBOOT_POWERON" />
Ref:
Trying to start a service on boot on Android
Boot Receiver not work
沒有留言:
張貼留言