Java 在android中显示通知方法更改?
我在vogella网站上找到了这种方法。他们从main.xml文件中按钮的onclick属性调用此方法。有人知道,如何在不调用View的情况下将此方法更改为使用吗Java 在android中显示通知方法更改?,java,android,xml,Java,Android,Xml,我在vogella网站上找到了这种方法。他们从main.xml文件中按钮的onclick属性调用此方法。有人知道,如何在不调用View的情况下将此方法更改为使用吗 public void createNotification(View view) { Intent intent = new Intent(this, Home.class); PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.
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);
}
这是我得到这个方法的地方的链接
我试过这样做。这是我对上述方法的编辑版本
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();
}
试试看{
Double inc_val=Double.parseDouble(display_incamo.getText().toString());
Double exp_val=Double.parseDouble(display_expamo.getText().toString());
如果(inc_val尝试以下方法:
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);
}
好的,据我所知,您不希望有一个将View
作为参数的方法,我查看了
我可以告诉你的是,本教程是为学习而编写的,所以作者希望你了解通知是如何发出的以及何时发出的,所以他制作了一个clickListner
事件,该事件会侦听属性中提到的按钮单击(android:onClick=“createNotification”)
只需删除view参数并调用createNotification();从您需要的任何位置。您可以将消息作为字符串参数添加。我尝试了这种方法,但出现了错误rahul。您尝试了什么?Post可能是您现在收到了与按钮单击相关的错误。由于我们删除了view参数,您一定收到了类似nosuchmethod异常的异常。但是您是否收到了一些exc接收或无异常,但未显示通知?
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="createNotification"
android:text="Create Notification" >
</Button>
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);
}