显示来自警报管理器的通知
Display Notification from Alarm Manager
我有一个警报管理器,它将当前时间放入一条消息中,并在 Toast 消息中输出该消息。
我想做的是输出通知而不是吐司消息,一直在努力做到这一点。
警报管理器正常工作。 Notification 当前也可以在按下按钮时工作(当前未连接到警报管理器)。
这是我的按钮,它从 Broadcast Receiver 获取消息:
public void onetimeTimer(View view){
Context context = this.getApplicationContext();
if(alarm != null){
alarm.setOnetimeTimer(context);
}else{
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}
这是我的警报管理器广播接收器:
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
//Acquire the lock
wl.acquire();
//You can do the processing here update the widget/remote views.
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();
if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){
msgStr.append("One time Timer : ");
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
// msgStr.append(formatter.format(new Date()));
//msgStr.append();
Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
//Release the lock
wl.release();
}
这是广播接收器中的方法:
public void setOnetimeTimer(Context context){
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
这是在另一个按钮后面单独工作的通知,我想通过按下警报管理器按钮将其关闭。
Button btnNotifyWorkout = (Button)findViewById(R.id.btnNotifyWorkout);
btnNotifyWorkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(HomeActivity.this, 0, intent, 0);
Notification noti = new Notification.Builder(HomeActivity.this)
.setTicker("TickerTitle")
.setContentTitle("Lukes App")
.setContentText("You have a workout due today")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, noti);
}
});
尝试将您的通知代码放入警报管理器接收器"onReceive"中
但有一些调整:
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification.Builder(context)
.setTicker("TickerTitle")
.setContentTitle("Lukes App")
.setContentText("You have a workout due today")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);
我有一个警报管理器,它将当前时间放入一条消息中,并在 Toast 消息中输出该消息。
我想做的是输出通知而不是吐司消息,一直在努力做到这一点。
警报管理器正常工作。 Notification 当前也可以在按下按钮时工作(当前未连接到警报管理器)。
这是我的按钮,它从 Broadcast Receiver 获取消息:
public void onetimeTimer(View view){
Context context = this.getApplicationContext();
if(alarm != null){
alarm.setOnetimeTimer(context);
}else{
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}
这是我的警报管理器广播接收器:
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
//Acquire the lock
wl.acquire();
//You can do the processing here update the widget/remote views.
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();
if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){
msgStr.append("One time Timer : ");
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
// msgStr.append(formatter.format(new Date()));
//msgStr.append();
Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
//Release the lock
wl.release();
}
这是广播接收器中的方法:
public void setOnetimeTimer(Context context){
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
这是在另一个按钮后面单独工作的通知,我想通过按下警报管理器按钮将其关闭。
Button btnNotifyWorkout = (Button)findViewById(R.id.btnNotifyWorkout);
btnNotifyWorkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(HomeActivity.this, 0, intent, 0);
Notification noti = new Notification.Builder(HomeActivity.this)
.setTicker("TickerTitle")
.setContentTitle("Lukes App")
.setContentText("You have a workout due today")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, noti);
}
});
尝试将您的通知代码放入警报管理器接收器"onReceive"中
但有一些调整:
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification.Builder(context)
.setTicker("TickerTitle")
.setContentTitle("Lukes App")
.setContentText("You have a workout due today")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);