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:
- I created a BroadcastReceiver. It handles the UI stuff.
- Then i created an IntentFilter, what filters only the
ACTION_BATTERY_CHANGED intents - Then i simply registered the IntentFilter and the BroadcastReceiver instances
with the registerReceiver() method of my main Activity.
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?
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.