如何在android中使用NOTIFICATION_SERVICE

Kam*_*one 2 android

我希望在手机中收到WAP推送消息后显示通知.我知道我需要使用NOTIFICATION_SERVICE.但我不知道如何使用它.请帮我克服这个问题.我能够在手机中收到消息,并且正确地在日志中打印.写了一些代码来显示通知,但它不起作用.附上代码请看一下,并期待对此进行适当的指导.

private void showNotification(String text,Context con) {

    Notification n = new Notification();

    n.flags |= Notification.FLAG_SHOW_LIGHTS;
    n.flags |= Notification.FLAG_AUTO_CANCEL;

    n.defaults = Notification.DEFAULT_ALL;

    n.icon = R.drawable.ic_sms_wap;
    //n.when = System.currentTimeMillis();

    // Simply open the parent activity

    // Change the name of the notification here
    //n.setLatestEventInfo(this, NOTIF_TITLE, text, pi);

    mNotifMan.notify(NOTIF_CONNECTED, n);
}
Run Code Online (Sandbox Code Playgroud)

jkh*_*uw1 6

很难回答而不知道你的问题究竟是什么(即你得到错误等等)但你可以试试这个:

private void showNotification(String text,Context con) {
     if (mNotifMan==null) {
          mNotifMan=(NotificationManager) con.getSystemService(NOTIFICATION_SERVICE);
     }
    Notification n = new Notification(R.drawable.ic_sms_wap,text,System.currentTimeMillis());

    n.flags |= Notification.FLAG_SHOW_LIGHTS;
    n.flags |= Notification.FLAG_AUTO_CANCEL;

    n.defaults = Notification.DEFAULT_ALL;

   // n.icon = R.drawable.ic_sms_wap;
   // n.when = System.currentTimeMillis();

    // Simply open the parent activity

    // Change the name of the notification here
    //n.setLatestEventInfo(this, NOTIF_TITLE, text, pi);

    mNotifMan.notify(null,NOTIF_CONNECTED, n);
}
Run Code Online (Sandbox Code Playgroud)

  • 你在建造哪个sdk?NOTIFICATION_SERVICE应该是一个常量android.content.Context.NOTIFICATION_SERVICE,解析为"通知" (3认同)