Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BOOT_COMPLETED not working Android

First of all, i know there has been hundreds of this kind of question asked, but i've been checking them all for a while and still couldn't find any solution.

I've seen this answer said BOOT_COMPLETED not send to application unless user launch your application first, after Android version 3.1 But i still see some applications are doing that, there must be a way. I really need to handle it, otherwise i'm also against to do something without user's interaction.

So here's my AndroidManifest:

<manifest ... >  <!-- to be activated service on boot is completed --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!-- Keeps the processor from sleeping when a message is received. --> <uses-permission android:name="android.permission.WAKE_LOCK" />  <application ... >     <!-- to receive data when boot completed -->     <receiver         android:name="myPackage.BootReceiver"         android:enabled="true"         android:exported="true"         android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >         <intent-filter>             <action android:name="android.intent.action.BOOT_COMPLETED" />         </intent-filter>     </receiver> </application>  </manifest> 

Thanks in advance.

Edit: There is no much thing to see in my broadcastreceiver but to whom required here it is:

package myPackage public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {     Utils.LogI("BootReceiver", "BootReceiver received!");     if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {         // Do my stuff     } } } 
like image 348
yahya Avatar asked Dec 07 '13 12:12

yahya


People also ask

What is Boot completed in Android?

When Android is booting ( Or you powered ON android first time or rebooted ), and when it has completed booting, Android sends a broadcast intent BOOT_COMPLETED to let all the applications, services know that now everything is setup and android booting has been completed, so we can start any other services , ...

What is Android permission Receive_boot_completed?

permission. RECEIVE_BOOT_COMPLETED. run at startup. Allows the app to have itself started as soon as the system has finished booting.

What is broadcast receiver in Android Studio?

Definition. A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.


2 Answers

This below thing worked for me

AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  <application>          <receiver android:name=".BootCompletedReceiver" >         <intent-filter>             <action android:name="android.intent.action.BOOT_COMPLETED" />             <action android:name="android.intent.action.QUICKBOOT_POWERON" />         </intent-filter>     </receiver>      <service android:name="NotifyingDailyService" >     </service> 

BootCompletedReceiver.class

public class BootCompletedReceiver extends BroadcastReceiver {  @Override public void onReceive(Context context, Intent arg1) {     // TODO Auto-generated method stub     Log.w("boot_broadcast_poc", "starting service...");     context.startService(new Intent(context, NotifyingDailyService.class)); }  } 

Service.class

 public class NotifyingDailyService extends Service {  @Override public IBinder onBind(Intent arg0) {     // TODO Auto-generated method stub     return null; }  @Override public int onStartCommand(Intent pIntent, int flags, int startId) {     // TODO Auto-generated method stub     Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();     Log.i("com.example.bootbroadcastpoc","NotifyingDailyService");      return super.onStartCommand(pIntent, flags, startId); } } 
like image 110
Piyush Avatar answered Sep 19 '22 12:09

Piyush


This is an old and basic question but a lot of Android developers now still confused about this trouble, because THEY DON'T TAKE TIME TO READ THE DOCS CAREFULLY

I saw someone shared some links and said that: "This won't work anymore", it's totally wrong and misunderstood.

About this concern: "I've seen this answer said BOOT_COMPLETED is not sent to the application unless the user launches your application first, after Android version 3.1", please read these lines (from official docs: https://developer.android.com/about/versions/android-3.1.html#launchcontrols) to understand correctly:

  • Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.

  • Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications). (They mean force stop an app)

enter image description here

  1. That means a user should launch app at least once after installation to activate the application, then the app can receive implicit broadcasts from OS as normal. (Just only one time launching ever !)

  2. "Does any app that gets installed and never open even only one time ever ?", yep, it 's spam and scam apps, this technique helps user to prevent that!

FURTHERMORE, UNTIL NOW (Android Oreo 8.0), when Android limits registering implicit broadcasts at Manifest (https://developer.android.com/about/versions/oreo/background.html#broadcasts), several broadcasts are still currently exempted from these limitations. And BOOT_COMPLETED is the first one they mention ! (https://developer.android.com/guide/components/broadcast-exceptions.html)

By the way, this is the best solution I found for this question:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>  <receiver android:name=".BootReceiver" android:enabled="true" android:exported="true">             <intent-filter>                 <category android:name="android.intent.category.DEFAULT"/>                 <action android:name="android.intent.action.BOOT_COMPLETED"/>                 <action android:name="android.intent.action.QUICKBOOT_POWERON"/>                 <!--For HTC devices-->                 <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>             </intent-filter>         </receiver> 

Finally, please read the document carefully and Think twice code once :3!

like image 20
Think Twice Code Once Avatar answered Sep 19 '22 12:09

Think Twice Code Once