我在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();
}
但是通知不会弹出。
答案 0 :(得分:1)
试试这个:
public void createNotification(View view) {
showNotification("hello");
}
public void showNotification(String msg)
{
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(msg).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);
}
答案 1 :(得分:0)
好的,据我所知,您不希望有一个以View
为参数的方法,我查看了Vogella处的代码,
我可以告诉你的是这个教程是为了学习而做的,所以作者希望你能够了解通知是如何以及何时制作的,所以他做了一个clickListner
事件,听取了提到的按钮点击在属性(android:onClick="createNotification")
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="createNotification"
android:text="Create Notification" >
</Button>
这就是为什么你的活动中有一个方法,要求你以View
作为参数来运行代码。
你可以通过让你的按钮引用彻底的代码来轻松改变这一点。像这样,你可以删除android:onClick="createNotification"
Button my_button = (Button)findViewById(R.id.button1);
my_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Now call any method which you want here or
makeNotification();
}
});
makeNotification(){
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);
}