Save incoming number in a variable to use in the Activity - android

I was looking at the BroadcastReceiver and Activity and I didn´t find a right mode to use the incoming number that I have from the BroadcastReceiver in my Activity. This is the code that I use to intercept the incoming number and visualize in a Toast:
public class CustomBroadcastReceiver extends BroadcastReceiver {
String ophoneNumber;
#Override
public void onReceive(Context context, Intent incoming) {
Bundle bundle = incoming.getExtras();
ophoneNumber= bundle.getString("incoming_number");
Toast.makeText(context, ophoneNumber, Toast.LENGTH_LONG).show();
}
}
Manifest part:
<receiver android:name=".CustomBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
My problem is that I have to use the incoming number in an Activity that is running, so I have also read about the possibility to put the BroadcastReceiver inside the Activity like this:
public void monitorIncomingCalls(){
INcall = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
final Bundle extras = intent.getExtras();
if(intent.getAction().equalsTelephonyManager.ACTION_PHONE_STATE_CHANGED))
inphoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
};
registerReceiver(INcall, new IntentFilter(Intent.ACTION_ANSWER));
}
But with this I don´t have nothing in the inphoneNumber variable, I think because I can´t register correctly the BroadcastReceiver because I miss the permission.
I didn´t find in the web an example that is working for what I want, that is basically put in a variable in my Activity the number that is calling me.

Retrieve incoming call's phone number in Android
Check out this link to save the incoming number.
Extend PhoneStateListener
Extend BroadCastReceiver
Display the toast.

Related

Set InnerClass broadcastreceiver to android action

This is what I am using right now:
in the onCreate method:
registerReceiver(bootup, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
the bootup receiver:
BroadcastReceiver bootup = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("test", "received");
}
};
These are both in the MainActivity class of my app. What I want is for the bootup receiver to be called when the bootup of the phone is complete. I need it in the MainActivity class because I need to access a few things from it.
How would I set this? My current solution does not work.
EDIT: The posted solution seems as though it will work for my purpose, and this is what has been suggested by other threads. However, when I put a log statement in the receiver that it ties to, nothing appears in the console. Is this because the application is not running? I have also tried sending a notification with notificationmanager
EDIT 2: I took advice and switched to a broadcastreceiver in a another class for detecting the reboot. It works by simply changing the name attribute on the manifest file. This being the reason why I cant use an inner class receiver doesn't really make any sense compared to what I have seen on other questions. Can someone explain why I can't point the receiver to the inner class one and why I have to use a separate class?
Create a Receiver:
public class BootupReceiver extends BroadcastReceiver {
private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
if (ACTION_BOOT.equals(intent.getAction()))
Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show();
}
}
And receiver and uses-permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.restarttest.BootupReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

Have BroadcastReceiver run as Service in the background + Auto Start after boot

I'm very new to Android and Programming in general, so I'm playing around with different
tutorials and info gathered here on stackoverflow.
What I would like to accomplish, is having the app with my SMS BroadcastReceiver run as a service, so I can get all the SMS broadcasts when app is in the background.
Also, how can I add a BroadcastReceiver for receiving broadcast of BOOT_COMPLETED and start app automatically?
Would I need several services for this, or is 1 service sufficient? (for detecting SMS + BOOT_COMPLETED continuously)
Currently I have a created a BroadcastReceiver for getting SMS, like this;
public class SMS extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
.. etc ..
.. etc ..
}
and my AndroidManifest.xml file has receiver and intent-filter with the
additional android.provider.Telephony.SMS_RECEIVED
Getting the SMS broadcast works fine, but I'm not sure where to go from here.
All help is much appreciated :)
Thanks.
To start your service on BOOT_COMPLETED event and to receive SMS intent continuously.
AndroidManifest.xml:
<receiver android:name="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
BootReceiver.java:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, SMSService.class);
context.startService(service);
}
}
SMSService.java:
public class SMSService extends IntentService {
#Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
if (Intent.BOOT_COMPLETED.equals(action)) {
//write your code to process BOOT_COMPLETED intent here
}
else if(Intent.SMS_RECEIVED.equals(action)) {
//Write your code for processing SMS intent here
}
}
}
As, Fildor has pointed out, it is unnecessary to start service on BOOT_COMPLETED intent. InentService would do the work. So, above two code snippets are not required. Just the last snippet would do the work.

How can i get recevier instance(which is registered in AndroidManifest.xml) in activity

I've register a receiver in AndroidManifest.xml like this
<receiver android:name="com.sunrise.taximate.message.MessageRecevier">
<intent-filter>
<action android:name="xxx.xxxx.xxx.xxx" />
</intent-filter>
</receiver>
and now I want to get the receiver's instance in one of my activities(Like MainActivity),but I don't know how to. anyone can help me?
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
// Your code here to do what ever you want
}
}
Receivers are meant to act on events generated by the system and sometimes the users. There are special cases where you might want to get an instance of those yourself but this is uncommon. The whole point of having receivers is to react to system events and take some action. Unless you know what you are doing, I would recommend against creating receiver instances yourself in an activity.
If you really want to, you can do it like this
private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
// do stuff
}
}
Also see this thread for related info:
BroadcastReceiver as inner class

How to detect incoming call with the help of Broadcast Receiver?

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.

Start/Stop Receiver manually and pass parameters to receiver

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);
}

Categories

Resources