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.
Related
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.
Is there anyway to register a BroadcastReceiver in AndroidManifest.xml and receives broadcast that is send by a LocalBroadcastManager?
Currently I must call
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
to register a Receiver, declare in AndroidManifest.xml won't work. But this means I must know exactly the receiver's package name and class name, not just the intent filter. Is it possible to declare the receiver in the manifest file?
following is my current code.
AndroidManifest.xml:
...
<receiver
android:name="com.example.test.MessageReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.m2x.test.intent.MESSAGE_RECEIVED" />
</intent-filter>
</receiver>
...
MainActivity.java:
Intent intent = new Intent();
intent.setAction("com.m2x.test.intent.MESSAGE_RECEIVED");
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mContext.get());
manager.sendBroadcast(intent);
MessageReceiver.java
public class MessageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.m2x.test.intent.MESSAGE_RECEIVED")) {
Toast.makeText(context, "user message received", Toast.LENGTH_SHORT).show();
}
}
}
No, you can't.
The local BroadcastReceiver isn't a real BroadcastReceiver, basically its a list of callbacks functions.
You can check the source code of LocalBroadcastManager.java.
My project is simple. It has an activity and a broadcast receiver. From within my app I want to be able to send sms and to receive sms. This works great.
But now I want to pass some data from the view to the receiver. Imagine a simple checkbox, I want to pass its value to the receiver.
So this is the basic life cycle of my app:
Start app
Press Send SMS
Receiver is started with params and sms is send
Receiver gets an sms and stops.
Receiver:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Activity:
Receiver:
I tried to pass a value, but it seems to be ignored. Any ideas?
You cann't just start and stop a BroadcastReceiver at any time you want. BroadcastReceiver is only alive while onReceive() is executing.
To send a broadcast you should use sendBroadcast(this.service) instead of startService(this.service);
Once you change it to sendBroadcast(this.service) you will receive TWO broadcasts (first from your sendBroadcast() and second from SmsManager). This is definitely not what you want because in the first case you will be able to get your checked param but not SmsMessage and vice versa in the second case.
You can just store this checked param in the SharedPreferences and then retrieve it on onReceive()
Use Custom Intent Broadcasting to Achieve Current flow . make changes in your code as:
STEP 1 :
register an Custom Intent with SMS_RECEIVED in Manifest as :
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="xx.xxx.xxx.intent.action.SMS_STATUS_ACTION" />
</intent-filter>
</receiver>
STEP 2 :
public class SmsReceiver extends BroadcastReceiver {
public static final String STATUS_INTENT =
"xx.xxx.xxx.intent.action.SMS_STATUS_ACTION";
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction().equals(SmsReceiver.STATUS_INTENT)) {
// get value here sended from Activity
}
else{
// Check for SMS_RECEIVED Action here
}
}
}
STEP 3 :
send value from your Activity as using sendBroadcast :
public static final String STATUS_INTENT =
"xx.xxx.xxx.intent.action.SMS_STATUS_ACTION";
#Override
public void onClick(View v) {
int checked = 0;
if(this.param.isChecked()){
checked = 1;
}
// put value here
Intent intent = new Intent();
intent.putInt("param", checked);
intent.setAction(CUSTOM_INTENT);
sendBroadcast(intent);
}
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
I am trying to start up a BroadcastReceiver within a Service. What I am trying to do is have a background running service going that collects incoming text messages, and logs incoming phone calls. I figured the best way to go about this is to have a service running that incorporates a broadcast receiver that can catalog either.
How do i go about doing this? I already have my service up and running.
as your service is already setup, simply add a broadcast receiver in your service:
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
//action for sms received
}
else if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
//action for phone state changed
}
}
};
in your service's onCreate do this:
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
filter.addAction("your_action_strings"); //further more
filter.addAction("your_action_strings"); //further more
registerReceiver(receiver, filter);
and in your service's onDestroy:
unregisterReceiver(receiver);
and you are good to go to receive broadcast for what ever filters you mention in onCreate. Make sure to add any permission if required. for e.g.
<uses-permission android:name="android.permission.RECEIVE_SMS" />
The better pattern is to create a standalone BroadcastReceiver. This insures that your app can respond to the broadcast, whether or not the Service is running. In fact, using this pattern may remove the need for a constant-running Service altogether.
Register the BroadcastReceiver in your Manifest, and create a separate class/file for it.
Eg:
<receiver android:name=".FooReceiver" >
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
When the receiver runs, you simply pass an Intent (Bundle) to the Service, and respond to it in onStartCommand().
Eg:
public class FooReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// do your work quickly!
// then call context.startService();
}
}