【问题标题】:Show Notifications in Android Without Using Intent在不使用 Intent 的情况下在 Android 中显示通知
【发布时间】:2012-11-09 11:57:59
【问题描述】:

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

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

【问题讨论】:

    标签: android android-layout android-intent notifications


    【解决方案1】:

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

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

    您可以更改吐司的显示时长和持续时间。

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      相关资源
      最近更新 更多