我必须通过AlaramManager调用我的BroadcastReceiver。
对于我所做的,不起作用:
AlarmActivity.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);
}
}
MyService.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
这是我使用但不起作用的。你能告诉我错过了什么吗?
答案 0 :(得分:1)
使用此
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(Scheduling.this, Feedback.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Scheduling.this, 0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(), 86400000, pendingIntent);
在清单中添加接收器
答案 1 :(得分:0)
这样做,
public class MyService extends BroadcastReceiver {
private Context mContext;
@SuppressLint("SimpleDateFormat")
@Override
public void onReceive(Context arg0, Intent arg1) {
StringBuilder msgStr = new StringBuilder();
Format formatter = new SimpleDateFormat("hh:mm:ss a");
msgStr.append(formatter.format(new Date()));
Log.e("Broadcast", "Done");
mContext = arg0;
}
}
在getSystemservice()中编写此代码,
AlarmManager am = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);