How to send data from BroadcastReceiver to Fragment in android - android

I am trying to make an chatting app.I have a SlidingDrawer Activity and it has many fragments and among which ChatFragment is one.Now when i am sending message to my friend , i have a Chatting Window and if any message comes from GCM service and i am in ChatFragment then this message will go to that Fragment and update the listview as i want to update the chatWindow when any message comes while chatting Now I tried to do like below.
GcmIntentService :
public class GcmIntentService extends IntentService {
public GcmIntentService() {
super("GcmIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (extras != null && !extras.isEmpty()) { // has effect of unparcelling Bundle
// Since we're not using two way messaging, this is all we really to check for
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Logger.getLogger("GCM_RECEIVED").log(Level.INFO, extras.toString());
showToast(extras.getString("message"));
Intent sendData = new Intent("chatupdater");
sendData.putExtra("msg", extras.getString("message"));
LocalBroadcastManager.getInstance(this).sendBroadcast(sendData);
Log.i("chat","i am in GCMIntentService");
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
Here i am starting a broadcastreceiver in onHandleIntent().
ChatBroadCastReceiver:
public class ChatBroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("chat","I am in ChatBroadCastReceiver");
String msg = intent.getStringExtra("msg");
Log.i("chat",msg);
//Intent data = new Intent("chatupdater");
//Intent data = new Intent("chatupdater");
//data.putExtra("key","data");
//data.putInt("fragmentno",1); // Pass the unique id of fragment we talked abt earlier
//context.sendBroadcast(intent);
}
}
This is my ChatBroadCastReceiver class and if any message comes it successfully receives at the onReceive() method.Now i want to send this message to the Fragment.What i tried , i registered the Fragment with it and tried to get the same data at the onReceive() of ChatFragment.But it didn't call.I tried to see by logging.
public class ChatFragment extends Fragment {
//ChatBroadCastReceiver mReceiver;
private EditText et_chat;
Bundle mailData;
String caller_mail;
private ListView chatListview;
private ChatWindowAdapter chatWindowAdapter;
private List<ChatSession>PreviousChatSession;
private List<ChatInfo> chatListItems;
Button chat_send;
public ChatFragment() {
}
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("chat","I am in BroadCastReceiver");
String msg = intent.getStringExtra("msg");
Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReceiver = new ChatBroadCastReceiver();
//getActivity().registerReceiver(new ChatBroadCastReceiver(),new IntentFilter("chatupdater"));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(new ChatBroadCastReceiver(),new IntentFilter("chatupdater"));
Log.i("chat", "I am in onCreate");
}
Now how can i get the message which i got in the Broadcastreceiver to the onReceive() of ChatFragment??

Try this
Create an interface which will have a method like this.
public interface DemoListener {
public void receiveMessage(String message);
}
Now implement this in your fragment and register the listener in BroadcastReceiver, now as soon as your broadcast receiver receives any message it will be available to your fragment via this Listener.Hope this helps.

To Register your Broadcast You are using:
mReceiver = new ChatBroadCastReceiver();
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver ,new IntentFilter("chatupdater"));
So you will Only receive Broadcast on ChatBroadCastReceiver class. Because mReceiver is a Instance of ChatBroadCastReceiver class.
Now to Receive broadcast Message on anonymous class that you have created in your ChatFragment you need to register that like below code. Where mReceiver is a Instance of anonymous BroadcastReceiver class that you have implemented in your ChatFragment class.
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver ,new IntentFilter("chatupdater"))
To Receive Broadcast Message Both on ChatFragment and ChatBroadCastReceiver you need to register broadcast receiver twice.
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver ,new IntentFilter("chatupdater"))
ChatBroadCastReceiver mChatBroadCastReceiver = new ChatBroadCastReceiver();
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mChatBroadCastReceiver ,new IntentFilter("chatupdater"))
Hope this answer will help you to understand your problem.

Related

pass an integer to Broadcast receiver

I have a service that subscribes from the server by Mqtt client. when arrived a message I do broadcast message and topic. In my fragment I declare a broadcast receiver like this:
private final BroadcastReceiver mChatReceiver = new BroadcastReceiver() {
int areaCode;
private BroadcastReceiver init(int areaCode) {
Log.i("====>", "init: BroadcastReceiver ");
this.areaCode = areaCode;
return this;
}
#Override
public void onReceive(Context context, Intent intent) {
Log.i("====>", "onReceive: BroadcastReceiver ");
//do sth
}
}.init(areaCode);
but init(areaCode) do not work and in original areaCode is for example 2 but I did not get 2 in private BroadcastReceiver init(int areaCode). I got 0.
how can I pass an integer out of private final BroadcastReceiver mChatReceiver class to this class?
Say you are registering for some custom IntentFilter like this:
IntentFilter filter = new IntentFilter("com.packageName.ACTION_SEND_INTEGER");
Register receiver like this:
your_context.registerReceiver(mChatReceiver,filter);
Trigger your broadcast receiver like this and send an integer:
Intent intent = new Intent("com.packageName.ACTION_SEND_INTEGER");
intent.putExtra("your_value",123);
sendBroadcast(intent);

creating an Async task inside a service

I am trying to implement an Async task that gets a string from a url inside a service.
I am using a startedService which calls the Async task get the correct string, update a public DB class content and return to the main activity, the problem is that the list adapter which i need to notify of the change in the DB is at the main activity and i don't have access to it from the Service , I am a a noobie so I am not familiar with what better to use , started or bind service for that job, any sugestions ?
thank you
You can use BroadcastReceiver :
In your Activity:
#Override
public void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("mybroadcast"));
}
// handler for received Intents for the "my-event" event
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
#Override
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
And to Broadcast from service use:
Intent intent = new Intent();
intent.setAction("mybroadcast");
sendBroadcast(intent)

Modify a TextView from multiple classes frequently

I'm looking to make my own debug Activity and then update it from various other classes (not just a single class like the other questions I've seen here) using THE LEAST amount of code in my other classes. So ex:
// activity class
public class DebugActivity extends Activity {
public TextView txtView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView txtView = (TextView)findViewById(R.id.text);
}
public someMethod(String text) {
//Update text view code here.
}
// A class updating the text view
public class Some other class {
someOtherMethod {
DebugActivity.someMethod(updatedTextViewSTring);
}
}
The easiest way that i can think of is by sending the data (text) to your DebugActivity. You need to use this code in each activity that work with DebugActivity:
Intent i = new Intent(this, DebugActivity.class);
i.putExtra("text", "some text");
startActivity(i);
And accept that data(text) in the onCreate of DebugActivity :
Bundle b = getIntent().getExtras();
if(b!=null)
String text = b.getString("text");
Note : Theres no use to change the textview in an Activity in realtime because only one activity is displayed at a time.
Elegant way to do is use EventBus lib https://github.com/greenrobot/EventBus
you need to write code like below in DebugActivity
EventBus.getDefault().register(this);
public void onEventMainThread(TextEvent textEvent)
{
txtView.setText(textEvent.getText());
}
after that you can send textevent from anywhere in App like below
eventBus.post(new TextEvent("my Message"));
Or
Pure Android way is use Broadcast / Handler
Here is how to use broadcast
An activity that watches for notifications for the event named "custom-event-name".
#Override
public void onCreate(Bundle savedInstanceState)
{
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,new IntentFilter("custom-event-name"));
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
And here is how you will send message from anywere in app
// Send an Intent with an action named "custom-event-name". The Intent sent should
// be received by the ReceiverActivity.
private void sendMessage()
{
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("custom-event-name");
// You can also include some extra data.
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

send data from a broadcast to an activity . how to do send?

how to send data from a broadcatReceiver to an activity in android as saying catch the received SMS and send the SMS (is the data) to the activity its my first time that i work with broadcatReceiver so can anyone help me i will appreciate any help.
i read several tutorials about the data transferring that have some steps :
create a class that extends activity
create a class that extends broadcastReceiver
but i did not know how is the communication between these 2 classes.
I would do something like that:
public class YourActivity extends Activity
{
private Handler handler = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.main_layout);
this.handler = new Handler() {
#Override
public void handleMessage(Message msg)
{
SmsMessage sms = (SmsMessage) msg.obj;
String senderNumber = sms.getOriginatingAddress();
}
};
// Register a new receiver that will trigger on SMS_RECEIVED event
IntentFiler filer = new IntentFilert("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSmsReceiver, filter);
}
#Override
protected void onDestroy()
{
super.onDestroy();
// Unregister the receiver
unregisterReceiver(mSmsReceiver);
}
private mSmsReceiver = new BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle == null || bundle.containsKey("pdus") return;
// Decode the message
SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[0]);
// Notify the activity with the message
Message msg = new Message;
msg.obj = sms;
YourActivity.this.handler.sendMessage(msg);
}
};
}
The receiver and the Activity are separate entities and do not usually interact directly. Imagine the following scenario: you have a receiver getting lots of SMS messages, and every time you get one, you launch an Activity to show the message received. Wouldn't this be very annoying to the user?
I'd says that you can interact with the user by creating a Notification and if the user clicks on it, then you open the Activity you want to show the details.
As how to pass the data to the Activity (hopefully using a notification first), given that SMS messages are short in nature you can just put the data in the Intent.
How to set a Notification to launch an Activity: see Open application after clicking on Notification

How to send data to a running activity from Broadcast Receiver,

I am able to receive C2DM message fine but I want to send the data to a running activity, i.e when the activity is running, if the receiver receives C2DM message it is to send the data to the running activity. The code of receiver is (no bugs in the code):
public class C2dmreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Message Receiver called");
if ("com.google.android.c2dm.intent.RECEIVE".equals(action))
{
final String payload = intent.getStringExtra("key1");
Log.d("C2DM", "message = " + payload );
}
}}
I have tried like this inside the activity in an attempt to register the receiver in the activity so that the receiver can send data and the running activity can receive the data :-
C2dmreceiver c2dmr = new C2dmreceiver();
Registration.this.registerReceiver(c2dmr, new IntentFilter());
I don't know what to put inside the IntentFilter(), also what else I have to put in the code of the activity and the code of the receiver so that while the activity is running and some C2DM message comes the receiver can send the data to the running activity.
So, please tell me the code that is to put in the activity and in the receiver and may also be in the manifest so that the data from the receiver could be send to running activity.
Any advice is highly appreciated.
First of all it's not the best idea to subscribe c2dm receiver in activity. Do it in manifest. For passing data to activity you can create static string field in Activity and set you String there.
You can do something like this:
in Activity
public static YourActivity mThis = null;
#Override
protected void onResume() {
super.onResume();
mThis = this;
}
#Override
protected void onPause() {
super.onPause();
mThis = null;
}
In your BroadcastReceiver:
#Override
public void onReceive(Context context, Intent intent) {
...
if (YourActivity.mThis != null) {
((TextView)YourActivity.mThis.findViewById(R.id.text)).setText("received c2dm");
}
else {
...
}

Categories

Resources