Back to the App after call
ok , I found the answer
here is the code :
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonCall);
// add PhoneStateListener
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
call();
}
});
}
private void call()
{
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:0377778888"));
startActivity(callIntent);
}
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
String LOG_TAG = "LOGGING 123";
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling) {
Log.i(LOG_TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
}
and in the manufest we need to add
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Return to app after call ends
After you start the phone call have a listener, finish the call activity, and it will redirect back to the last activity (your app). Here is a link to some sample code (it's in spanish but you should be able to make out the code at least): Returning to App After Phone Call
Android: how to make an activity return results to the activity which calls it?
In order to start an activity which should return result to the calling activity, you should do something like below. You should pass the requestcode as shown below in order to identify that you got the result from the activity you started.
startActivityForResult(new Intent(“YourFullyQualifiedClassName”),requestCode);
In the activity you can make use of setData()
to return result.
Intent data = new Intent();
String text = "Result to be returned...."
//---set the data to pass back---
data.setData(Uri.parse(text));
setResult(RESULT_OK, data);
//---close the activity---
finish();
So then again in the first activity you write the below code in onActivityResult()
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
String returnedResult = data.getData().toString();
// OR
// String returnedResult = data.getDataString();
}
}
}
EDIT based on your comment:
If you want to return three strings, then follow this by making use of key/value pairs with intent instead of using Uri.
Intent data = new Intent();
data.putExtra("streetkey","streetname");
data.putExtra("citykey","cityname");
data.putExtra("homekey","homename");
setResult(RESULT_OK,data);
finish();
Get them in onActivityResult like below:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
String street = data.getStringExtra("streetkey");
String city = data.getStringExtra("citykey");
String home = data.getStringExtra("homekey");
}
}
}
stop launching of call Receive screen and respond by own Activity in android
first make a subclass of BroadcastReceiverpublic class CallReceiver extends BroadcastReceiver {
add it in manifest.xml file<receiver android:name="com.myapp.calldropper.CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
in onReceive start another Activity Screen upon the default
Intent callRejectIntent = new Intent(context, MainActivity.class);
callRejectIntent.putExtra("MOBILE_NUMBER", mobNum);
callRejectIntent.putExtra("REJECT_COUNT", rejectCount);
callRejectIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callRejectIntent);
your Activity will launch upon the default one. now you can responde the incomming call from your activity, you can Reject call.
for that make a seperate package named com.android.internal.telephony
and in this create a simple text file named ITelephony.aidl
. this file will contain
package com.android.internal.telephony;
import android.os.Bundle;
interface ITelephony {
boolean endCall();
void dial(String number);
void answerRingingCall();
}
add code below in onCreate
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
try {
// "cheat" with Java reflection to gain access to TelephonyManager's ITelephony getter
Class<?> c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(tm);
} catch (Exception e) {
e.printStackTrace();
}
now you can reject the call by calling below function
private void ignoreCall() {
try {
// telephonyService.silenceRinger();
telephonyService.endCall();
} catch (RemoteException e) {
e.printStackTrace();
}
moveTaskToBack(true);
finish();
}
Related Topics
Android Get Free Size of Internal/External Memory
How to Use Asynctask Correctly in Android
How to Force Use of Overflow Menu on Devices With Menu Button
Android Chrome Ignoring -Webkit-Text-Size-Adjust:None Property. Text Is Being Scaled When Zoomed Out
Differencebetween Min Sdk Version/Target Sdk Version VS. Compile Sdk Version
What Does Layoutinflater in Android Do
Accessing External Storage in Android API 29
Outlined Edit Text from Material Design
How to Add a Jar in External Libraries in Android Studio
Android.Content.Activitynotfoundexception:
What's the Best Way to Limit Text Length of Edittext in Android
How to Detect Android App Uninstall