How to send data from Main Activity to BroadcastReceiver? - android

I know that exists thread about that, but I can't get how to do it, I only need send data a Broadcast, this is the code
Main activity
public void passData(){
passIntent = new Intent(this,Notification_Reciever.class);
path=String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath());
passIntent.putExtra("KEY",path);
sendBroadcast(passIntent);
}
BroadCast
public class Notification_Reciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
String value=intent.getStringExtra("KEY");
if(Constantes.ACTION.PLAY_ACTION.equals(action)){
actionPlay();
Toast.makeText(context,""+value,Toast.LENGTH_SHORT).show();
}
if(Constantes.ACTION.PREV_ACTION.equals(action)){
}
if(Constantes.ACTION.NEXT_ACTION.equals(action)){
}
}
Manifest
<receiver android:name=".Notification_Reciever">
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="PLAY_ACTION"/>
</intent-filter>
</receiver>
Always get null

Since you have intent-filter defined in manifest,
<intent-filter>
<action android:name="PLAY_ACTION"/>
</intent-filter>
You need to set action to your intent in order to receive data...
passIntent = new Intent(this,Notification_Reciever.class);
passIntent.setAction("PLAY_ACTION");
path = String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath());
passIntent.putExtra("KEY",path);
sendBroadcast(passIntent);

You have no set an action to the intent which you are broadcasting.
passIntent = new Intent("action");
In your case 'passIntent = new Intent(Constantes.ACTION.PREV_ACTION);` or whichever the action that you want to broadcast.
You can also use intent.setAction(action) to set an action.

you need to set action here.In manifest it is already registered
public void passData(){
passIntent = new Intent(this,Notification_Reciever.class);
passIntent.setAction("PLAY_ACTION"); path=String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath();passIntent.putExtra("KEY",path);
sendBroadcast(passIntent);
}

Here is an example of broadcast. do it like this
Intent intent = new Intent("IncomingChat");
intent.putExtra("visitor", String.valueOf(eventParameters[0]));
intent.putExtra("Action", "receivedincommingchats");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
and add this to the activity where you want to receive the broadcast
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(IncomingBroadcastReceiver, new IntentFilter("IncomingChat"));
}
private final BroadcastReceiver IncomingBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String Action = intent.getStringExtra("Action");
switch (Action){
case "receivedincommingchats":
String recVisit = intent.getStringExtra("visitor");
break;
default:
break;
}
}
};

Intent intent = new Intent();
intent.setAction("com.example.Broadcast");
intent.putExtra("data", "data");
sendBroadcast(intent);
your manifest file should have something like this ..
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="com.example.Broadcast" >
</action>
</intent-filter>
</receiver>
You have to register your broadcast receiver in your activity..
IntentFilter filter = new IntentFilter("com.example.Broadcast");
MyReceiver receiver = new MyReceiver();
registerReceiver(receiver, filter);
And your broadcast receiver will be like this
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Implement code here to be performed when
// broadcast is detected
}
}

Related

Limiting Broadcast Receiver for Fragment only

I have created a BroadCast Receiver to notify the GPS state as below :
public class GpsLocationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(context, "asdsadasdsaD", Toast.LENGTH_SHORT).show();
}
}
Receiver in Manifest as below :
<receiver android:name=".utility.GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Now, the issue is that What if I want to check gps state only in single Fragment ? Right now it broadcasting for overall app.
Thanks.
ReceiverActivity.java
A 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);
}
};
#Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
SenderActivity.java
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);
in OnResume() of your fragment write
LocalBroadcastManager.getInstance(context).registerReceiver(gpsChangeReceiver , new IntentFilter("android.location.PROVIDERS_CHANGED"));
An in onPause() of your fragment write
LocalBroadcastManager.getInstance(context).unregisterReceiver(gpsChangeReceiver );
At bottom of your fragment write
private BroadcastReceiver gpsChangeReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "asdsadasdsaD", Toast.LENGTH_SHORT).show();
}
};

Broadcastreceiver not receiving?

I am trying to pass values from service to activity using broadcast
I am using following code to call broadcast in service
Intent i = new Intent();
i.putExtra("test",result);
sendBroadcast(i);
And receiving in main activity using following code
public class myreciver extends BroadcastReceiver{
public String data =null;
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String datapassed = intent.getStringExtra("test");
}
}
In Main Activity
myreciver m = new myreciver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(m, intentFilter);
but my receiver is not called.
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pragadees.restex" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MainActivity$myreciver" >
</receiver>
<service
android:name=".MyIntentService"
android:exported="false" >
</service>
<service
android:name=".MyService"
android:enabled="true"
android:exported="false" >
</service>
<activity
android:name=".display"
android:label="#string/title_activity_display" >
</activity>
</application>
</manifest>
Action missing in Intent which is passing to sendBroadcast method.do it as:
Intent i = new Intent(MyService.MY_ACTION); //<< pass Action to Intent
i.putExtra("test",result);
sendBroadcast(i);
use broad cast like this
Intent i = new Intent("Broadcastname");
context.sendBroadcast(i);
and now receive broad cast like this way
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter intentFilter = new IntentFilter("Broadcastname");
BroadcastReceiver Receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
// to your work here
}
});
}
};
this.registerReceiver(Receiver, intentFilter);
finally unregister in onstop() method
#Override
protected void onStop() {
// TODO Auto-generated method stub
if (Receiver != null) {
this.unregisterReceiver(this.Receiver);
}
super.onStop();
}
Android's BroadcastReceiver is part of a framework that allows activities and services to send data to one another, even if they belong to separate apps. This is how apps share data with one another, such as when you share a picture from your gallery to Facebook or G+. However, this extensive capability means that you have to be careful about how you filter your requests, which means that it can be harder to just send a quick message from inside your own app.
If you don't need to worry about receiving data from other apps, then you can use the LocalBroadcastManager, which is an implementation of BroadcastReceiver that is confined inside of your own app's jurisdiction. It can't send or receive intents from outside your app. Its interface is nearly identical to BroadcastReceiver's:
public class MyActivity extends Activity {
private LocalBroadcastManager mBroadcastManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBroadcastManager = LocalBroadcastManager.getInstance(this);
//Build an intent filter so you only receive relevant intents
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Test from Service to Activity");
//Register a new BroadcastReceiver with the LocalBroadcastManager
mBroadcastManager.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String dataPassed = intent.getStringExtra("test");
}
}, intentFilter);
//If you ever want to send a broadcast, use this:
Intent sendIntent = new Intent(this, MyService.class);
sendIntent.setAction("Test from Activity to Service");
sendIntent.putExtra("test", "This is a test from Activity!");
mBroadcastManager.sendBroadcast(sendIntent);
}
}
//Then in your Service...
public class MyService extends Service {
private LocalBroadcastManager mBroadcastManager;
public void onCreate() {
mBroadcastManager = LocalBroadcastManger.getInstance(this);
}
public int onStartCommand(Intent intent, int flags, int startId) {
//Build intent filter
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Test from Activity to Service");
mBroadcastManger.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String dataPassed = intent.getStringExtra("test");
}
}, intentFilter);
//To send data to the activity:
Intent sendIntent = new Intent(this, MyActivity.class);
sendIntent.setAction("Test from Service to Activity");
sendIntent.putExtra("test", "This is a test from Service!");
mBroadcastManager.sendBroadcast(sendIntent);
}
}

Listening for ACTION_DREAMING_STOPPED

How would I get my app to listen for when DayDream stops. When the system stops dreaming it sends the ACTION_DREAMING_STOPPED string out. I have added a BroadcastReceiver in my OnResume and onCreate and neither are used when DayDream stops. So where should I put my listener? I do apologize if I am calling something by its wrong name, I haven't worked with DayDream before.
#Override
protected void onResume() {
mDreamingBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
// Resume the fragment as soon as the dreaming has
// stopped
Intent intent1 = new Intent(MainActivity.this, MainWelcome.class);
startActivity(intent1);
}
}
};
super.onResume();
}
The BroadcastReceiver can be created in your onCreate.
Ensure you register the receiver with: registerReceiver(receiver, filter) and that you've got the intent-filter inside your AndroidManifest.xml.
Sample:
MainActivity.java
public class MainActivity extends Activity
{
private static final String TAG = MainActivity.class.toString();
private BroadcastReceiver receiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, TAG + " received broacast intent: " + intent);
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
Log.d(TAG, "received dream stopped");
}
}
};
filter = new IntentFilter("android.intent.action.DREAMING_STOPPED");
super.registerReceiver(receiver, filter);
}
}
AndroidManifest.xml
<activity
android:name="com.daydreamtester.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.DREAMING_STOPPED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

registerReceiver Does not work

I'm trying to deal with push notifications in my main class (and i also have GCMBroadcastReceiver - for all the notifications that comes when i'm not running the main class)
but the registerReceiver Does not work
(GCMBroadcasrReceiver works fine)
my code:
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(mHandleMessageReceiver, new IntentFilter("com.google.android.c2dm.intent.RECEIVE"));
}
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BroadcastReceiver","Working");
}
};
}
Manifest:
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
*Works fine only in my 4.1.2 (S3)
Well, found the solution:
in my GCMIntentService.java i need to set sendBroadcast like so:
#Override
protected void onMessage(Context context, Intent intent) {
Intent i = new Intent("com.my.app.DISPLAY_PUSH");
i.putExtra("msg", intent.getExtras().getString("msg"));
context.sendBroadcast(i);
}
and the BroadcastReceiver should be
protected void onCreate(Bundle savedInstanceState) {
registerReceiver(mHandleMessageReceiver, new IntentFilter("com.my.app.DISPLAY_PUSH"));
}
.
.
.
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BroadcastReceiver","Working with msg:" + intent.getExtras().getString("msg") );
}
};
I wonder why it works in 4.1.2 without the sendBroadcast...
if you call sendBroadcast like this
Intent intent = new Intent(context, mBroadcastReceiver.getClass());
intent.setAction(ACTION_ON_CLICK);
context.sendBroadcast(intent);
// or
Intent intent = new Intent(context, MyBroadcastReceiver.class);
intent.setAction(ACTION_ON_CLICK);
context.sendBroadcast(intent);
change it to this :
Intent intent = new Intent(ACTION_ON_CLICK);
context.sendBroadcast(intent);

How to send and receive broadcast message

I am trying to pass data between two activities that are inside of tabs. I am trying to use sendBroadcast(). With breakpoints set I never reach onReceive().
Manifest:
<activity
android:name=".WebResults"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.toxy.LOAD_URL" />
</intent-filter>
</activity>
Activity Sender:
Intent intent=new Intent(getApplicationContext(),WebResults.class);
intent.setAction("com.toxy.LOAD_URL");
intent.putExtra("url",uri.toString());
sendBroadcast(intent);
Activity Receiver :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter("com.toxy.LOAD_URL");
this.registerReceiver(new Receiver(), filter);
}
private class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
String url = arg1.getExtras().getString("url");
WebView webview =(WebView)findViewById(R.id.webView);
webview.loadUrl(url);
}
}
I was having the same problem as you, but I figured out:
Remove the intent filter from the manifest and change
Intent intent=new Intent(getApplicationContext(),WebResults.class);
for
Intent intent=new Intent();
Hope it helps!
Please use
intent.getStringExtra("");
and
new Intent();
Worked for me.
You can do like this
Intent intent = new Intent("msg"); //action: "msg"
intent.setPackage(getPackageName());
intent.putExtra("message", message.getBody());
getApplicationContext().sendBroadcast(intent);
Then for receiving write something like this (inside Activity)
#Override
protected void onResume() {
super.onResume();
mBroadcastReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent){
/* Toast.makeText(context, "Message is: "+ intent.getStringExtra("message"), Toast.LENGTH_LONG)
.show();*/
String action = intent.getAction();
switch (action){
case "msg":
String mess = intent.getStringExtra("message");
txt.setText(mess);
break;
}
}
};
IntentFilter filter = new IntentFilter("msg");
registerReceiver(mBroadcastReceiver,filter);
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mBroadcastReceiver);
}

Categories

Resources