我想做的是,通知用户有关即将完成的任务的通知,该任务已添加到待办事项列表EVEN而无需打开待办事项应用程序。我顺便使用了BroadcastReceiver,因为我已经阅读了android检查它们,看看是否有任何消息已被他们收到。 我知道有些东西不见了因为我已经把所有东西都放在了mainactivity.java中,所以只需打开应用即可。
这是我的BroadcastReceiver文件 public class Broadcast扩展BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Toast.makeText(context, "received an alarm", Toast.LENGTH_SHORT).show();
}
}
这是我的mainactivity.java
公共类MainActivity扩展了Activity {
public NotificationCompat.Builder builder;
public NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// NOTIFICATION
AlarmManager alarmMang = (AlarmManager) getSystemService(ALARM_SERVICE);
// ---get current date and time---
Calendar calender = Calendar.getInstance();
calender.add(Calendar.MINUTE, 5);
Intent noteIntent = new Intent(this, Broadcast.class);
PendingIntent pendI = PendingIntent.getActivity(this, 0, noteIntent, 0);
long fireTime = System.currentTimeMillis();
builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// // BUTTON NOT
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
notificationManager.notify(0, builder.build());
}
});
// // AET ALARM
Toast.makeText(this, "lilili", Toast.LENGTH_SHORT).show();
alarmMang.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(),
pendI);
notificationManager.notify(2332323, builder.build());
// finish();
}
答案 0 :(得分:0)
注意:显示基本通知相对简单,此示例代码可以帮助您了解其工作原理
// Notification.Builder is used to create the notification
Notification.Builder builder = new Notification.Builder(this);
// This set the title of the notification
builder.setContentTitle("Title")
// This set the content of the notification (the text inside the notification)
.setContentText("Content of the notification")
// Here you set the icon of the notification
.setSmallIcon(R.drawable.ic_action_copy);
// Build the notification and save it
Notification notification = builder.build();
// To show the notification you need to use the NotificationManager, to get it you should use getSystemService
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// .. then you can use .notify to show the notification
// 0 is the ID of the notification, you can use this ID to update the notification later.
// notification is the notification to show (you can use builder.build() inside too.)
manager.notify(0, notification);
但是如果您需要有关通知的更多信息,应该阅读this,这是一个非常好的教程,并且还可以解释图像的所有内容。