PhatHV
PhatHV

Reputation: 8141

Passing data from Activity to BroadcastReceiver

I have an BroadcastReceiver which add prefix to phone number of outgoing call and prefix is add by user.

Are there any way to pass Prefix (String variable) to BroadcastReceiver?

I mean after my app is kill, this BroadcastReceiver still working with Prefix that user wanted to add.

This is my code for register BroadcastReceiver

PackageManager pm  = getApplicationContext().getPackageManager();
ComponentName componentName = new componentName(MyActivity.this,MyBroadcastReceiver.class);
pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);

Please help me regarding this.

Upvotes: 3

Views: 11043

Answers (5)

Rock
Rock

Reputation: 51

I got same problem, I register my broadcastreceiver in mainfest file, but don't know how to pass pre-fix number to my broadcast receiver. Anybody know how to do it?

Upvotes: 2

APriya
APriya

Reputation: 2004

intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("keyvalue",getmessage);

// getmessage is string value

in pending intent you have to use below line- PendingIntent.FLAG_CANCEL_CURRENT);

In broadcastreceiver

 String message = context.getStringExtra("keyvalue");

Using this i passing the data from activity to broadcast receiver.

Hope this helpful

Upvotes: 3

Nitesh Khosla
Nitesh Khosla

Reputation: 875

By using Intent, we can passing the data from activity to broadcast receiver.

intent.getExtras().get("testString");

Upvotes: 3

Galaxy S2
Galaxy S2

Reputation: 438

Through intent you can do like this -

Passing Class -

Intent i = new Intent(passing.this, received.class);
Bundle b = new Bundle();
b.putString("keyvalue", "yourprefixvalue");
i.putExtras(b);
startActivity(i);

Received Class -

In your broadcast receiver class contains onReceive method and having arguments intent. So that it can be used to get the result value from bundle.

@Override
public void onReceive(Context context, Intent intent) 
{
    String result = intent.getString("keyvalue");
    // your method
}

Try this out. I've passed some values to my BroadcastReceiver class like this.

Upvotes: 7

user1203673
user1203673

Reputation: 1015

Through intent u can pass the string value to the broadcast reciever

Upvotes: 2

Related Questions