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. How to overlay text on video from camera (using Mediarecorder)

How to overlay text or any other object(image or draw something)
during recording video from camera using Mediarecorder ?
For example I need to record video with date and time over.
Is it any way to overlay or draw something ?

-- 

2. How to scroll/move text within TextView on home screen widget ?

Hi All,

I have created a widget to be displayed on android emulator's home
screen to display some long text. In my main.xml layout file i've
already set TextView properties like singleLine="true",
ellipsize="marquee", focusable="true" etc, but still when my widget is
displayed on home screen text does not move/scroll.


----------------- main.xml layout file ------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="fill_parent"
androidrientation="vertical"
android:layout_gravity="center"
android:layout_height="wrap_content" android:background="@drawable/
background">
<TextView android:id="@+id/widget_textview"
android:text="@string/widget_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal|center"
android:layout_marginTop="5dip"
androidadding="10dip"
android:textColor="@android:color/black"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true" />
</LinearLayout>


----------------- end of main.xml layout file ------------------------



------------- My WidgetProvider.java file ----------------------

package com.android.weatherdata;

import android.util.Log;
import android.widget.RemoteViews;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.app.PendingIntent;

public class WeatherWidget extends AppWidgetProvider{

private static final String TAG = "WeatherWidget";

@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds)
{
Log.d("WEATHER-WIDGET", "onUpdate(): ");

final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to
this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];

// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, Weather.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);

Log.i("WEATHER-WIDGET", " Create and Attach Text-view click handler
");
// Get the layout for the App Widget and attach an on-click listener
to the button
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.main);
views.setOnClickPendingIntent(R.id.widget_textview , pendingIntent);

// Tell the AppWidgetManager to perform an update on the current App
Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}


----------------- End of WidgetProvider file--------------------

When i add widget on home screen than only some part of text is
displayed as " Latest Weather Infor" but i would like to write the
code in such a way that the complete text should scroll till the
complete string/text is displayed.


Please suggest whether i need to change my layout file or what logic i
can write so that i can scroll the text.

Thanks in advance,
Ravi

-- 

3. Good site for standard cookbook style examples - date pickers, etc.?

4. Handler loses message after wait() and notify()

5. FTP client : How to change working directory

6. how can i launch a application or activity when call is incoming

7. Tab Click Event