【问题标题】:android status bar notyfication iconandroid状态栏通知图标
【发布时间】:2014-07-15 07:49:38
【问题描述】:

我正在尝试为状态栏设置一个图标,单击按钮后我无法查看它。 问题是删除选项。我想设置这样你就不会被删除 只要那些进入应用程序并删除

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

  }

【问题讨论】:

    标签: android notifications statusbar


    【解决方案1】:
          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】:

      这就是我创建通知的方式。使用 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;
      

      【讨论】:

      • 不工作,我看到需要使用 FLAG_ONGOING_EVENT 但 eclipse 没有给我这个选项
      【解决方案3】:

      你也可以这样做

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

      【讨论】:

      • 我可以设置即使关机再开机后手机图标仍然停留?
      • 如果您希望图标保持不变,请使用 startForeground(notificationId, mBuilder.build());
      猜你喜欢
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多