{"id":421,"date":"2012-08-11T16:39:49","date_gmt":"2012-08-11T11:09:49","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=421"},"modified":"2012-08-11T16:39:49","modified_gmt":"2012-08-11T11:09:49","slug":"starting-android-activity-on-device-restart","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/starting-android-activity-on-device-restart\/","title":{"rendered":"Starting Android Activity on device restart"},"content":{"rendered":"<p>I finally got around to fixing a bug in my Android application, <a href=\"https:\/\/market.android.com\/details?id=ram.kulkarni.str\" target=\"_blank\">Simple Task Reminder<\/a>, which was on my to-do list for a long time.The issue was that all reminders set in this application were lost on device restart. The solution was to set reminders again after device rebooted.<\/p>\n<p>Android SDK provides a mechanism to receive notification for your application, after the device is booted. You need to add a broadcast receiver in the manifest file &#8211;<br \/>\n<!--more--><\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #333385;\">receiver<\/span> <span style=\"color: #474796;\">android:name<\/span><span style=\"color: #308080;\">=<\/span><span style=\"color: #1060b6;\">\"<\/span><span style=\"color: #1060b6;\">BootNotificationReceiver<\/span><span style=\"color: #1060b6;\">\"<\/span> <span style=\"color: #474796;\">android:exported<\/span><span style=\"color: #308080;\">=<\/span><span style=\"color: #1060b6;\">\"<\/span><span style=\"color: #1060b6;\">false<\/span><span style=\"color: #1060b6;\">\"<\/span>\n    <span style=\"color: #474796;\">android:label<\/span><span style=\"color: #308080;\">=<\/span><span style=\"color: #1060b6;\">\"<\/span><span style=\"color: #1060b6;\">Boot Notification Receiver<\/span><span style=\"color: #1060b6;\">\"<\/span> <span style=\"color: #474796;\">android:enabled<\/span><span style=\"color: #308080;\">=<\/span><span style=\"color: #1060b6;\">\"<\/span><span style=\"color: #1060b6;\">true<\/span><span style=\"color: #1060b6;\">\"<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n\n    <span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #333385;\">intent-filter<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n        <span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #333385;\">action<\/span> <span style=\"color: #474796;\">android:name<\/span><span style=\"color: #308080;\">=<\/span><span style=\"color: #1060b6;\">\"<\/span><span style=\"color: #1060b6;\">android.intent.action.BOOT_COMPLETED<\/span><span style=\"color: #1060b6;\">\"<\/span><span style=\"color: #0057a6;\">\/&gt;<\/span>\n    <span style=\"color: #0057a6;\">&lt;\/<\/span><span style=\"color: #333385;\">intent-filter<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n<span style=\"color: #0057a6;\">&lt;\/<\/span><span style=\"color: #333385;\">receiver<\/span><span style=\"color: #0057a6;\">&gt;<\/span><\/pre>\n<p>Value of android:name attribute above is your class name that extends BroadcastReceiver class. In onReceive function of this class you can start the Activity of your application e.g. &#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #200080; font-weight: bold;\">class<\/span> BootNotificationReceiver <span style=\"color: #200080; font-weight: bold;\">extends<\/span> BroadcastReceiver <span style=\"color: #406080;\">{<\/span>\n\n    <span style=\"color: #308080;\">@<\/span>Override\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #7779bb;\">void<\/span> onReceive<span style=\"color: #308080;\">(<\/span>Context context<span style=\"color: #308080;\">,<\/span> Intent intent<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        Intent mainIntent <span style=\"color: #308080;\">=<\/span> <span style=\"color: #200080; font-weight: bold;\">new<\/span> Intent <span style=\"color: #308080;\">(<\/span>context<span style=\"color: #308080;\">,<\/span> SimpleTaskReminderActivity<span style=\"color: #308080;\">.<\/span>class<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        mainIntent<span style=\"color: #308080;\">.<\/span>putExtra<span style=\"color: #308080;\">(<\/span><span style=\"color: #1060b6;\">\"refresh_reminders\"<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #200080; font-weight: bold;\">true<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        mainIntent<span style=\"color: #308080;\">.<\/span>setFlags<span style=\"color: #308080;\">(<\/span>Intent<span style=\"color: #308080;\">.<\/span>FLAG_ACTIVITY_NEW_TASK<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        context<span style=\"color: #308080;\">.<\/span>startActivity <span style=\"color: #308080;\">(<\/span>mainIntent<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n<span style=\"color: #406080;\">}<\/span><\/pre>\n<p>You also need to add RECEIVE_BOOT_COMPLETED user permission in the menifest file-<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #333385;\">uses-permission<\/span> <span style=\"color: #474796;\">android:name<\/span><span style=\"color: #308080;\">=<\/span><span style=\"color: #1060b6;\">\"<\/span><span style=\"color: #1060b6;\">android.permission.RECEIVE_BOOT_COMPLETED<\/span><span style=\"color: #1060b6;\">\"<\/span><span style=\"color: #0057a6;\">\/&gt;<\/span><\/pre>\n<h3>Accessing WebView local DB from Java :<\/h3>\n<p>I have used WebView for this application and used local database of the WebView to store tasks. All data access code in WebView is in JavaScript. Initially, I was looking for a way to access this database from Java code in my Activity. I found that you can get path of the database file of WebView as follows &#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">appView.getSettings().getDatabasePath()<span style=\"color: #308080;\">;<\/span> <span style=\"color: #595979;\">\/\/appView is an instance of WebView.<\/span><\/pre>\n<p>However I did not end up using this API for refreshing reminders, because I already had all the code in JavaScript to get tasks and format data to schedule reminders. What I ended up doing was starting the Activity that had WebView, execute JavaScript function to re-schedule reminders and then hiding the Activity. Hiding Activity in an Android application is as simple as calling moveTaskToBack method on the activity &#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">activity.moveTaskToBack(true)<span style=\"color: #308080;\">;<\/span><\/pre>\n<p>-Ram Kulkarni<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I finally got around to fixing a bug in my Android application, Simple Task Reminder, which was on my to-do list for a long time.The issue was that all reminders set in this application were lost on device restart. The solution was to set reminders again after device rebooted. Android SDK provides a mechanism to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/starting-android-activity-on-device-restart\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Starting Android Activity on device restart&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[3,1],"tags":[2,45,46],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-6N","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/421"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=421"}],"version-history":[{"count":0,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/421\/revisions"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}