Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6650083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:51:03+00:00 2026-05-26T00:51:03+00:00

I have spent few days to work on Service + BroadcastReceiver, but still cannot

  • 0

I have spent few days to work on Service + BroadcastReceiver, but still cannot make it perfect. I hope someone can help, thanks!

I am writing a App that show user current location on map(The map that I wrote, not Google Map) and send out Notification alarm when user go inside a predefined zone.

In my code. there are two main objects. A GPS service and Main Activity.

The GPS service broadcast location when location changed. The Main Activity receive the latest location by BroadcastReceiver.

I have done some researches on how to register BroadcastReceiver. There are two ways that I found:


Method 1 – Register Broadcast Receiver inside Activity (I am using this method in my code. I need to update latest location on map)

Main.java:

public class Main extends Activity{
.
.
.
    public class MyLocReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            Bundle bundle=intent.getExtras();
            String locData = bundle.getString("loc");

            // Some work


        }
        public MyLocReceiver(){
            Log.e(TAG, "in MyReceiver()");
        }

    }

    private MyLocReceiver myLocReceiver;

    // Register BroadcastReceiver
    if (myLocReceiver == null)
        {
            Common.writeFile("service.txt", "Main - in myBindService() register receiver" + "\n", true);

            myLocReceiver=new MyLocReceiver();

            registerReceiver(myLocReceiver, new IntentFilter("com.nwfb.LOC_DATA"));
        }

Method 2 – Register BroadcastReceiver at Manifest.xml

Manifest.xml:

    <receiver android:name=".LocationBoardcastReceiver" android:enabled="true"> 
            <intent-filter>
                <action android:name="com.abc.LOC_DATA" /> 
            </intent-filter> 
    </receiver>

LocationBoardcastReceiver.java:

    public class LocationBoardcastReceiver extends BroadcastReceiver {
        private static final String TAG = LocationBoardcastReceiver.class.getSimpleName();

        @Override
        public void onReceive(Context context, Intent intent) {

        Bundle bundle=intent.getExtras();
        // Do something
        }
    }

* What I want is the BroadcastReceiver MUST NOT KILLED by the OS in the application life time. Also the BroadcastReceiver must able to pass data to my Main.java

I read this actical:
http://developer.android.com/reference/android/content/BroadcastReceiver.html

The section of ‘Receiver Lifecycle’ and ‘Process Lifecycle’ state that the BroadcastReceiver will be finished and no longer active after onReceive(Context, Intent) called.

I am using Method 1, I can receive more than 1 Location data from the Service. I found that the BroadcastReceiver keeps alive if the BroadcastReceiver keeps receiving data from the Broadcast.
If I turn off GPS at ‘Setting -> Location ‘ and let the BroadcastReceiver idle for about 1 to 2 hours, then the BroadcastReceiver will killed by OS.


Does the OS kill BroadcastReceiver if it idle too long? Does the OS NOT kill the BroadcastReceiver if it keeps receiving broadcast?

Will the BroadcastReceiver (Method 1 and 2) killed by OS when it is under cases of extreme memory pressure?

For Method 2, is it possible that the LocationBoardcastReceiver.java send data to a running activity (Eg: Main,java)?

For Method 1, is it any way to keep the BroadcastReceiver alive during the life time of the Main.java (Main Activity)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:51:04+00:00Added an answer on May 26, 2026 at 12:51 am

    Does the OS kill BroadcastReceiver if it idle too long? Does the OS
    NOT kill the BroadcastReceiver if it keeps receiving broadcast?

    Will the BroadcastReceiver (Method 1 and 2) killed by OS when it is
    under cases of extreme memory pressure?

    Your receiver comes in action when it receives any notification and it’s active duration is it’s onReceive() Method.

    For Method 2, is it possible that the LocationBoardcastReceiver.java
    send data to a running activity (Eg: Main,java)?

    Bad idea.

    For Method 1, is it any way to keep the BroadcastReceiver alive during
    the life time of the Main.java (Main Activity)?

    Check above.

    Your best way will depend over your app what it requires.

    1. If your app wants to use BroadcastReceiver while app is in foreground then you should go for Method1 as you have mentioned.

    2. If your app need to receive system’s notification as Boot Completion etc then you should go for Method2 as you have mentioned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent the last few days trying to parse a SOAP response but
I've spent the last few days banging my head and searching Google and have
I have spent the last few days attempting to integrate a Grails (version 1.3.2)
I have spent a few days learning and writing NSCoding and finally got it
I have spent a good few days learning linq and producing json results from
I have spent the last few days playing around with Drupal for the first
I have spent several days so far laying the ground work to use FastReport
I have spent a few days learning Objective-C and have a few questions about
I have spent the last few days trying to look up how to use
I've spent so much time the last few days trying to work out some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.