Android broadcast receiver not working with intent receiver - android

Hi I developed one small android application in which I am using one activity one intent service and one broadcast receiver.
So my code looks like :
public class Main_Activity extends Activity {
private ResultReceiver resultReciver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_);
Log.i("***************************88", "inside activity on create");
IntentFilter filter = new IntentFilter("com.nilkash.broadcast.receiver");
resultReciver = new ResultReceiver();
registerReceiver(resultReciver, filter);
//LocalBroadcastManager.getInstance(this).registerReceiver(resultReciver, filter);
Intent intent = new Intent(this, ExampleService.class);
startService(intent);
}
public class ResultReceiver extends BroadcastReceiver{
public ResultReceiver()
{
}
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.i("**********************", "inside broadcast receiver: ");
}
}
}
And intent service
public class ExampleService extends IntentService{
public ExampleService(String value)
{
super(value);
}
public ExampleService()
{
super("");
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Log.i("********************************", "inside intetn reciver: ");
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.nilkash.broadcast.receiver");
//broadcastIntent.putExtra("value", "nilkash");
sendBroadcast(intent);
//LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
In manifest file I define service.
So my problem is that I start service from activity and its working fine. From service on intent receive I sent one broadcast receiver but it not listening inside my broadcast receiver.
Am i doing some thing wrong? Need Help. Thank you.

There is an error: sendBroadcast(intent);. Should be another intent object (broadcastIntent).

Related

How can I check that whether a broadcast receiver is working or not?

Here is bootstartup.java file. I have checked that TestService is working properly. But I just want to know how can I check that data is broadcasted?
Code
public class bootstartup extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// start the service
Intent service=new Intent(context,TestService.class);
context.startService(service);
Log.e("com.example.myglobalbroadcastreceiver","Broadcast Received:"+intent.getAction());
// start the app
Intent app= new Intent(context,Abhijeet.class);
context.startService(app);
}
}

unable to send and receive Broadcast from activity :android

I am unable to send a Broadcast from one activity to other activity please see the code below. There are two buttons one for send Broadcast and other is for receiving Broadcast. I have tried following code. But my Receiver activity is running only when I click on checkBrodcast button.
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
sendBrodcast = (Button) findViewById(R.id.send_brodcast);
checkBrodcast = (Button) findViewById(R.id.check_brodcast);
sendBrodcast.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.w("Check", "inside send broadcast");
Intent broadcast = new Intent();
broadcast.setAction("BROADCAST_ACTION");
broadcast.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(broadcast);
}
});
checkBrodcast.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Receiver.class);
startActivity(intent);
}
});
}
}
public class Receiver extends Activity {
BroadcastReceiver br = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.w("Check", "Inside On Receiver");
Toast.makeText(getApplicationContext(), "received",
Toast.LENGTH_LONG).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter();
filter.addAction("BROADCAST_ACTION");
filter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(br, filter);
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(br);
}
}
The way you have Initiated the BroadCast is fine. You just need to change the way you intercept this Broadcast.
INITIATE A BROADCAST
Intent broadcast = new Intent();
broadcast.setAction("BROADCAST_ACTION");
broadcast.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(broadcast);
INTERCEPT IT
A) CREATE A RECEIVER
BroadcastReceiver br = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.w("Check", "Inside On Receiver");
Toast.makeText(getApplicationContext(), "received",
Toast.LENGTH_LONG).show();
}
};
B) REGISTER RECEIVER - Do this onCreate Activity Call Back
registerReceiver(br , new IntentFilter("BROADCAST_ACTION"));
Broadcast receivers registered this way(SINGLETON DECLARATION and NOT IN MANIFEST) - do not receive broadcasts unless the containing app is running. But as in your case you are firing a broadcast message onClick event, so the the app must be running. So I guess it is safe to assume that your receiver set up using this method, will work fine - provided the class in which you declared your receiver is created and exists in the activity stack, before you fire a broadcast from a different activity.
This is because you're not declaring your broadcast receiver in your Android Manifest. Dynamically registered receivers well not receive broadcasts unless the containing app is running.
If you want the second activity to get the broadcasts without having to click the button to start the app, then add the appropriate broadcast receiver to the second activities android manifest.
If you want to broadcast data from one activity to another, simply make use of Intent. In the light of your case simply call finish() in your onClickListener() and then in the onDestroy method of your second activity create Intent object and broadcast data as intent extra then, use intent.getExtra() method on onReceive() method of your broadcastReceiver class.
For more details:
follow this tutorial
The problem is with your manifest. You have to register your receiver in your Manifest like this:
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="com.android.mybroadcast" />
</intent-filter>
</receiver>
But anyway, personally, I don't like how you are building the broadcast receiver structure.
You should create a class that extendes from BroadcastReceiver like:
public class MyBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Your receiver!!!!.",
Toast.LENGTH_LONG).show();
}
}
Dont forget to set up your manifest:
<receiver android:name="MyBroadcastReceiver" >
</receiver>
And now the class from you are calling:
public class AlarmActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startAlert(View view)
{
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
}
}
Something like that!!!

BroadcastReceiver to start a service when device is turned on?

I can get the BroadcastReceiver to work but I'm not sure why the code for the intent to start the service has an error.
public class BroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent backGround = new Intent(this,BackGround.class);
startService(backGround);
}
}
The error's are with the new intent(this,BackGround.class) and startService(); now I think I need to implement the service class for the startService method and (this) needs to be something else but I'm not sure what to do here.
This should do it!
public class BroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, BackGround.class));
}
}
and don't forget to include service in manifest as well.

sending intent extras from service to activity

I have a service that listens for (ON_BATTERY_CHANGE), then onReceive service sends a Broadcast to My MainActivity. The problem is that I somehow can't get them from service to my main activity. Code: Main Activity:
public class MainActivity extends Activity
private BroadcastReceiver batteryReceiverService;
private TextView text2;
....
protected void onCreate(Bundle savedInstanceState) {
text2=(TextView)findViewById(R.id.TV_text2);
batteryReceiverService = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
text2.setText("left: "+intent.getStringExtra("H")+" hours "+intent.getStringExtra("M")+" minute(s)");
Log.e("text2","text2 HHH " +intent.getStringExtra("H")); //log shows 0
Log.e("text2","text2 MMM " +intent.getStringExtra("H")); // log shows 0
}
};
registerReceiver(batteryReceiverService, new IntentFilter(UltimateBatterySaverService.BROADCAST_ACTION));
....
#Override
protected void onDestroy() {
unregisterReceiver(batteryReceiverService);
super.onDestroy();
}
Service:
public class UltimateBatterySaverService extends Service {
private Intent intent;
static final String BROADCAST_ACTION = "lt.whitegroup.ultimatebatterysaver";
private BroadcastReceiver batteryLevelReceiver;
....
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
#Override
public void onDestroy() {
unregisterReceiver(batteryLevelReceiver);
super.onDestroy();
}
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
batteryLevelReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent){
// Receiving data, calculating and etc
averageChargingH=timeAllInHours;
averageChargingM=timeAllInMinutes;
// to put extras and send broadcast
does();
......
public void does(){
String strLong = Long.toString(averageChargingH);
String strLong2 = Long.toString(averageChargingM);
Log.e("cccccc","strLong h "+strLong); // getting good value not 0(everything ok)
Log.e("cccccc","strLong2 m"+strLong2); // getting good value not 0(everything ok)
intent.putExtra("H", strLong);
intent.putExtra("M", strLong2);
sendBroadcast(intent);
}
Any ideas why my information is not transfered correctly?
The does() method seems to be using variables in the same scope as onReceive so I'm guessing that the intent variable in does() is actually the Intent passed in from onReceive.
Try adding some logging before sending the broadcast to check if the action of the intent is correct, or simply create the broadcast intent in the onReceive method and name it intent2.

Service to Activity communication

I am trying to update the UI (Activity) after some action has been performed in the service. This is very simple example but it doesn't seem to work for me. What am I missing here?
ExampleService:
public class ExampleService extends IntentService{
#Override
protected void onHandleIntent(Intent intent) {
notifyActivity();
}
private void notifyActivity() {
Intent broadcast = new Intent(this, ExampleActivity.class);
sendBroadcast(broadcast);
}
}
ExampleActivity:
public class ExampleActivity extends ListActivity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
#Override
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
registerReceiver(receiver, filter);
}
}
You cannot send a broadcast to an anonymous dynamic receiver that way. You will need to define an action string in the Intent and use that action string in the IntentFilter.
You might consider using LocalBroadcastManager for this scenario, for better performance. Here is a sample project demonstrating this.

Categories

Resources