我有简单的通知设置,应该在时间到达时显示状态栏上的通知(基本上是余数)。我这样设定时间:
Calendar cal = Calendar.getInstance();
cal.set(2016, 0, 30, 10, 56, 00);
alarmReceiver.setCredentials(NAME,PODCAST);
setAlarm(cal);
此处给出了setAlarm
方法:
private void setAlarm(Calendar target){
Toast.makeText(c, "Reminder set", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), req1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, target.getTimeInMillis(), pendingIntent);
}
这会调用AlarmReciever
类,即:
public class AlarmReceiver extends BroadcastReceiver {
static int notifyId=1;
String name="";
String disc="";
NotificationCompat.Builder notification;
PendingIntent pIntent;
NotificationManager manager;
Intent resultIntent;
TaskStackBuilder stackBuilder;
public void setCredentials(String n,String d){
name=n;
disc=d;
}
@Override
public void onReceive(Context context,Intent intent) {
//Toast.makeText(context,"Alarm has been set",Toast.LENGTH_SHORT).show();
Log.d("Midhun","onReceiver receiver...");
notification = new NotificationCompat.Builder(context);
//set title for NOTIFICATION
notification.setContentTitle(name);
//message in notification
notification.setContentText(disc);
//Alert shown when notification recieved
notification.setTicker("INVENTO 16");
notification.setSmallIcon(R.drawable.ic_notify);
stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
resultIntent = new Intent(context,MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
pIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pIntent);
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(10,notification.build());
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
}
问题是通知未显示。
我试过以下:
1.我打印了来自AlarmReceiver
类的语句,只是为了查看是否调用了该方法。消息在正确的时间显示在控制台中(我在行中设置的时间:{{1 }。) @ 2015年1月30日8:56。
2.调用cal.set(2016, 0, 30, 08, 56, 00);
时显示的Toast。
所以基本上问题是My AlarmReciever类。但是我无法弄清楚它是什么。
答案 0 :(得分:0)
嗯,当我删除语句stackBuilder.addParentStack(MainActivity.class);