这里有两个问题。我尝试让用户选择特定时间让应用提醒用户使用应用。一旦用户设定时间说晚上8点。通知应该每天弹出。但是,在我使用
之后"NotificationManager" and "Notification"
首先,没有任何弹出窗口。例如,现在是AU的上午1:19,然后,我将此应用设置为凌晨1点20分,以便在状态栏中显示通知作为测试。
其次,如果在我的 TabBar 类中我使用
nm.cancel(getIntent().getExtras().getInt("NotifID"));
它将获得空指针异常
这是我的代码:
TabBar.class
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.cancel(getIntent().getExtras().getInt("NotifID"));
Notification.class
package com.example.tabpro;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
public class SettingNotification extends Activity{
TimePicker timePicker;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.setting_notification);
Button btnset = (Button) findViewById(R.id.btnSetAlarm);
btnset.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
timePicker = (TimePicker) findViewById(R.id.timePicker_settime);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
Intent i = new Intent("com.example.tabpro.DisplayNotification");
Intent i2 = new Intent(SettingNotification.this, TabBar.class);
i.putExtra("NotifID", 1);
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0, i, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), displayIntent);
startActivity(i2);
}
});
}
}
DisplayNotification.class
package com.example.tabpro;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class DisplayNotification extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
int notifID = getIntent().getExtras().getInt("NotifID");
Intent i = new Intent("com.example.tabpro.TabBar");
i.putExtra("NotifID", notifID);
PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher, "Time's up!", System.currentTimeMillis());
CharSequence from = "AlarmManager - Time's up!";
CharSequence message = "This is your alert, courtesy of the AlarmManager";
notif.setLatestEventInfo(this, from, message, detailsIntent);
finish();
}
}
答案 0 :(得分:0)
您需要在NotificationManager
上致电notify()
以实际显示通知。由于您从未调用此方法,因此永远不会显示您的通知。
您的NullPointerException可能是一个不同的问题。为此,你应该自己做一些调试。首先,您需要确定什么是null。然后确定它为null的原因并修复它。在您的代码中,导致TabBar
中的NPE的最可能候选者是nm
,getIntent()
和getExtras()
。
答案 1 :(得分:0)
希望这会有所帮助: 您需要调用notify()方法在设备的通知栏中显示通知。
以下是代码:
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context, YourActivity.class), 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(yourappname).setWhen(0)
.setAutoCancel(true).setContentTitle(yourappname)
.setContentText(yourmessage).build();
notificationManager.notify(0, notification);