gpt4 book ai didi

java - 在android中显示通知方法更改?

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:40 26 4
gpt4 key购买 nike

我在 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();
}

但是没有弹出通知。

最佳答案

试试这个:

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);

}

关于java - 在android中显示通知方法更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34528954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com