Balázs Édes
Balázs Édes

Reputation: 13817

Get a BroadcastReceiver to outlive it's Activity

I work on an Android app, what should simply post some information about the battery in the notification-bar. I started this project like all the tutorials advised:

It works really fine, until the Activity (what registered the two) stops. By stoping i mean, i press the back button on the phone, and i assume the onStop() or/and onDestroy() methods get called.

My question is, how could i get the BroadcastReceiver to run after the Activity is finished, and only stop recieving, when i 'Force close' the app?

UPDATE:

Okay, from the previous answers i think, what i need to do, is start my BroadcastReciever from the manifest file, and not from the Activity. This is what i tried to do, but it simply doesn't start recieving:

<receiver android:name="com.battery.indicator.BatteryReciever" android:enabled="true" >
    <intent-filter>
        <action android:name="android.intent.action.BATTERY_CHANGED" ></action>
    </intent-filter> 
</receiver>

The name attribute is the whole package-path to my Reciever class. The actions name is what Eclipse's intellisense found. All of this is in the <application></application> part of the xml.

What am I doing wrong?

Upvotes: 0

Views: 706

Answers (3)

Mike
Mike

Reputation: 2409

I'm not sure if this will help or not, but here is how I setup my manifest.

<manifest xmlns:android="http://schemas.android.com./apk/res/android"
          package="com.namespace"
          versionCode, etc>

   //List permissions you use, looks like there is one for battery stats
   <uses-permission android:name="android.permission.BATTERY_STATS" />

   <application>
     <receiver android:name=".broadcast_recievers.BatteryReciever">
       <action android:name="android.intent.action.BATTERY_CHANGED" />
     </receiver>
   </application>
</manifest>

The main difference I want to illustrate is you declare your project namespace/package in the manifest tag and then declare the path to the class in the receiver name. In my example I would have a class named BatteryReciever located in com.namespace.broadcast_recievers.

You'll have to do research to determine if you need to include the permission or not. This SO post suggests that you do not.

Upvotes: 0

fweigl
fweigl

Reputation: 22038

If you declare your BroadcastReceiver in the Manifest (rather than the code) and let if receive Boot completed, it will always be alive.

Like this in your manifest file:

        <receiver android:name=".BootReceiver">  
         <intent-filter>  
             <action android:name="android.intent.action.BOOT_COMPLETED" />  
         </intent-filter>  
    </receiver>

Note: you need this permission to do that:

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

Upvotes: 0

Kaediil
Kaediil

Reputation: 5535

Create service and register your receiver in the Manifest. Then Android will wake up and call your receiver even if your app is currently closed.

Upvotes: 2

Related Questions