How to implement a BroadcastReceiver in Android - android

I am trying to implement a BroadcastReceiver but it is not working.
I want to use it to return progress from a class that implements network io, which is called from an AsyncTask inside my Activity.
Here is the code for my activity:
package com.ClinicalInterface;
public class TestActivity extends ListActivity {
static AsyncDataLoader mAsyncDataLoader;
static ProgressDialog dialog;
static ArrayList<String> list;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dialog = new ProgressDialog(this);
dialog.setMessage("Loading...");
dialog.show();
mAsyncDataLoader = new AsyncDataLoader();
mAsyncDataLoader.execute();
}
public class AsyncDataLoader extends AsyncTask<Void, Integer, String> {
public class mTestReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
System.out.println( "I've received something!" );
publishProgress(2);
}
}
#Override
protected String doInBackground( Void ... params ) {
TestLoader tl = new TestLoader();
tl.setContext(getApplicationContext());
tl.setServeraddress("192.168.2.109");
list = tl.doLST(null);
return "COMPLETE!";
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (values[0] == 2) {
dialog.setMessage("Loading data ...");
}
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.dismiss();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(TestActivity.this, R.layout.list_item, list);
TestActivity.this.setListAdapter(adapter);
}
}
}
This is supposed to display a list, then overlay this with a progress dialog while the data for the list is returned from a server. And this works OK.
I would like to update the text in the progress dialog as the network io is done.
This is the code that implements the network io:
package com.ClinicalInterface;
public class TestLoader {
private Context mContext;
public void setContext(Context context) {
mContext = context;
}
public ArrayList<String> doLST(String arg) {
// Send intent to AsyncTask
Intent intent = new Intent(mContext, mTestReceiver.class);
intent.setAction("PROGRESS");
mContext.sendBroadcast(intent);
System.out.println( "message sent" );
// Code that actually does network io removed for brevity
}
}
In the Android manifest file I've added the following:
<activity android:name="TestActivity" android:label="TestActivity">
<receiver android:name=".AsyncDataLoader.mTestReceiver">
<intent-filter>
<action android:name="android.intent.action.PROGRESS" />
</intent-filter>
</receiver>
</activity>
I get the log print "message sent"
But not the log print "I've received something"
I am an Android newbie so I assume I've not implemented something correctly.
Any ideas?
Original post updated with intent-filter and set of action when creating the intent. Still not working.

Your <receiver> tag needs to contain an <intent-filter> to tell Android which broadcast intents you actually want to receive.
EDIT:
An Intent is not much more than a container for a message; it's the function that you call to send the Intent that determines which fields you need to set.
From the docs for Intent:
[Intent] can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
Those functions are your options for sending Intents. startActivity and the startService/bindService functions use explicit Intents; sendBroadcast does not.
Notice that startActivity throws an exception if it can't find your Intent's target class, and startService will return null if it can't find your target class. sendBroadcast doesn't do anything like that because it doesn't even look at that field. sendBroadcast "Broadcasts the given intent to all interested BroadcastReceivers."
Since you are using Context.sendBroadcast() to send your intent, you should be setting an action on your Intent and your BroadcastReceiver should have an intent filter containing an entry for that action.

Ok
It seems like the answer is that its not possible to use the manifest to register the receiver.
If I change the code so that it uses registerReceiver then it works ok.
Code updated like this:
#Override
protected String doInBackground( Void ... params ) {
TestLoader tl = new TestLoader();
IntentFilter intentFilter = new IntentFilter("PROGRESS");
mReceiver mmReceiver = new mReceiver();
registerReceiver(mmReceiver, intentFilter);
tl.setContext(getApplicationContext());
tl.setServeraddress("192.168.2.109");
list = tl.doLST(null);
unregisterReceiver(mmReceiver);
return "COMPLETE!";
}
And remove anything to do with the receiver from the manifest.
Note: the code that sends the Broadcast is like this:
Intent intent = new Intent();
intent.setAction("PROGRESS");
mContext.sendBroadcast(intent);

Related

How to start Activity once and then use the same instance?

I have the Activity with Messages list. When new message arrived I want to show it in the list. And I want to use the same Activity instance instead of creating new for each new message.
But my Activity doesn't opening (and I don't see my log message from onCreate() method).
There is me Activity code:
public class MessagesListDialogActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
super.onCreate(savedInstanceState);
Log.e("MESSAGE", "onCreate()");
// ... create UI
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(#NonNull Intent intent) {
// obtain the message and add to list on UI
}
}
And this is code from my Launcher class:
public static void showMessage(#NonNull Context context, #NonNull Message message) {
final Intent intent = new Intent(context, MessagesListDialogActivity.class);
BundleUtil.setMessage(intent, message);
context.startActivity(intent);
}
And this lines from Manifest:
<activity android:name=".features.messages.list.MessagesListDialogActivity"
android:screenOrientation="portrait"
android:theme="#style/DialogTheme"
android:launchMode="singleTop"/>
I try use different launchMode params, like singleTask and singleInstance but it doesn't work.
Thank you for your attention. After lot of attempts to fix it i try use this flags:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
And this help me. Thank you and hope my experience help you in the future.

Broadcast receiver not working in some cases

So i have this receiver in my Fragment.class
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(WeekplanHepler.ACTION_SERVICE_BINDED) && getUpdateService() != null) {
getCounts();
}
if (intent.getAction().equals(Helper.UpdateCounts)) {
HashMap<String, Integer> counts = (HashMap<String, Integer>) intent.getSerializableExtra(Helper.PARAM_LIST_COUNTS);
// Updating counts
}
}
};
For registering/uregistering this receiver i'm using this code:
#Override
public void onStart() {
super.onStart();
getCounts(true);
getActivity().registerReceiver(receiver, getIntentFilter());
}
#Override
public void onStop() {
super.onStop();
getActivity().unregisterReceiver(receiver);
}
getCounts() is a RetrofitRequest puted in UpdateService, which is getUpdateService() here
So, when retrofit request has been finished, it returns counts through Intent and then, as you see, i'm updating them. But if i go to next Activity and then returns, request will work fine, and it will send intent, but receiver wont get it. I think this can be caused by method service is binded in first place. So i have BaseFragment, which binds service for all Fragments needed
public abstract class BaseFragment<T> extends CustomListFragment<T> {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getApplicationContext().bindService(new Intent(getActivity().getApplicationContext(),
WeekplanUpdateService.class), connection, Context.BIND_AUTO_CREATE);
}
}
When i go to next activity, it fragment will bind service, and my previous fragment will call request again, and will have counts. Maybe receiver has some limitations for how much he can get same intents, but i don't think so. Please help
Oh, i forgot to mention how i'm sending intent
sendBroadcast(new Intent(Helper.UpdateCounts).putExtra(Helper.PARAM_LIST_COUNTS, counts));
So, when i've started to use
sendStickyBroadcast(new Intent(Helper.UpdateCounts)
.putExtra(Helper.PARAM_LIST_COUNTS, counts));
instead of
sendBroadcast(new Intent(Helper.UpdateCounts)
.putExtra(Helper.PARAM_LIST_COUNTS, counts));
it worked.

Android - Wiring up ContentObserver with Service

I have a simple application where I am trying to learn how to:
1) Call a web service
2) Parse the Data
3) Save it to SQLiteDatabase
4) Set-up a ContentObserver/Broadcaster to detect changes in an Activity and change the UI
So far, I have done everything but having trouble with step 4.
Code:
LaunchActivity which shows the layout containing the UI which I eventually want to change launches the Service class:
Intent intent = new Intent(this, DataService.class);
intent.putExtra("restMethodType", 0);
startService(intent);
The DataService class then runs the desired AsyncTask Rest Method:
public class DataService extends IntentService {
public DataService() {
super("DataService");
}
public DataService(String name) {
super(name);
}
#Override
protected void onHandleIntent(Intent intent) {
RestMethodType restMethodType = RestMethodType.values()[intent.getIntExtra("restMethodType", 0)];
switch (restMethodType) {
case UPCOMING_MATCH:
UpcomingMatchRestMethod restMethod = new UpcomingMatchRestMethod(getApplicationContext(), intent);
restMethod.execute();
break;
}
}
}
Within the onPostExecute method of the AsyncTask, I save the retrieved data in to the database and then broadcast the event:
long newRowId = database.insert(MatchContract.MatchEntry.TABLE_NAME, MatchContract.MatchEntry.COLUMN_NAME_LOCATION, values);
if(newRowId != -1){
mIntent.putExtra("matchId", newRowId);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(mIntent);
}
Back in the LaunchActivity, I set-up a receiver object:
DataReceiver mDataReceiver;
#Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter("What Goes In Here?");
registerReceiver(mDataReceiver, intentFilter);
}
This is what the receiver looks like:
private class DataReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Do Stuff like update the UI
// Make sure to wrap any View objects in runOnUIThread
}
}
So what goes in the IntentFilter? And what else am I missing? I followed the Google Docs but getting a little confused.
Thank you in advance.

Passing Data from Broadcast Receiver to another Activity

Hi I've been having an issue with Broadcast Receivers and passing information to another activity. I'm trying to create an application that will capture incoming SMS messages, look for a website in the text, then pop up an Alert Dialog box asking if the user wants to go to the website.
public class TextReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
// .. other code that
// sets received SMS into message
Toast toast = Toast.makeText(context,
"Received Text: " + message.getMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
So that code works fine, receive a text it pops up a toast with the message. The toast is useless but it shows the receiver works. But I want to communicate with an activity to show an Alert Dialog and start up a webView. I already programmed the code that will take a string search for the website and open the webView. Is it possible to get the string from the broadcast receiver and do something like this?:
public class ReceiveText extends Activity{
public void onCreate(Bundle savedInstanceState) {
// Somehow pass the string from the receiver into this activity,
//stored in variable messages
findOpen(messages);
// is that possible?
}
public class findOpen(string messages){
// do stuff ... open alert...open site if OK
}
So basically I just want to pass a string from a Broadcast Receiver to another activity that will use that string. The rest of the code is basically in place all I need is that string... I'm new to this and Java and any help would be much appreciated. Thanks
Instantiate a BroadcastReceiver in the activity you want to get your data to, for example:
private BroadcastReceiver mServiceReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent)
{
//Extract your data - better to use constants...
String IncomingSms=intent.getStringExtra("incomingSms");//
String phoneNumber=intent.getStringExtra("incomingPhoneNumber");
}
};
Unregister your receiver on onPause():
#Override
protected void onPause() {
super.onPause();
try {
if(mServiceReceiver != null){
unregisterReceiver(mServiceReceiver);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Register it on onResume():
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.SmsReceiver");
registerReceiver(mServiceReceiver , filter);
}
Broadcast your data from the service via an Intent, for Example:
Intent i = new Intent("android.intent.action.SmsReceiver").putExtra("incomingSms", message);
i.putExtra("incomingPhoneNumber", phoneNumber);
context.sendBroadcast(i);
and that's it! goodLuck!
If you have your activity named ReceiveText, then in your BroadcastReceiver, you should do the following:
Intent i = new Intent(context, ReceiveText.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("message", message.getMessageBody());
context.startActivity(i);
Then, in your activity, you will need to getExtra as so:
Intent intent = getIntent();
String message = intent.getStringExtra("message");
And then you will use message as you need.
If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="#android:style/Theme.Dialog" /> in your manifest for ReceiveText and then set the message to a textview in the activity.
EDIT: This restarts your activity. this answer is likely a better solution for most people.
We can send the data from onReceive to another activity using LocalBroadcastManager.
It means you are again broadcasting the data using the context
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Broadcast", "wifi ConnectivityReceiver");
Bundle extras = intent.getExtras();
Intent intent = new Intent("broadCastName");
// Data you need to pass to another activity
intent .putExtra("message", extras.getString(Config.MESSAGE_KEY));
context.sendBroadcast(intent );
}

Android Best Practice on Updating the UI from BroadcastReceiver to a certain activity

When i have a broadcastReceiver say android.intent.action.MEDIA_BUTTON and i want to update the current activity's UI without creating a new activity, is there any good practice on this one?
What i know (might not be correct)
1) I can put the BroadcastReceiver in the same class as the activity and call the updateUI function after certain activity
2) Create a ContentObserver?
3) Communicate to a service created by the activity, use aidl. (I dont know how to get the current service if its registered from an activity)
4) Create a custom filter on the broadcastReceiver located on the same class as the activity, and use context.sendBroadcast(msg of custom filter) and in the custom filter call updateUI (same as one but more generic?)
The final flow is it would come from a BroadcastReceiver and ends up updating the UI without renewing the activity (unless the activity is dead?)
Kindly provide links/source code on your how you tackle this kind of problem. Thanks a lot in advance :)
The easiest way to provide this functionality is to put the broadcast receiver in you Activity and bind / unbind it using registerReceiver and unregisterreceiver:
public class MyActivity extends Activity {
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
MyActivity.this.receivedBroadcast(intent);
}
};
#Override
public void onResume() {
super.onResume();
IntentFilter iff = new IntentFilter();
iff.addAction("android.intent.action.MEDIA_BUTTON");
// Put whatever message you want to receive as the action
this.registerReceiver(this.mBroadcastReceiver,iff);
}
#Override
public void onPause() {
super.onPause();
this.unregisterReceiver(this.mBroadcastReceiver);
}
private void receivedBroadcast(Intent i) {
// Put your receive handling code here
}
}
Depending on the intent you wish to receive, you may need to add the appropriate permissions to your AndroidManifest.xml file.
What I recently had to do to change a Button's text after receiving data from a LocalBroadcastManager is to store the value in a private field and then do the UI stuff in my onResume() method.
public class myClass extends Activity {
private String myString;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// register to receive data
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, new IntentFilter("myAction"));
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// get the extra data included in the intent
myString = intent.getStringExtra("myString");
}
};
#Override
public void onResume() {
super.onResume();
System.out.println("onResume");
// do something to the UI
myButton.setText(myString != null ? myString : "Default");
}
}

Categories

Resources