简体   繁体   中英

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:

  • 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.

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"/>  

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM