我正在使用Android Studio中的Java开发简单的提醒。 我使用Alarm Manager和Broadcast Receiver安排闹钟。当我运行我的程序并安排警报时,不会出现任何警报。但它没有显示任何错误。
MainActivity.java
final Intent intentAlarm = new Intent(this, Doritos.class);
EditText cas = (EditText) findViewById(R.id.editText);
EditText datum = (EditText) findViewById(R.id.editText2);
EditText text = (EditText) findViewById(R.id.editText5);
String Datum[] = datum.getText().toString().split(":");
String Cas[] = cas.getText().toString().split(":");
Calendar cal = new GregorianCalendar(Integer.parseInt(Datum[2]),Integer.parseInt(Datum[1]) - 1,Integer.parseInt(Datum[0]),Integer.parseInt(Cas[0]),Integer.parseInt(Cas[1]),Integer.parseInt(Cas[2]));
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
intentAlarm.putExtra("text", text.getText());
alarmManager.set(alarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, pocet, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Brodcast receiver.java(Doritos.java)
public class Doritos extends BroadcastReceiver{
private MainActivity sss = new MainActivity();
@Override
public void onReceive(Context context, Intent intent) {
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_error_outline_black_24dp);
mBuilder.setContentTitle("Nezabudnicek");
mBuilder.setContentText(intent.getExtras().getCharSequence("text"));
Intent resultIntent = new Intent(context,MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(sss.pocet,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(sss.pocet, mBuilder.build());
}
}
答案 0 :(得分:1)
看看Manifest。你可能忘了在那里添加广播接收器。 希望它有所帮助