Android - Set notification to never go away

风格不统一 提交于 2020-01-02 16:22:48

问题


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.



来源:https://stackoverflow.com/questions/12906332/android-set-notification-to-never-go-away

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!