android状态栏通知图标

时间:2014-05-26 13:55:08

标签: android notifications statusbar

我正在尝试将状态栏设置为状态栏,单击按钮后我无法立即查看。 问题是删除选项。我想设置,只要那些进入应用程序并删除

的人就不会被删除
 public void setNotificationToStatusBar(){
      Intent intent= new Intent(this, PrefActivitySmsForwarder.class);
      PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent, 0);
      String forwarder_start_str= getResources().getString(R.string.sms_forwarding_activated);
      String app_name=getResources().getString(R.string.app_name);
      Notification n= new Notification(R.drawable.ic_launcher,forwarder_start_str, System.currentTimeMillis());
      n.setLatestEventInfo(getApplicationContext(), app_name, forwarder_start_str, pi);
      n.defaults= Notification.DEFAULT_ALL;
      nm.notify(uniqueId, n);
      finish();

  }

3 个答案:

答案 0 :(得分:0)

这是我创建通知的方式。使用setOngoing(true)我使其持久化

http://developer.android.com/reference/android/app/Notification.Builder.html#setOngoing(boolean)

Intent intent = new Intent("MY_INTENT");
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),0,intent,PendingIntent.FLAG_ONE_SHOT);
Notification notification = new Notification.Builder(getApplicationContext())
                                .setContentTitle("Title")
                                .setContentText("Click here")
                                .setSmallIcon(R.drawable.img2)
                                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.img1))
                                .setContentIntent(pendingIntent)
                                .setOngoing(true)
                                .build();

如果您想保留自己创建通知的方式而不使用Notification.Builder,可以添加此行代码来编辑通知的标记字段

notification.flags |= Notification.FLAG_ONGOING_EVENT;

答案 1 :(得分:0)

      String forwarder_start_str= getResources().getString(R.string.sms_forwarding_activated);
      String app_name=getResources().getString(R.string.app_name);
      Intent intent= new Intent(this, PrefActivitySmsForwarder.class);
      Notification n= new Notification(R.drawable.ic_launcher,forwarder_start_str, System.currentTimeMillis());

     /** I ADD THIS **/ n.flags=Notification.FLAG_ONGOING_EVENT;
      PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent,0);
      n.setLatestEventInfo(getApplicationContext(), app_name, forwarder_start_str, pi);
      n.defaults= Notification.DEFAULT_ALL;
      nm.notify(uniqueId, n);
      finish();

答案 2 :(得分:0)

你也可以这样做

    int icon = R.drawable.icon_notification;


    String msg= "hey";

    Intent notificationIntent = new Intent(context,Activity.class);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                                                                .setSmallIcon(icon)
                                                                .setContentTitle(context.getString(R.string.app_name))
                                                                .setContentIntent(intent)
                                                                .setPriority(PRIORITY_LOW) 
                                                                .setContentText(msg)
                                                                .setAutoCancel(true)
                                                                .setDefaults(Notification.DEFAULT_SOUND   | 
                                                                             Notification.DEFAULT_VIBRATE | 
                                                                             Notification.DEFAULT_LIGHTS);


    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(notificationId, mBuilder.build());