抱歉愚蠢的问题......我尝试在AlarmManager类上集成一个通知栏(相同的代码在新活动中完美运行)。
Context context = getApplicationContext(); and
Intent notificationIntent = new Intent(this, AlarmManager.class);
这不正确吗?
public class AlarmReceiver extends BroadcastReceiver {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = android.R.drawable.stat_notify_chat;
CharSequence tickerText = "tickerText";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(this, AlarmManager.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, notification);
答案 0 :(得分:1)
您需要调用context.getSystemService(ns)
,其中context是传递给onCreate的变量。