I am sending intent from "activity" to a receiver in "service" (and pass the data). My code has activity and service (that has reciever). Receiver is declared as follows
<receiver android:name="xxx"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<!-- protected intents meant for os and not for us <action android:name="android.intent.action.ACTION_NEW_OUTGOING_CALL" android:priority="0" /> -->
</intent-filter>
</receiver>
Activity is defined as follows
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I reviewed
Use an intent to send data to my activity
Intent I am invoking is the call intent and passing the destination call number, with the following code
Log.e(TAG,"Calling "+number);
Intent callIntent = new Intent(Intent.ACTION_CALL); //ACTION_NEW_OUTGOING_CALL is deprecated in API 21, hence ACTION_CALL
callIntent.putExtra("PHONE_NUMBER",number);
number = "tel:"+number;
callIntent.setData(Uri.parse(number));
startActivity(callIntent);
Above code successfully makes a telephone call from my app. I also have a receiver to intercept the calls and the reciever intercepts the above call just fine. However 'extras' of above intent is missing in the receiver; I always get "PHONE_NUMBER" as null in the following code
#Override
public void onReceive(Context context, Intent intent) {
//blah blah..
savedNumber = intent.getExtras().getString("PHONE_NUMBER");
if(savedNumber == null)
savedNumber = intent.getStringExtra("PHONE_NUMBER");
Log.e(TAG, " savedNumber = "+savedNumber);
}
What is my mistake and why is that I get the intent in the reciever but the 'extras' is missing (as you may have noticed, I tried to get it both ways from intent)
Try this:
Intent intent = context.getIntent();
savedNumber = intent.getStringExtra("PHONE_NUMBER");
getIntent() is method of Activity class. You can see in the onReceive() method has an intent argument, you get string from this.
String number = null;
number = intent.getStringExtra("PHONE_NUMBER");
But i read on this article: How to pass Extra to BroadcastReceiver, when initiating ACTION_CALL
Only the Android system itself can broadcast the NEW_OUTGOING_CALL Intent.
You can't add your own extras to this Intent. You'll need to come up with another way to do whatever it is you are trying to accomplish.
Start service like this-
Intent callIntent=new Intent(this, Service.class);
callIntent.putExtra("phonenumber",number);
this.startService(callIntent);
Then retrieve data from the service;
data=(String) intent.getExtras().get("phonenumber");
You can access your parameter from either the onHandleIntent or onStartCommand Intent parameter.
Service
protected void onStartCommand (Intent intent, int flags, int startId) {
data=(String) intent.getExtras().get("data");
}
IntentService
protected void onHandleIntent(Intent intent) {
data=(String) intent.getExtras().get("data");
}
It depends on which type of service you are running.
Related
In my application i have to use a BroadcastReceiver which must run in it's own process.
<receiver
android:name="com.greenroad.mobile.asimov.tiles.AsimoveTileRequestUpdateReceiver"
android:process="com.zonarsystems.Sample2020App.tile" >
<intent-filter>
<action android:name="com.zonarsystems.twenty20.tile.intent.action.TILE_REQUEST_UPDATE" />
</intent-filter>
</receiver>
The application sending data to this receiver in order to process them.
for calling this receiver the application using
Intent intent = new Intent("com.zonarsystems.twenty20.tile.intent.action.TILE_REQUEST_UPDATE");
intent.putExtras(indicationsBundle);
sendBroadcast(intent);
But i get nohing in
#Override
public void onReceive(Context context, Intent intent) { ... }
How it can be solved?
Thanks,
Eyal.
I want to trigger an application, B from application, A.
To achieve this i wrote the following in A
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.somepackage.appb");
intent.putExtra("secret", "message");
startActivity(intent);
However this opens up the application B which is not desired.
Please suggest a work around to avoid B from opening up and receive the data in background from application A.
Write a BroadcastReceiver in application B and declare it in your manifest.
In application A, craft an intent with extras to target that receiver, and call sendBroadcast with that intent.
Application B:
Manifest
<application>
...
<receiver android:name=".IncomingReceiver" android:enabled="true">
<intent-filter>
<action android:name="jason.wei.custom.intent.action.TEST"></action>
</intent-filter>
</receiver>
</application>
IncomingReceiver.java
public class IncomingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST";
if (intent.getAction().equals(CUSTOM_INTENT)) {
System.out.println("GOT THE INTENT");
Toast.makeText(context, "GOT THE INTENT", Toast.LENGTH_LONG).show();
}
}
}
Application A:
String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST";
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
sendBroadcast(i);
You can use SharedPreferences.
As you can see there are many ways to do this...myself i prefer creating a service which can update data without interacting with app B
I'm trying to recognize incoming calls in thru a broadcast receiver. I'm UNABLE to do so! Infact, I'm unable to 'trigger' the broadcast!
Here's my code:
activate.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Clicked",1).show();
final String BROADCAST_ACTION_NAME = ".BroadcastMM";
Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION_NAME);
sendBroadcast(intent);
}
}
I dunno if this 'sendBroadcast' is ever triggered! In my Broadcast Receiver file:
public void onReceive(Context context, Intent intent)
{
if(intent.getAction()=="android.intent.action.PHONE_STATE"){
Toast.makeText(c,"BroadCast fired!",1).show();}
Bundle extras = intent.getExtras();
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Ringing", 1).show();
}
}
My manifest file:
<receiver android:name=".BroadcastMM" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Is there some logic I'm missing? I'm very new to Android, so please help me out.
intent.getAction()=="android.intent.action.PHONE_STATE"
should be
TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction());
Since this is how you compare Strings (with equals()).
Also, the code you use to broadcast, should never broadcast - there is no ".BroadcastMM" action. Try making an explicit one instead:
Intent intent = new Intent(v.getContext(),BroadcastMM.class);
sendBroadcast(intent);
It is also likely that you can't broadcast android.intent.action.PHONE_STATE, so your if won't be executed if you make an explicit Intent.
If you really want to check that your BroadcastReceiver is working, put printouts/Toasts outside all ifs. Then once you establish that the BroadcastReceiver responds, do your check. Keep in mind though, that since you only listen for one Intent-Filter, the if checking if the Intent is a PHONE_STATE Intent is a bit redundant.
I'm initiating new call from my activity. And trying to pass a boolean extra.
public class CallInitiatingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
intent.putExtra("com.demoapp.CUSTOM_CALL", true);
startActivity(intent);
}
}
I also have a BroadcastReceiver registered, which listens for outgoing calls:
<receiver android:name=".OutgoingCallListener" android:exported="true" >
<intent-filter android:priority="0" >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Basically what I'm expecting onReceive to see my extra, but somehow it is not passed:
public class OutgoingCallListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
for (String key : extras.keySet()) {
Log.i(Constant.LOG_TAG, key + ": " + extras.get(key));
}
}
}
Output:
android.phone.extra.ALREADY_CALLED:false
android.intent.extra.PHONE_NUMBER:+370652xxxxx
com.htc.calendar.event_uri:null
android.phone.extra.ORIGINAL_URI:tel:+370652xxxxx
Your custom extra is present in the Intent that you use to start the "call" activity. But it isn't copied into the NEW_OUTGOING_CALL Intent that is broadcast as a part of the actual call mechanism. These 2 operations are distinct and only indirectly related to each other. Only the Android system itself can broadcast the NEW_OUTGOING_CALL Intent.
You can't add your own extras to this Intent. You'll need to come up with another way to do whatever it is you are trying to accomplish.
I have an activity. It contains a button whose text changes dynamically. I would like to pass this text to my broadcast receiver which receives the sms. Now my broadcast receiver should receive the text and based on the text it should start or stop a service. How to do this?
if your BroadcastReceiver is defined in a separate class file, then you may simply broadcast the value to that receiver. Once the value is received, do the magic for service by using receiver's context
Update:
in your activity:
Intent in = new Intent("my.action.string");
in.putExtra("state", "activated");
sendBroadcast(in);
in your receiver:
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Receiver", "Broadcast received: " + action);
if(action.equals("my.action.string")){
String state = intent.getExtras().getString("state");
//do your stuff
}
}
in manifest xml:
<receiver android:name=".YourBroadcastReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="my.action.string" />
<!-- and some more actions if you want -->
</intent-filter>
</receiver>
You can have your activity send an intent to the receiver, and pass the text as an extra
Intent i= new Intent(this, YourReceiver.class);
i.putExtra("txt", "the string value");
startActivity(i)
And then in your receiver, start the service using the startService function