3

I am trying to invoke a BroadcastReceiver from a service through intent.

I am calling BroadcastReceiver as follows in my service file :

    final Handler handler = new Handler();   
    final Runnable r = new Runnable()     {     
    public void run()                  {    

                // code here what ever is required  
                System.out.println("Runnnn");
                counter++;

                Intent i = new Intent();
                i.setAction("Refresh");
                Bundle b = new Bundle();
                b.putInt("Counter", counter);
                i.putExtra("Bundle", b);
                ctx.sendBroadcast(i);

                handler.postDelayed(this, 1000);     


                Toast.makeText(getApplicationContext(), "counter"+counter, Toast.LENGTH_LONG).show();



                }    

          };     
          handler.postDelayed(r, 1000);

onReceive() in BroadcastReceiver is as follows:

public void onReceive(Context context, Intent arg1) {
    System.out.println("OnReceiveeeeee");

    if(arg1.getAction().equalsIgnoreCase("Refresh"))
    {
        System.out.println("Received Intent");
        Bundle b = arg1.getExtras();
        c=b.getInt("Counter");
        System.out.println("Counter in Receiver:::"+c);
    }
}

But I am getting value in onReceive as zero. How can I get right value in onReceive() method?

halfer
  • 19,824
  • 17
  • 99
  • 186
Android_programmer_camera
  • 13,197
  • 21
  • 67
  • 81
  • I am doing something similar HERE!!! http://stackoverflow.com/questions/14571564/android-pendingintent-extras-not-received-by-broadcastreceiver/14612215#14612215 – Etienne Lawlor Jan 31 '13 at 07:53

2 Answers2

1

here's snippets of the code i use to broadcast a logout to prompt all my apps Activities to close when going back to the login screen

logoutBroadcastReceiver lbr;

@Override
public void onResume(){ 
...
// register the broadcast receiver
IntentFilter intentfilter = new IntentFilter("com.on3x.action.DO_LOGOUT");
lbr = new logoutBroadcastReceiver();
registerReceiver(lbr,intentfilter);
super.onResume();
...
}

// broadcast receiver grabbing the "test" bundled extra
    public class logoutBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(getString(R.string.app_name), "broadcast string: " + intent.getStringExtra("string_example"));
            Log.d(getString(R.string.app_name), "extra!: " + intent.getIntExtra("int_example",0));

            finish();
        }
    }   


// broadcast the intent to logout when logout button clicked
// put the extra "test" in the bundle
    public void onClickLogout(View _view) {
        Intent i = new Intent("com.on3x.action.DO_LOGOUT");
        i.putExtra("string_example", "here is a broadcasted string");
        i.putExtra("int_example", 100);
        sendBroadcast(i);
    }

I hope that code helps you out getting yours working?

Edit: updated to use putExtra() and getStringExtra()/getIntExtra()

wired00
  • 13,930
  • 7
  • 70
  • 73
  • Hi Android_programmer, after posting this i went and tested it actually works as epected with my code. It actually wasn't and was returning null. I changed broadcast to something like `i.putExtra("Counter", 100);` and receiver to `intent.getIntExtra("Counter", 0)` and now working perfectly. updating my full example now... – wired00 Apr 17 '11 at 11:46
1

You are accessing in wrong way.

Bundle b = arg1.getExtras();

You need to access as follow.

Bundle b = intent.getBundleExtra("Bundle");

================================================

You can alternatively write your code without using bundle also:

In Service

i.putExtra("Counter", counter);

In BroadcastReceiver

intent.getIntExtra("Counter", -1); // -1 is defalut value
Hagai L
  • 1,593
  • 1
  • 18
  • 40
Vivek
  • 4,170
  • 6
  • 36
  • 50