我添加的后台服务运行得很好,但是当我的手机关机并重新开机时,我的接收器类无法获取android.intent.action.BOOT_COMPLETED。但是服务没有启动。请看下面的代码,帮助我!!!
我还在manifest文件中添加了权限。
public class ReceiverCall extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
static final String ACTION1 = "android.intent.action.QUICKBOOT_POWERON";
@Override
public void onReceive(Context context, Intent intent) {
Log.v("log_tag", "Action :: "+intent.getAction());
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
/* Setting the alarm here */
Intent alarmIntent = new Intent(context, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), + (1000 * 60 * 2), pendingIntent);
Log.v("log_tag", "REPEAT");
}
}
我还在manifest文件中添加了权限。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.employeemanager.app.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:theme="@style/AppTheme" >
<activity
android:name="***8"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="***.MyAlarmService"
android:enabled="true" >
</service>
<receiver android:name="***.ReceiverCall"
android:enabled="true" >
<intent-filter>
<action android:name="***" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.LAUNCHER" >
</category>
</intent-filter>
</receiver>
</application>