Android - Set notification to never go away

2019-05-23 06:32发布

问题:

I have this code that works great:

Notification n = builder.build();
n.flags = Notification.FLAG_NO_CLEAR;

But when I restart the phone, the notification goes away. Is there any flag that make that happen?

回答1:

If you want to print notification when the device boots up, you can create a receiver that is invoked when the system boot is completed, for this, first create a receiver,

public class MyReciever extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) 
{
        Log.d("BOOT COMPLETE","SERVICE CALLED>>>>>>>>>>>>");
        //use your code here to print notifications

    }
}

This receiver is invoked when the system boot is completed. You can also call a service from the onReceive method of receiver to print the notification.

Also you must define the following regularities in your manifest file,

First define permission for getting BOOT_COMPLETION intent,

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Then define your receiver also,

 <receiver android:name=".MyReciever" 
             android:enabled="true" 
             android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>


回答2:

No. I don't think that is possible.

You could have a service that runs at start-up to to bring up that notification again. Notifications otherwise do not persist across reboots.