在Android中如何显示通知方式更改?

4

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

但是通知没有弹出。

1
只需删除视图参数并从任何需要的地方调用createNotification();。您可以将消息作为字符串参数添加。 - rahul
我尝试了那种方式,但是出现了一个错误,Rahul。 - ShanWave007
你尝试了什么??发一下。 - Anup Dasari
你现在可能会遇到与按钮点击相关的错误。因为我们已经删除了视图参数,所以你可能会得到类似nosuchmethod异常的异常信息。 - rahul
但是您是否遇到了异常或者没有异常,但通知未能显示? - rahul
我遇到了“没有这样的方法异常”。我正尝试从另一个方法中调用上述创建通知方法。 - ShanWave007
2个回答

1

Try this :

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

}

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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接