toast message android - android

I am trying to pop up a toast message as long as the phone rings and destroy the toast them the call is rejected or answered.
In the OnReceive method I have something like this:
Bundle bundle=intent.getExtras();
final String state=bundle.getString(TelephonyManager.EXTRA_STATE);
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
Toast toast= new Toast(context);
toast.show();
new CountDownTimer(3500,1000)
{
#Override
public void onFinish()
{
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)||
(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
toast.cancel();
}
else
{
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
start();
}
}
The problem is that even after the call is hanged up the toast message keeps poping up. It's like the state is never in the HANG_UP or IDLE Mode.
What did I do wrong?

in your broad cast recievere on recieve use this
#Override
public void onReceive(Context context, Intent intent) {
Log.d("INCOMING", "service start");
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
// telephonyService = (ITelephony) m.invoke(tm);
Bundle bundle = intent.getExtras();
String phoneNumber = bundle.getString("incoming_number");
Log.d("INCOMING", phoneNumber);
if ((phoneNumber != null) && !phoneNumber.equals(selectedtednumber)) {
Toast.makeText(context, "Someone other calling sending to voice mail ...", Toast.LENGTH_LONG).show();
sendToVoiceMail();
}
}

This is wrong usage of the Toast feature. You should find a another way to do whatever you trying to do.
I think the closest thing that matches your need is PopupWindow. Check out the documentation.

Related

Android - telephonyService.endCall() is delayed

I have this following BroadcastReceiver class to block incoming calls.
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);
telephonyService.endCall();
} catch (Exception e) {
System.out.println("error on end call : "+e.getMessage());
e.printStackTrace();
}
}
}
Everything is working as intended but my issue is, everytime there is a delay in telephonyService.endCall();. Meaning, I can hear a fraction of ringtone before it gets disconnected. Also, the call disconnected by endCall() is not counted as a missed call but as a incoming call. Is this the default behaviour?
Thanks for any inputs.

Send sms after call rejection - being sent multiple times

I want to send SMS after reject incoming call.
App is sending the SMS but the problem is that it is sending it twice. I am unable to figure out where problem is.
Below is the code used.
public class CallBarring extends BroadcastReceiver
{
private String number;
#Override
public void onReceive(Context context, Intent intent)
{
// If, the received action is not a type of "Phone_State", ignore it
if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
// Else, try to do some action
else
{
// Fetch the number of incoming call
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
// Check, whether this is a member of "Black listed" phone numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context);
// return;
}else{
disconnectPhoneItelephony(context);
sendSMS(context);
}
}
}
public void sendSMS(Context context) {
try{
String message = SharedPrefActivity.CommonMethod.getPrefsData(context, SharedPrefActivity.Constants.TextMessage, "");
Intent intent=new Intent(context,CallBarring.class);
PendingIntent pi=PendingIntent.getActivity(context, 0, intent,0);
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(number, null, message, pi,null);
Toast.makeText(context, "SMS Sent", Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(context, "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
// Method to disconnect phone automatically and programmatically
// Keep this method as it is
#SuppressWarnings({ "rawtypes", "unchecked" })
private void disconnectPhoneItelephony(Context context)
{
ITelephony telephonyService;
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try
{
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Try This:
// Check, whether this is a. member of "Black listed" phone. numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context);
// return;
}else{
disconnectPhoneItelephony(context);
sendSMS(context);
}
This to
// Check, whether this is a. member of "Black listed" phone. numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context)
}

Send a missed call-Android program

I want to write a android application which can send sb a missed call. It means that application call to sb by about 5 second.
I know that this code start calling.
private void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
} catch (ActivityNotFoundException e) {
Log.e("helloandroid dialing example", "Call failed", e);
}
}
But I dont know how to stop it(after 5 second)?
Ps.This question have got mark -1 because question is stupid or my english is bad?
There is no direct way to do this because once you start the callIntent, the control will go to the calling application.
But there are workarounds to do this by getting the ITelephony object of TelephonyManagerusing java reflection.
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);
}catch(Exception e){}
telephonyService.endCall();
source:
call-control-in-android
The simplest way is to use a delay Handler and make a new call to self after you have made the call to your target number. Hoping this is not for spam purposes which is frowned upon. In your code snipplet or loop you can have the below
if (checkPermission(Manifest.permission.CALL_PHONE)) {//targetMSISDN the phone no you are calling out to
String dial = "tel:" + targetMSISDN;
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
//startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
//delay a drop call
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 10000ms
Toast.makeText(YourNotSpamActivity.this, "Calling me to end you!", Toast.LENGTH_SHORT).show();
//end call now
String TAG="ERR:";
try {
Log.v(TAG, "Get getTeleService...");
if (checkPermission(Manifest.permission.CALL_PHONE)) {
String dial = "tel:YourPhoneNumber";
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));//call yourself to terminate the first call
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG,
"FATAL ERROR: couldn't call ");
Log.e(TAG, "Exception object: " + e);
}
}
}, 10000);
//end the end call
} else {
Toast.makeText(YourNotSpamActivity.this, "Permission Call Phone denied", Toast.LENGTH_SHORT).show();
}

Keep the current Activity in front while call come

Backgroud
I am developing an app which makes calls like lollpop. When a call comes it should show a popup on the screen and the default calling screen should not be visible.
What I have done
I have build the broadcast receiver and currently I am able to show a popup every time a call come.
by using this code
public class PhoneCallReceive extends BroadcastReceiver {
Class<?> c;
public static ITelephony telephonyService = null;
TelephonyManager telephony;
Method m;
public static int flag = 0;
public static AudioManager audioManager;
#Override
public void onReceive(Context conk, Intent inter) {
final Context context = conk;
final Intent intent = inter;
Thread pageTimer = new Thread() {
public void run() {
try {
sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
c = Class.forName(telephony.getClass().getName());
m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
} catch (Exception e) {
e.printStackTrace();
}
if (telephony.getCallState() == 1)
flag = 1;
Log.d("Rec", "reciever");
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
callingscreen("1", context, intent);
}
}
};
pageTimer.start();
}
public void callingscreen(final String type, final Context context,
final Intent intent) {
Intent intentPhoneCall = new Intent(context.getApplicationContext(),
CallerScreen.class);
intentPhoneCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
intentPhoneCall.putExtra(
"number",
intent.getExtras().getString(
TelephonyManager.EXTRA_INCOMING_NUMBER));
intentPhoneCall.putExtra("type", type);
context.startActivity(intentPhoneCall);
}
} // class
Problem
But the Problem is that this popup is showing over the default calling screen. What I want to show the popup on the user current screen on which he was working.
Question
So the simple question is that how can I get the user current screen with its all functionality below the popup?
Please help:)
This solution works on every version of android, including lollipop
Use this broadcast reciever
class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// CALL_STATE_IDLE;
Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
// CALL_STATE_RINGING, call intent here
Intent i = null;
i = new Intent(context, My_activity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
break;
default:
break;
}
}
}
and register it like this in onCreate
TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
And yes, please dont miss
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Cheers! Your problems is solved ;)

how to end up my outgoing call.

with statrtActivity(callIntent), call goes and then i have to wait for few seconds and end automatically. to end up my call i have taken mycalss extends Broadcastreceiver then in that onreceive() i implemented.in that method i got only to set old number and newnumber and toast is printing.
What i want exactly is to end call what i need to write. and how to call onreceive method from my class? plase help me. i didnt get anywhere.
#Override
public void onReceive(Context context, Intent intent) {
final String oldNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
this.setResultData(newPhNnumber);
final String newNumber = this.getResultData();
if((newNumber!=null)&&(newNumber!=oldNumber))
{
String msg = "Intercepted outgoing call. Old number " + oldNumber + ", new number " + newNumber;
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
this.abortBroadcast();----> what it does?
}
You may try this:
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService = (ITelephony) m.invoke(tm);
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
you simply abort outgoing call through following code
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction()
.equals("android.intent.action.NEW_OUTGOING_CALL")) {
phoneNo = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (phoneNo.isBlocked(blockNo)) {
setResultData(null);
}
}
}

Categories

Resources