What happens with the registered broadcast receivers after the application is killed?

by Tughi » Thu, 25 Mar 2010 23:06:43 GMT


Sponsored Links
 Hi guys,
I want to optimize the CPU usage in my application.

Currently my application is configured to always receive the
CONNECTIVITY_CHANGED action to force an update if the previous update
failed because there was no connectivity.
What I don't like about this is that the broadcast receiver gets to be
called too many times although it is not needed.

I was thinking to register my broadcast receiver only if an update
failed using the Context.registerReceiver(BroadcastReceiver receiver,
IntentFilter filter) method. But I'm not so sure if this is a good
idea.

I'm concerned that if my application is evicted from memory the
broadcast receiver will be unregistered or lost and my application
will not be notified about the future CONNECTIVITY_CHANGED actions.

The update is done in a short lived service. So if the update fails,
the service will register the broadcast receiver just before it ends
its execution time.

Can somebody explain what happens to my broadcast receiver after the
application is evicted from memory?

Thanks

--



What happens with the registered broadcast receivers after the application is killed?

by Mark Murphy » Thu, 25 Mar 2010 23:23:28 GMT


 


You will leak memory, as your BroadcastReceiver will keep the component
in RAM (even if it was destroyed) until such time as Android terminates
the process.

You might consider going with a manifest-registered receiver, but
enabling and disabling the component as needed via PackageManager.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Android Training...At Your Office:  http://commonsware.com/training 

--


Sponsored Links


What happens with the registered broadcast receivers after the application is killed?

by Tughi » Fri, 26 Mar 2010 21:21:47 GMT


 I had no idea I could do that with the components declared in the
manifest.

Thanks a lot!






--



What happens with the registered broadcast receivers after the application is killed?

by mmonibi » Fri, 23 Apr 2010 08:03:45 GMT


 What if my receiver is registered for the ACTION_BATTERY_CHANGED
intent?

As stated in the documentation, "You can not receive this through
components declared in manifests, only by explicitly registering for
it with Context.registerReceiver()."

Is there anyway to make sure my receiver is always registered for this
intent, even if the process is destroyed? Would it make a difference
if I used the application context when registering the receiver
instead of the activity context? (frankly, I still don't have a good
grasp of the context object and don't quite understand how the
application and activity contexts differ)

I know that I may be able to use the new battery related broadcasts
that have been added to API level 4, but I'm trying to stick to level
3 to ensure compatibility with all devices.

Thanks!







--



What happens with the registered broadcast receivers after the application is killed?

by mmonibi » Fri, 23 Apr 2010 08:03:58 GMT


 What if my receiver is registered for the ACTION_BATTERY_CHANGED
intent?

As stated in the documentation, "You can not receive this through
components declared in manifests, only by explicitly registering for
it with Context.registerReceiver()."

Is there anyway to make sure my receiver is always registered for this
intent, even if the process is destroyed? Would it make a difference
if I used the application context when registering the receiver
instead of the activity context? (frankly, I still don't have a good
grasp of the context object and don't quite understand how the
application and activity contexts differ)

I know that I may be able to use the new battery related broadcasts
that have been added to API level 4, but I'm trying to stick to level
3 to ensure compatibility with all devices.

Thanks!







--



Other Threads

1. onDraw() being called multiple unintended times

Here is a short program where my intent is to scale up or down the
size of a circle when the zoom controls are used. In addition to my
intended behavior, I am getting two "extra" (unintended) calls to the
onDraw() function in response to my zoomControl onClick callback.  (I
have used DDMS to log my debug statements.)

Can anyone explain the source of the onDraw() calls besides the
zoomControl onClick() method?

---------------------------------------------------------------------------------------
package com.example.simpledraw;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.ZoomControls;

public class SimpleDraw extends Activity {

        private static final String TAG = "SimpleDrawActivity";
        public static double scale;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        scale = 2.0;
        RelativeLayout relativeLayout = new RelativeLayout
(this);
        setContentView(relativeLayout);
        final MyView myView = new MyView(this);
        RelativeLayout.LayoutParams myViewLayoutParams =
                new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT
 );
        relativeLayout.addView(myView, myViewLayoutParams);
        RelativeLayout.LayoutParams zoomControlsLayoutParams =
                new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT
 );
        zoomControlsLayoutParams.addRule
(RelativeLayout.ALIGN_PARENT_BOTTOM);
        zoomControlsLayoutParams.addRule
(RelativeLayout.CENTER_HORIZONTAL);
        ZoomControls zoomControls = new ZoomControls(this);

        relativeLayout.addView
(zoomControls,zoomControlsLayoutParams);
        myView.setEnabled(true);
        zoomControls.setIsZoomInEnabled(true);
        zoomControls.setIsZoomOutEnabled(true);
        zoomControls.setOnZoomInClickListener(new OnClickListener()
{
            public void onClick(View v) {
                myView.invalidate();
                scale *= 1.25;
                Log.v(TAG, "The scale is  " + scale);
            }
        });
        zoomControls.setOnZoomOutClickListener(new OnClickListener()
{
            public void onClick(View v) {
                myView.invalidate();
                scale *= 0.8;
                Log.v(TAG, "The scale is  " + scale);
            }
        });
    }

    static public class MyView extends View {

        Integer number = 0;

        public MyView(Context context) {
                super(context);
        }
        @Override
        protected void onDraw(Canvas canvas){
                //super.onDraw(canvas);
                Log.w(TAG, "The onDraw() method is called for the " + number + "
time.");
                //Log.w(TAG, "The view calling OnDraw is " + this.getId() );
                canvas.drawColor(0);
                Path circle = new Path();
                circle.addCircle(150, 150, (int)(scale*10.), Direction.CW);
                Paint cPaint = new Paint();
                cPaint.setColor(Color.LTGRAY);
                canvas.drawPath(circle, cPaint);
                Paint textPaint = new Paint();
                textPaint.setColor(Color.GREEN);
                canvas.drawText(number.toString(), 150, 250, textPaint);
                number++;

        }
    }


}

--

2. Rejoining Contact failing

I am having an issue with the automerge.

I have multiple contacts in my address book that are close to match as
well as face book accounts with the same.
After I separate these contacts using the contacts build in system I
am not able to rejoin them to the correct contacts.

Example:

John Doe  Gmail contact
John Doe Facebook contact
Jonathan Doe Gmail contact
Jonathan Doe Facebook contact

All of these contacts are merged originally into one contact via
android contact automerge.  I separate them into individual contacts.

Reattempt to merge John Doe Gmail and John Doe Facebook.  When I
attempt this the join list is empty and android will not allow me to
merge these contacts.

Any help would be appreciated.

--

3. access voice dialer through my bluetooth

4. USB connection question

5. Why Android Sucks

6. MIP Client on Android

7. Flash and Youtube