Android Call Application "how to" - android

i have this very basic code that should work as a call application.
everything is hardcoded.
i don't find usefull tutorials according to this application i need / want to create.
my question is large ! Can anyone pls help me creating a CallApplication
requirement is simple i think
need to be able to dail a number
this is the code i have at the moment, as said it is very basic, but i'm stuck ^^
any help is much appreciated!
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 btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
PhoneCallListener phoneCallListener = new PhoneCallListener();
TelephonyManager telManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
// add button listener
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse("tel:123456"));
startActivity(phoneCallIntent);
}
});
}
// monitor phone call states
private class PhoneCallListener extends PhoneStateListener {
String TAG = "LOGGING PHONE CALL";
private boolean phoneCalling = false;
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(TAG, "OFFHOOK");
phoneCalling = true;
}
// When the call ends launch the main activity again
if (TelephonyManager.CALL_STATE_IDLE == state) {
Log.i(TAG, "IDLE");
if (phoneCalling) {
Log.i(TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
phoneCalling = false;
}
}
}
}
}

As per #SeeSharp comment.
I guess your requirement is to remove hardcoded contact.
Way 1:
Take contact as Input from User (Example in EditText , Design UI).
Way 2:
Use Getting a Result from an Activity to get contact from Contacts App.
startActivityForResult() Contacts Application to Pick a contact from android.provider.ContactsContract. More at : this and this.
Read ContactsContract.
Note : Please make your problem/Issue in question explanatory enough to be understood.
Hope this helps !!

Related

How to get call state of outgoing android call

I am originating a call from Android Studio. The code is as follows:
I want to get the state of the call at any point. The link: https://developer.android.com/reference/android/telecom/Call.html
shows the state of the call can be obtained by using the Class call.. SO if I use Call.getState() I should be able to get the current state. But I get the compilation error:
Error:(28, 20) error: Call() is not public in Call; cannot be accessed from outside package. There are several call states defined in the enum: Dialing, Ringing, Connected, DIsconnected, Holding, etc.
When I run the code, it does make the call as I can see the screen of emulator making the call.
The developer guide does not provide any examples of using these classes.
Thank you for your help.
package com.example.ramesh.makeacall;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.Call;
import android.telephony.*;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Call call;
call = new Call();
call();
}
private void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:5555551212"));
System.out.println("====before startActivity====");
startActivity(callIntent);
} catch (ActivityNotFoundException e) {
Log.e("helloAndroid","Call failed",e);
}
}
}
Try to use like this(Haven't tried it though) -
Call.Callback callback = new Call.Callback() {
#Override
public void onStateChanged(Call call, int state) {
super.onStateChanged(call, state);
if(state == Call.STATE_RINGING){
//you code goes here
}
}
};
public class MyPhoneStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
handleRinging(incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
handleOffHook();
break;
case TelephonyManager.CALL_STATE_IDLE:
handleIdle();
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
and register statelistener :
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

Why PhoneStateListener still alive after sending LISTEN_NONE

I'm a beginner with programming in Android OS that is why I've decided to ask your help. My problem is related to PhoneStateListener which should be called depending on toggle button status:
togglebtn==ON - If ServiceStateChanges or DataConnectionStateChanges -> print a Toast on the screen
togglebtn==OFF - do not check ServiceStateChanges or DataConnectionStateChanges
I've found that in order to stop listening i should send LISTEN_NONE as parameter to listen method, but it does not work
Here's the simplest version of my code:
package network.com.example;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
ToggleButton button_on_off=(ToggleButton)findViewById(R.id.toggleButton1);
button_on_off.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
nw_update(tm , "onCRE");
tm.listen(new PhoneStateListener() {
#Override
public void onServiceStateChanged(ServiceState serviceState) {
nw_update(tm , "onSSC");
}
#Override
public void onDataConnectionStateChanged(int state) {
nw_update(tm , "onDCC");
}
}, PhoneStateListener.LISTEN_SERVICE_STATE | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
}
else
{
tm.listen(null, PhoneStateListener.LISTEN_NONE | PhoneStateListener.LISTEN_NONE );
}
}
}); // end of button on/off
} // end of onCreate
private final void nw_update(TelephonyManager tm , String rodzaj) {
String _rodzaj = rodzaj;
Toast.makeText(getApplicationContext(), _rodzaj, Toast.LENGTH_SHORT).show();
}
} // end of main activity
Any ideas?
I am looking forward to hearing from you.
Quoting the documentation for listen(), with added emphasis:
To unregister a listener, pass the listener object and set the events argument to LISTEN_NONE (0).
You are not passing the listener object -- you are passing null. You need to pass the same PhoneStateListener instance as you used with the original listen() call.

Android Dialer in the Background

Is it possible to call the Intent.ACTION_CALL in the background ?
I want my application to call but I don't want it to put in the background, I want it to stay in the foreground while it is calling.
The Intent.ACTION_CALL will call the default Android Phone activity and call the number you provided. If you want your App to be in the foreground, which i take to understanding you are builder your own calling app, you will have to implement the Calling part yourself.
Otherwise you can launch your activity with say a 5 seconds delay if you just want to show the user a context of your application.
You can invoke your activity after making a call using this,
package com.sdi.androidcall;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button call;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
call = (Button) findViewById(R.id.call);
final PhoneCallListener phoneListener = new PhoneCallListener();
final TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
call.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
}, 10000);
}
});
}
private class PhoneCallListener extends PhoneStateListener {
boolean flag = true;
String LOG_TAG = "Call TEST";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
Log.i(LOG_TAG, "OFFHOOK");
// invoking activity
if (flag) {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
System.out.println("flagged retrieving app");
flag = false;
}
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
Log.i(LOG_TAG, "IDLE");
Log.i(LOG_TAG, "restart app");
}
}
}
}
The activity will be called after a delay of 10 seconds here using PhoneStateListener
You need to specify the intent permissions in the mainfest as
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Try it, let me know if worked.

Back to the App after call

i'm trying to make a phone call from the app
and I want it to return back to the app after the call
i asked that question in this forum but i didn't understand the answer
How to make a phone call in android and come back to my activity when the call is done?
public void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:048598077"));
getContext().startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("dialing-example", "Call failed", activityException);}
finally {
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
private class EndCallListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
Log.i(LOG_TAG, "OFFHOOK");
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Log.i(LOG_TAG, "IDLE");
}
}
i understand some thing like that, but i didn't start the CallListener at any point.
So how can i do that ?
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" />
I don't really know how your app is supposed to work,but if you have your phone number in a text view ,just set(in your .xml file) the property: android:autoLink="phone" and everything will work just fine.(On click you will be able to make a phone call and when you hit end call it will return to your activity)
EDIT:If you want to do it automatically then you should use an intent.try this:
how to make phone call using intent in android?

ANdroid Development - Call state of the phone

I'm new to Android and I need to read the call state of the phone. I receive error when the app runs (stopped):
package com.example.droid1;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.TextView;
import android.widget.Toast;
public class DroidActivity extends Activity {
private TextView text0;
private TelephonyManager telephoneM;
private PhoneStateListener listner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_droid);
text0 = (TextView) findViewById(R.id.textout);
telephoneM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
listner = new PhoneStateListener()
{
public void onCallStateChanged(int state, String incomingnumber) {
String stateS = "N/A";
switch(state) {
case TelephonyManager.CALL_STATE_IDLE:
stateS = "Oscioso";
Toast.makeText(DroidActivity.this, ""+stateS,Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
stateS = "Sonando";
Toast.makeText(DroidActivity.this, ""+stateS,Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateS = "Ocupado";
Toast.makeText(DroidActivity.this, ""+stateS,Toast.LENGTH_SHORT).show();
break;
}
text0.append (String.format("\nonCallStateChanged: %s",stateS));
}
};
telephoneM.listen(listner, PhoneStateListener.LISTEN_CALL_STATE);
}
}
I don't have any error messages in eclipse, the app installs without a problem on Virtual device but when it run I have the error message of "Unfortunatly Droid1 has stopped"
Any advice will be appreciated. Thx
check your manifest.
Did you put this permission
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
?

Categories

Resources