Adn*_*hid 2 android android-alarms
如果这是一个菜鸟问题,请原谅。我已经尝试了所有可能的方法,每天从五个编辑文本中设置五个闹钟。但没有任何效果!我还有一个按钮(未在此代码中显示)更新这些编辑文本(因此也应该更新警报时间)。这是我的代码:
for (int i = 0; i < 5; i++) {
switch (i) {
case 0:
fajr.setText(result[i]);
tFajr = new GregorianCalendar();
tFajr.set(year, month, day,
Integer.parseInt(result[i].substring(0, 2)),
Integer.parseInt(result[i].substring(3, 5)));
break;
case 1:
zuhr.setText(result[i]);
tZuhr = new GregorianCalendar();
tZuhr.set(year, month, day,
Integer.parseInt(result[i].substring(0, 2)),
Integer.parseInt(result[i].substring(3, 5)));
break;
case 2:
asr.setText(result[i]);
tAsr = new GregorianCalendar();
tAsr.set(year, month, day,
Integer.parseInt(result[i].substring(0, 2)),
Integer.parseInt(result[i].substring(3, 5)));
break;
case 3:
maghrib.setText(result[i]);
tMaghrib = new GregorianCalendar();
tMaghrib.set(year, month, day,
Integer.parseInt(result[i].substring(0, 2)),
Integer.parseInt(result[i].substring(3, 5)));
break;
case 4:
isha.setText(result[i]);
tIsha = new GregorianCalendar();
tIsha.set(year, month, day,
Integer.parseInt(result[i].substring(0, 2)),
Integer.parseInt(result[i].substring(3, 5)));
break;
}
}
Run Code Online (Sandbox Code Playgroud)
PS:fajr,zuhr,asr,maghrib,isha 是五个 EditText。我尝试使用挂起的意图和警报管理器来触发警报,但没有奏效。有没有人有好的建议?
首先,您需要为每个警报声明一个待处理的意图。因此,如果您想要 5 个警报,则需要运行 5 次
PendingIntent sender = PendingIntent.getBroadcast(context,intent_code, intent, 0);
Run Code Online (Sandbox Code Playgroud)
并且 intent_code 也应该改变。每次注册一个新代码时,您都必须使用不同的代码。在我的应用程序中,每次执行时都会生成一个随机数。您还可以使用 Intent 将数据传递到此处的通知槽。注意 Intent 和 PendingIntent 之间的区别。
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("title", "some title");
intent.putExtra("notes","some notes");
Random r = new Random();
intent_code = r.nextInt();
PendingIntent sender = PendingIntent.getBroadcast(context,intent_code, intent, 0);
Run Code Online (Sandbox Code Playgroud)
之后,您需要注册您的闹钟。再次 5 次,每个要触发的警报对应一次。
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, Time_in_milis_from_now_till_your_alarm, sender);
Run Code Online (Sandbox Code Playgroud)
您需要一个 BroadcastReceiver 来接收您的警报并显示通知。我正在粘贴我的整个班级。这将在Time_in_milis_from_now_till_your_alarm 中设置的时间之后触发。在这里,您几乎可以随心所欲地运行任何东西。我不知道你想要什么样的警报,就我而言,我正在使用通知。您可以在此处和此处找到有关通知的详细信息。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("receiver", "received");
NotificationManager mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Bundle b = intent.getExtras();
int icon = R.drawable.icon; // icon from resources
CharSequence tickerText = b.getString("title"); ; // ticker-text
long when = System.currentTimeMillis(); // notification time
CharSequence contentText = b.getString("notes");; // message text
Toast.makeText(context, tickerText, Toast.LENGTH_SHORT).show();
Intent notificationIntent = new Intent(context, AppDelegate.class);
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0,
notificationIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
// the next two lines initialize the Notification, using the
// configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context,tickerText, contentText, contentIntent);
mManager.notify(12, notification);
}
}
Run Code Online (Sandbox Code Playgroud)
最后,不要忘记在清单中声明您的广播,否则它将无法工作。这进入应用程序标签。
如果您对警报管理器方法有任何问题,可以在此处找到文档。
祝一切顺利!
编辑
要播放声音,请使用开发人员指南推荐的 MediaPlayer。将文件 mysound.mp3 保存在您的文件夹 /res/raw 中。您只需在 BroadcastReceiver 中调用以下方法即可!
public void playSound() {
MediaPlayer sound = MediaPlayer.create(this, R.raw.mysound);
sound.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
quadrantChangeSound.start();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1601 次 |
| 最近记录: |