【问题标题】:How can I always display a Notification icon after I restart mobile phone?重启手机后怎么总是显示通知图标?
【发布时间】:2014-02-24 07:20:46
【问题描述】:

我正在使用以下代码将图标添加到通知状态,效果很好,即使我退出应用程序也始终显示。

但是重启手机后图标消失了,怎么才能让手机重启后图标一直显示呢?

private void showNotification() {

    NotificationManager notificationManager = (NotificationManager)
    this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);


    Notification notification = new Notification(R.drawable.smsforward,"My System", System.currentTimeMillis());


    notification.flags |= Notification.FLAG_ONGOING_EVENT; 
    notification.flags |= Notification.FLAG_NO_CLEAR;

    CharSequence contentTitle = "My System Title"; 
    CharSequence contentText = "My System Title content"; 

    Intent notificationIntent = new Intent(this, SMSMain.class); 

    PendingIntent contentItent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    notification.setLatestEventInfo(this, contentTitle, contentText,contentItent);

    notificationManager.notify(0, notification);
}

【问题讨论】:

    标签: android


    【解决方案1】:

    您需要在启动完成时收听,然后再次显示相同的通知。这是一个简单的例子。

    在您的清单文件中。

    <receiver android:name="BootNotificationReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    

    在你的代码中。

     public class BootNotificationReceiver extends BroadcastReceiver
        {
        public void onReceive(Context context, Intent intent) {
    
         // display notification again
         showNotification();// or whatever
    
          }
        }
    

    快乐编码:)

    【讨论】:

    • 谢谢!在我的 mainifest 文件中,我有代码 ,我可以添加一个新的 吗?
    • 当然。您可以根据需要添加任意数量的广播接收器。
    【解决方案2】:

    您可以处理设备启动意图并再次启动相同的通知。

    Android BroadcastReceiver, auto run service after reboot of device

    【讨论】:

      【解决方案3】:

      在清单 xml 中声明 Broadcast Receiver

      public class BootReceiver extends BroadcastReceiver
          {
      
              @Override
              public void onReceive(Context ctx, Intent intent) {
                  if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
                  {
                      // your code here
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 2019-04-21
        • 2018-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多