我将自动启动编码为:
public class AutoStart extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
SetAlarm(context);
}
}
public void SetAlarm(Context context) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, ServiceUpdateData.class);
// PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
PendingIntent pi = PendingIntent.getService(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);//
int dbtime = 10;
int time = 1000 * 60 * dbtime;
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
time, pi); // Millisec * Second * Minute
Toast.makeText(context, "Service Started Again", Toast.LENGTH_LONG)
.show();
}
}
我在 androidmainifest.xml 中添加了
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
为什么我的服务在启动设备 android 后没有自动启动。你能帮助我吗?