【发布时间】:2015-10-16 02:47:57
【问题描述】:
我无法设置针对某个目标的通知的警报。我可以在单击 SET 按钮时显示通知,但我希望根据用户设置(选择的日期、时间、重复、声音)显示该通知。
SetNotifyActivity 布局:
目标名称:(数据显示)
开始日期:(数据显示)
结束日期:(数据显示)
警报\通知设置:
时间选择器 [设置时间按钮]
REPEAT: [ToggleButton] 如果为 false,则启用 SetTimebutton 如果为真,则弹出alertDialog输入数字并选择类型:(按分钟、按小时、按天)
声音: [ToggleButton] 如果为 false,则振动;如果为真,则警报响起时会开启声音。
[设置按钮]
布局没有问题。
代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_notify);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
goalname = (TextView)findViewById(R.id.goal_name);
startdate = (TextView)findViewById(R.id.sdate);
enddate = (TextView)findViewById(R.id.edate);
showrepeat = (TextView)findViewById(R.id.showRepeat);
showTime = (TextView) findViewById(R.id.showtime);
setTime = (Button)findViewById(R.id.settime);
timePicker = (TimePicker)findViewById(R.id.timePicker);
setRepeat = (Button)findViewById(R.id.setRepetition);
goal_id = Integer.parseInt(extras.getString("goalid"));
//Create database
dbhandler = new MyDBAdapter(this);
displayDetails();
displayTime();
changeTimeButton();
selectRepeat();
//for notifications
// listener handler
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
switch (v.getId()) {
case R.id.setnotify:
showNotification();
done();
break;
//case R.id.btnCancelNotification:
//cancelNotification(0);
//break;
}
}
};
// we will set the listeners
findViewById(R.id.setnotify).setOnClickListener(handler);
//findViewById(R.id.btnCancelNotification).setOnClickListener(handler);
}
//for notifications
public void showNotification(){
List<Goals> oneGoal = dbhandler.getLatestGoal(goal_id);
for (final Goals goals : oneGoal) {
//to get the first incomplete activity in the goal
int actid = Integer.parseInt(dbhandler.getFirstIncompleteActivity(goal_id));
Activities oneAct = dbhandler.getActivity(actid);
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(SetNotifyActivity.this, ViewTasksActivity.class);
intent.putExtra("goalid", Integer.toString(oneAct.getGId()));
intent.putExtra("activityId", Integer.toString(oneAct.getActId()));
PendingIntent pIntent = PendingIntent.getActivity(SetNotifyActivity.this, 0, intent, 0);
// intent triggered, you can add other intent for other actions
Intent intent2 = new Intent(SetNotifyActivity.this, ViewActActivity.class);
intent2.putExtra("goalid", Integer.toString(oneAct.getGId()));
PendingIntent pIntent2 = PendingIntent.getActivity(SetNotifyActivity.this, 0, intent2, 0);
//String msg = "/n"+oneAct.getActivityName()+" is due on "+oneAct.getEndDate()+".Check the tasks to finish the activity.";
//assigned a unique id to notifications
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
String msg ="";
if(goals.getComplete().equalsIgnoreCase("true")){
msg = "Complete";
}else if(goals.getComplete().equalsIgnoreCase("false")){
msg = "Incomplete";
}
// this is it, we'll build the notification!
// in the addAction method, if you don't want any icon, just set the first param to 0
Notification mNotification = new Notification.Builder(this)
.setContentTitle(goals.getGoalName())
.setContentText(goals.getStartDate() + " - " +goals.getEndDate()+" ( "+goals.getPercent()+"%, "+msg+" )")
.setSmallIcon(R.drawable.gsoicon)
.setContentIntent(pIntent2)
.setSound(soundUri)
//.addAction(R.drawable.gsoicon, "View", pIntent)
//.addAction(0, "View All Activities", pIntent2)
//.addAction(0, "Check Tasks", pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// If you want to hide the notification after it was selected, do the code below
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(m, mNotification);
}
}
而且,如果我重新启动应用程序,所有这些都会消失。我必须单击设置按钮才能再次显示通知。
如何解决这个问题?
--而且,我不能在这个函数中使用示例代码或注释 -> notification.setLatestEventInfo() 因为它无法识别它。我不知道为什么。
【问题讨论】:
-
@MikeM。完毕。先生,您能帮我解决这个问题吗?
标签: android notifications