我在vogella网站上找到了这个方法。他们将此方法称为从main.xml文件中的一个按钮的onclick属性调用的方法。有人能告诉我,如何更改此方法以在不调用View的情况下使用?
public void createNotification(View view) {
Intent intent = new Intent(this, Home.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("title")
.setContentText("content").setSmallIcon(R.drawable.original_logo)
.setContentIntent(pIntent)
.addAction(R.drawable.original_logo, "Call", pIntent)
.addAction(R.drawable.original_logo, "More", pIntent)
.addAction(R.drawable.original_logo, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
这是我获取此方法的链接
http://www.vogella.com/tutorials/AndroidNotifications/article.html
我尝试了这种方式。以下是我编辑过的该方法版本。
public void createNotification(String title,String content) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, Home.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(content).setSmallIcon(R.drawable.original_logo)`enter code here`
.setContentIntent(pIntent)
.addAction(R.drawable.original_logo, "Call", pIntent)
.addAction(R.drawable.original_logo, "More", pIntent)
.addAction(R.drawable.original_logo, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
然后我尝试按照以下方式调用这个编辑过的方法。
try {
Double inc_val = Double.parseDouble(display_incamo.getText().toString());
Double exp_val = Double.parseDouble(display_expamo.getText().toString());
if(inc_val<exp_val){
createNotification("Expenses are High","Your expenses are almost higher than income");
}else{
createNotification("Expenses are Low","Keep it up Buddy!!!");
}
}catch(Exception e){
e.printStackTrace();
}
但是通知没有弹出。