2

我是 android 的初学者。我想在我的应用程序中显示通知。我使用以下代码

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager notificationManager = (NotificationManager)  getSystemService(ns);
        int icon = R.drawable.icon;
        CharSequence tickerText = "";
        long when = System.currentTimeMillis();
        Notification notification  = new Notification(icon, tickerText, when);
        Context context = getApplicationContext();
        CharSequence contentTitle = "Hello";
        CharSequence contentText = "Welcome ";
        Intent notificationIntent = new Intent();
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, null, 0);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);       
        notificationManager.notify(HELLO_ID, notification); 

但我的问题是每当运行我的应用程序时,意图都会加载图标。然后图标会显示在状态栏中。如何在不使用意图的情况下显示通知?请帮忙

4

1 回答 1

0

我不确定这是否是你想要的,但如果你想显示一些快速通知,我会想到祝酒词:

    Context context = getApplicationContext();
    CharSequence text = "Notification!";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();

您可以更改 toast 显示的持续时间。

于 2012-12-20T02:32:07.507 回答