How to make an Android status bar notification persist across phone reboot?

AndroidApps/ApplicationsMobile Development

<p>This example demonstrate about How to make an Android status bar notification persist across phone reboot</p><p><strong>Step 1</strong> &minus; Create a new project in Android Studio, go to File &rArr; New Project and fill all required details to create a new project.</p><p><strong>Step 2</strong> &minus; Add the following code to res/layout/activity_main.xml.</p><pre class="prettyprint notranslate">&lt;? xml version = &quot;1.0&quot; encoding = &quot;utf-8&quot; ?&gt; &lt;RelativeLayout xmlns: android = &quot;; &nbsp; &nbsp;xmlns: tools = &quot;; &nbsp; &nbsp;android :layout_width = &quot;match_parent&quot; &nbsp; &nbsp;android :layout_height = &quot;match_parent&quot; &nbsp; &nbsp;android :padding = &quot;16dp&quot; &nbsp; &nbsp;tools :context = &quot;.MainActivity&quot; / &gt;</pre><p><strong>Step 3</strong> &minus; Add the following code to src/MainActivity.</p><pre class="prettyprint notranslate">package ; import android.os.Bundle ; import ; public class MainActivity extends AppCompatActivity { &nbsp; &nbsp;@Override &nbsp; &nbsp;protected void onCreate (Bundle savedInstanceState) { &nbsp; &nbsp; &nbsp; super .onCreate(savedInstanceState) ; &nbsp; &nbsp; &nbsp; setContentView(R.layout. activity_main ) ; &nbsp; &nbsp;} }</pre><p><strong>Step 4</strong> &minus; Add the following code to src/USBStateReceiver.</p><pre class="prettyprint notranslate">package ; import android.annotation. SuppressLint ; import ; import ; import ; import android.content.BroadcastReceiver ; import android.content.Context ; import android.content.Intent ; import ; import android.util.Log ; public class USBStateReceiver extends BroadcastReceiver { &nbsp; &nbsp;public static final String NOTIFICATION_CHANNEL_ID = &quot;10001&quot; ; &nbsp; &nbsp;private final static String default_notification_channel_id = &quot;default&quot; ; &nbsp; &nbsp;boolean connected = true; &nbsp; &nbsp;@SuppressLint ( &quot;UnsafeProtectedBroadcastReceiver&quot; ) &nbsp; &nbsp;@Override &nbsp; &nbsp;public void onReceive (Context context , Intent intent) { &nbsp; &nbsp; &nbsp; NotificationCompat.Builder builder = new NotificationCompat.Builder(context , default_notification_channel_id ) ; &nbsp; &nbsp; &nbsp; builder.setContentTitle( &quot;USB - Notification&quot; ) ; &nbsp; &nbsp; &nbsp; String action = intent.getAction() ; &nbsp; &nbsp; &nbsp; Log. e ( &quot;USB&quot; , action) ; &nbsp; &nbsp; &nbsp; assert action != null; &nbsp; &nbsp; &nbsp; builder.setContentText( &quot;Connected&quot; ) ; &nbsp; &nbsp; &nbsp; builder.setSmallIcon(R.drawable. ic_launcher_foreground ) ; &nbsp; &nbsp; &nbsp; builder.setAutoCancel( true ) ; &nbsp; &nbsp; &nbsp; builder.setChannelId( NOTIFICATION_CHANNEL_ID ) ; &nbsp; &nbsp; &nbsp; Notification notification = ; &nbsp; &nbsp; &nbsp; NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context. NOTIFICATION_SERVICE ) ; &nbsp; &nbsp; &nbsp; if (android.os.Build.VERSION. SDK_INT &gt;= android.os.Build.VERSION_CODES. O ) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int importance = NotificationManager. IMPORTANCE_HIGH ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID , &quot;NOTIFICATION_CHANNEL_NAME&quot; , importance) ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;assert notificationManager != null; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;notificationManager.createNotificationChannel(notificationChannel) ; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; assert notificationManager != null; &nbsp; &nbsp; &nbsp; if ( connected ) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;notificationManager.notify( 1 , notification) ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;connected = false; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;notificationManager.cancel( 1 ) ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;connected = true; &nbsp; &nbsp;} }</pre><p><strong>Step 5</strong> &minus; Add the following code to AndroidManifest.xml</p><pre class="prettyprint notranslate">&lt;? xml version= &quot;1.0&quot; encoding= &quot;utf-8&quot; ?&gt; &lt;manifest xmlns: android = &quot;; &nbsp; &nbsp;package = &quot;; &gt; &nbsp; &nbsp;&lt;uses-permission android :name = &quot;android.permission.VIBRATE&quot; /&gt; &nbsp; &nbsp;&lt;uses-permission android :name = &quot;android.permission.RECEIVE_BOOT_COMPLETED&quot; /&gt; &nbsp; &nbsp;&lt;application &nbsp; &nbsp; &nbsp; android :allowBackup= &quot;true&quot; &nbsp; &nbsp; &nbsp; android :icon = &quot;@mipmap/ic_launcher&quot; &nbsp; &nbsp; &nbsp; android :label = &quot;@string/app_name&quot; &nbsp; &nbsp; &nbsp; android :roundIcon = &quot;@mipmap/ic_launcher_round&quot; &nbsp; &nbsp; &nbsp; android :supportsRtl = &quot;true&quot; &nbsp; &nbsp; &nbsp; android :theme= &quot;@style/AppTheme&quot; &gt; &nbsp; &nbsp; &nbsp; &lt;activity android :name = &quot;.MainActivity&quot; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;intent-filter&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;action android :name = &quot;android.intent.action.MAIN&quot; /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;category android :name = &quot;android.intent.category.LAUNCHER&quot; /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;/intent-filter&gt; &nbsp; &nbsp; &nbsp; &lt;/activity&gt; &nbsp; &nbsp; &nbsp; &lt;receiver android :name = &quot;.USBStateReceiver&quot; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;intent-filter&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;action android :name = &quot;android.intent.action.BOOT_COMPLETED&quot; /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;/intent-filter&gt; &nbsp; &nbsp; &nbsp; &lt;/receiver&gt; &nbsp; &nbsp;&lt;application&gt; &lt;/manifest&gt;</pre><p>Let&#39;s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project&#39;s activity files and click Run <img src="" class="fr-fic fr-dii" width="14" height="14">&nbsp;icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen &minus;</p><p><img src="" class="fr-fic fr-dib" style="width:254px; height:446px;" width="254" height="446"></p><p>Click <a href="/android/projects/how_to_make_an_android_status_bar_notification_persist_across_phone_reboot/" rel="nofollow" target="_blank">&nbsp;here&nbsp;</a> to download the project code</p>
Updated on 03-Jul-2020 11:54:06