Adding the if condition quits the android app in telephony manager - java

I am trying to make a simple android app in which there is a toast message containing the number of the incoming call. I have stored the number in a string variable. When I display just the toast, it works fine but before the toast, if I add a simple if condition comparing the number to another string, the app quits. The required permissions are given. Can someone help me?
This Works:
public void onReceive(Context context, Intent intent) {
String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
}
This Does not Work (App quits)
public void onReceive(Context context, Intent intent) {
String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(incomingNumber.equals("+919999999999"))
{
Toast.makeText(context, "Call from Mom", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
}
}

It sounds a little silly but I just wrapped my "if" condition in a try-catch block and it solved the problem. I am not used to programming in JAVA so it took me a lot to figure out this simple thing. Thank You All for providing me with suggestions :)

Related

Android sending string from my application to any other applications

I have a service application. I need to send a string to any application (browser, word, ...wherever the keyboard cursor focused). How can I do this?
//onReceive of my service onStartCommand...
#Override
public void onReceive(Context context, Intent intent) {
String textToSend = intent.getStringExtra("data");
if(textToSend!=null && textToSend.length()>0) {
//Here I need send "textToSend" to another application
}else{
Toast.makeText(context, "Error! ", Toast.LENGTH_SHORT).show();
}
}
You can achieve it with Intents. Passing the string value in the URL, specifically: http://developer.android.com/training/sharing/send.html.
You can check this question for more info.

retry with varying intervals on each time

I am working on a ble-android communication app. I am getting TYPE_GATT_ERROR sometimes when I try to read values from ble device. So I like to retry the scan and reading values each time the TYPE_GATT_ERROR is seen. first time when it happens, I want to start the scan again after 30secs and if that time also if the code reaches TYPE_GATT_ERROR segment, It should start scan again after 1 min and third time if it reaches the error segment, It should rescan with time interval 2mins. How can I do that with the below code segment? The Elseif case with the TYPE_GATT_ERROR condition is where I want to implement the retry algorithm.
private final BroadcastReceiver bleServiceReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent){
String type = intent.getExtras().getString(
BleReceivedService.EXTRA_TYPE);
if (BleReceivedService.TYPE_GATT_CONNECTED.equals(type)){
Log.d(TAG, " MYTAG DashBoard onReceive TYPE_GATT_CONNECTED");
if (BleReceivedService.getGatt() != null){
BleReceivedService.getGatt().discoverServices();
Log.d(TAG, " Connect Device");
stopScan();
}
} else if (BleReceivedService.TYPE_GATT_DISCONNECTED.equals(type)){
Log.d(TAG, " MYTAG Disconnect Device");
Log.d(TAG, " MYTAG DashBoard onReceive TYPE_GATT_DISCONNECTED");
BleReceivedService.getInstance().disconnectDevice();
if(isScanning) {
Log.d("ScanCheck","Scanning after disconnection");
stopScan();
}
} else if (BleReceivedService.TYPE_GATT_ERROR.equals(type)) {
Log.d(TAG, " MYTAG DashBoard onReceive TYPE_GATT_ERROR");
//here i want to call startscan() method in the above described fashion
} else {
//do someting
}
}
};
Please help me with the code to achieve this.
Thanks in advance.

mToast.cancel() is not working

I made a method to make sure my toast messages will be displayed immediately, without having to wait the previous toast to dissapear. The method :
public void myToaster(String message){
if(mToast!=null){
mToast.cancel();
}
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
I'm using Android Studio with API23.
mToast is never assigned/reassigned . This
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
should be
mToast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
mToast.show();
Toast.cancel doesn't remove the current Toast immediately. The fade out animation takes place nevertheless

Stop app processes on phone call?

I have an app which plays audio. The thing is, when I get a call on my phone while I am interacting with the app, the audio continues to play. This becomes a problem because the audio makes it hard to understand what the person is saying. Is there any method that gets called when the user is talking on a call? That way I can mute my audio there. I have thoroughly researched the activity life cycle, but cannot find anything that will help me with this issue. I really appreciate all of your expert advice.
I have tried to put this code into my BaseActivity which gets extended by all other activities:
PhoneStateListener phoneStateListener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
//Incoming call: Pause music
TwentySeconds.stopTimer();
Intent i = new Intent(BaseActivity.this, FinishedBeforeTimer.class);
startActivity(i);
Toast.makeText(BaseActivity.this, "INCOMING CALL", Toast.LENGTH_SHORT).show();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
//Not in call: Play music
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
//A call is dialing, active or on hold
TwentySeconds.stopTimer();
Intent i = new Intent(BaseActivity.this, FinishedBeforeTimer.class);
startActivity(i);
Toast.makeText(BaseActivity.this, "DIALING", Toast.LENGTH_SHORT).show();
}
But then the strangest thing happens. When I call the my phone, the phone I am calling with says that I can't call my phone. I am confused as to what to do now, I really appreciate your time.
Thanks,
Rich

trying to make Toast after Activity gets opened by intent

I have an activity which gets called by 2 intents, one after a simple menu-selection and the other way by a intent after a deletion of an item in a database. However, I wanted to display in the called activity a little Toast, but only when it's opened through the intent of the deletion. I thought of following solution
public void intentCheck(){
Log.d("ShowActivity","intentCheck() called");
Bundle extras = getIntent().getExtras();
if (extras != null){
String check = extras.getString("AdvancedViewActivityCall");
if(check == "calling"){
Log.d("ShowActivity","delete-intent succeeded");
Toast success = new Toast(ShowActivity.this);
success.makeText(ShowActivity.this, "Deletion succeded", Toast.LENGTH_LONG);
}
}
but it doesn't work... somehow, no toast gets displayed.
edit:// i applied success.show(); now, but now i get a RunetimeException O.o ( http://pastebin.com/Th3NY5d0 )
edit: SOLUTION: Toast.makeText(context, text, duration).show(); //seems to be the "static way", which eclipse proposed
Have you tried if ("calling".equals(check)) instead of if(check == "calling") ?
EDIT:
try Toast.makeText(context, text, duration).show();
you have to call show method for toast until otherwise toast will not display.
success.show();

Categories

Resources