2014-02-24 73 views
1

我正在使用以下代碼將圖標添加到通知狀態,它運行良好,即使我退出應用程序,它也會始終顯示。重新啓動手機後,如何始終顯示通知圖標?

但是,只要我重新啓動手機,圖標就會消失,即使重新啓動手機,圖標也會始終顯示,我該怎麼做?

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

回答

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 

     } 
    } 

編碼快樂:)

+0

謝謝!在我的mainifest文件中,我有代碼<! - Broadcast receiver - > ,我可以添加一個新的? – HelloCW

+1

當然。您可以根據需要添加儘可能多的廣播接收器。 – Adnan

1

在清單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 
      } 
     } 
    } 
相關問題