#android #android-intent #android-notifications #android-pendingintent #android-alarms
#Android #android-намерение #android-уведомления #android-pendingintent #android-сигналы тревоги
Вопрос:
я сделал будильник в Android. Я хочу, когда придет время будильника, программно открыть макет без уведомления о щелчке.
Вот коды;
это моя функция fireAlarm
public void fireAlarm() {
c = Calendar.getInstance();
seconds = c.get(Calendar.SECOND)*1000;
minutes = c.get(Calendar.MINUTE)*60*1000;
hours = c.get(Calendar.HOUR)*3600*1000;
ct = seconds minutes hours;
sure = (tp.getCurrentMinute()*60*1000) (tp.getCurrentHour()*3600*1000);
finalarm = sure - ct;
Toast.makeText(MainActivity.this, "Alarm " finalarm/1000 " Saniye sonra çalacak", Toast.LENGTH_LONG).show();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.set( AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() finalarm , pendingIntent );
}
и это коды уведомлений
private void generateNotification(Context context, String message) {
System.out.println(message " 2");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
String subTitle = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("content", message);
PendingIntent intent = PendingIntent.getActivity(context, 111,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, subTitle, intent);
//To play the default sound with your notification:
notification.sound = Uri.parse(MainActivity.path);
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
должен ли я использовать другой сервис или что-то в этом роде? Спасибо.
Ответ №1:
В случае, если вы используете широковещательный приемник, ваш переопределенный метод «onReceive» должен иметь контекст, полученный в качестве аргумента, который является контекстом действия, которое отправляет сигнал тревоги, в этом случае просто:
notificationIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(notificationintent);
где-то в вашем методе generateNotification.
Если вы не используете широковещательный приемник, это может быть лучшим подходом.
Или просто поместите этот код в конец вашего метода generateNotification, это может действительно сработать, просто я не тестировал сам.
Комментарии:
1. после этого должно быть намерение. В logcat указано FLAG_ACTIVITY_NEW_TASK. и это работает, большое спасибо!
2. Благодаря вам я сначала не добавлял FLAG_ACTIVITY_NEW_TASK, подумал, что это проще, я отредактировал свой ответ, чтобы добавить ваше исследование для решения