Not receiving Extras from sendBroadcast - android

I have a MainClass that registers a BroadcastReceiver and overrides its onReceive() method
BroadcastReceiver broadcastReceiver=new BroadcastReceiver(){
#Override
public void onReceive(Context context,Intent intent){
Log.w("MyApp","RICEVUTO MESSAGGIO BROADCAST: "+intent.getStringExtra("com.example.avis.NUOVANEWS"));
Log.w("MyApp","Intent TOSTRING "+intent.toString());
}
};
this.getApplicationContext().registerReceiver(broadcastReceiver, new IntentFilter("nuovanewsavis"));
In the class NewsService, when a new news is found, i broadcast a new message. Here's the code
Intent intento=new Intent("nuovanewsavis");
intent.setAction("nuovanewsavis");
intent.putExtra("com.example.avis.NUOVANEWS", "Nuova notizia");
this.sendBroadcast(intento);
The MainClass cannot get the extra: if i try to read it through getStringExtra i get a null.
Am i doing something wrong?

I think there has a typo...your using intent instead of intento when putting extra.
Intent intento = new Intent("nuovanewsavis");
intento.setAction("nuovanewsavis");
intento.putExtra("com.example.avis.NUOVANEWS", "Nuova notizia");
this.sendBroadcast(intento);

Related

Android Change a variable in service from other app

the title says all, I need to change the variable of my service from a activity in my other app , what to finalize the service or not, this is possible?
I found the Message object , but I do not quite understand
The simplest solution would be to implement a BroadcastReceiver. Your Service listens for the Broadcast and the other App sends the Broadcast.
Example Reciever:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Get bundle from intent and use it to set your Variable in your Service
}
}
Example Broadcaster (courtesy of Vogella):
Intent intent = new Intent();
intent.setAction("de.vogella.android.mybroadcast");
sendBroadcast(intent);

How to pass data in install data intent and get it installed Broadcast Receiver

Hi I am looking a way to send any data in install intent & get this data in install/replace broadcast reciever.
I am doing install intent like below
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(
Uri.parse("file:///" + Environment.getExternalStorageDirectory() + "/test.apk"), "application/vnd.android.package-archive");
promptInstall.putExtra("data", "value");
startActivity(promptInstall);
In Install Broadcast Receiver.
public class NewPackageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("DEBUG"," test for application install/uninstall");
}
}
Question
I sent promptInstall.putExtra("data", "value"); in install intent then How I can get this data value in the install broadcast receiver.
Thanks in advance.
You can't. The extra data you add to your install Intent is not included in the PACKAGE_ADDED Intent sent by the system when a package was added.
What i did to achieved this is simply by creating a public method in the BroadcastReceiver, example :
public static void setAlarms(Context context)
And then i called that method in the actvitiy directly, and call it in the onReceive :
#Override
public void onReceive(Context context, Intent intent) {
setAlarms(context);
}

Send integer value using Broadcast receiver in Android

How to pass a integer value from Service class using a Broadcast intent to a Activity. Below is my code but getting some errors,
Sending side
private void DisplayLoggingInfo() {
Log.d(TAG, "entered DisplayLoggingInfo");
int dataJNI = BioLIb.intFromJNI();
intent.putExtra("mykey", dataJNI);
sendBroadcast(intent);
}
Receiving side
private Intent intent;
the above is a global variable. In my onCreate I will call BroadcastReceiver class like below.
intent = new Intent(this, BroadCastService.class);
onReceive of BroadcastReceiver i will call
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
int time = intent.getINtExtra("mykey", 1);
But I am not getting my exact value of dataJNI in a receiving side. I am always getting value is 1, How to get resolve this.
When you're sending you're putting an integer into the Intent Bundle. When receiving you0re trying to extract a long. Did you try using the same type in both sides?
use the intent that you received as a parameter of onReceive method
int time;
public void onReceive(Context context, Intent intent) {
updateUI(intent);
time = intent.getLongExtra("mykey", 1);
}

calling a method inside MainActivity from GCM onMessage()

i want when a new message received by onMessage() inside GCMIntentService.java , the onMessage() call a method called blinkLED() inside the MainActivity.java so the blinkLED() method can use the data received by onMessage() , how can i implement that ? a sample code will be helpful.
Use BroadcastReceiver. This tutorial shows you how to send a broadcast intent from a class, and have another class handles it.
Short example, in your GCMIntentService::onMessage(), you may have this:
Intent intent = new Intent();
intent.setAction("com.my.app.blinkled");
sendBroadcast(intent);
Then in your MainActivity, you implements a BroadcastReceiver :
private class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
blinkLED();
}
}
and register for it in onResume() of MainActivity:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.my.app.blinkled");
receiver = new MyBroadcastReceiver();
registerReceiver(receiver, intentFilter);

addAction() & Service calling Dilemma

I'm stuck here at my previous struggle >> Prev. Struggle!
Raanan there helped! me a lot but then he I think went away as timing zone is different , now I'm stuck with my service code that I'm using to call my BroadcastReceiver() that is in the activity! and also I'm not getting with what parameter I should load the filter.addAction(action); in place of action??
Kinldy guide me!
CODE in the Server:
Toast.makeText(Server.this, hr +" , " +min, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, andRHOME.class);
//intent.putExtra("sendMessage","1");
sendBroadcast(intent);
and CODE IN THE ACITIVITY(Broadcast Receiver)
private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "IN DA BroadCASTER",
Toast.LENGTH_LONG).show();
sendMessage("1");
}
};
IntentFilter filter = new IntentFilter();
You need to add these line to regiester your receiver for some action for example define a Global variable like this:
public static String NOTIFCATION_BROADCAST_ACTION = "com.your_packagename.UPDATE_NOTIFICATION_INTENT";
then register the action like this in your activity onCreate() Method.
IntentFilter filter = new IntentFilter();
filter.addAction(Global.NOTIFCATION_BROADCAST_ACTION);
registerReceiver(ReceivefrmSERVICE, filter);
Then send the broadcast from your service like this
Intent broadcast = new Intent();
broadcast.setAction(Global.NOTIFCATION_BROADCAST_ACTION);
sendBroadcast(broadcast);
Then in your broadcast Receiver filter this action like this
private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Global.NOTIFCATION_BROADCAST_ACTION)) {
//Do your stuff here :)
}
}
};

Categories

Resources