我必须通过 AlramManager 调用我的 BroadcastReceiver。
为此,我所做的不起作用:
报警活动.java
public class AlarmActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 50);
calendar.set(Calendar.SECOND, 0);
**Edit : Solution**
//Intent intent = new Intent(AlarmActivity.this, MyService.class);
//PendingIntent pendingIntent = PendingIntent.getService(AlarmActivity.this, 0, intent, 0);
//alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, c.getTimeInMillis(), pendingIntent);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(AlarmActivity.this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
我的服务.java
public class MyService extends BroadcastReceiver {
@SuppressLint("SimpleDateFormat")
@Override
public void onReceive(Context arg0, Intent arg1) {
Log.e("Broadcast", "Done");
}
}
Also give alarm permission and receiver
这是我使用但不工作的。你能说出我错过了什么吗?