ANdroid Development - Call state of the phone - android

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" />
?

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

Detecting incoming call on android device with Cordova

I am new to learn to programme for Android. I was looking for a very long plug for the incoming call number. My search attempts were unsuccessful. I decided to create a plugin. I used as an example here are the sources:
https://github.com/petervojtek/cordova-plugin-signal-strength/blob/master/src/org/apache/cordova/plugin/SignalStrength.java
https://github.com/don/cordova-plugin-hello
Here's what I came up with:
package org.apache.cordova.plugin;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Intent;
public class SignalStrength extends CordovaPlugin {
CallStateListener ssListener;
String Number;
#Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("go")) {
TelephonyManager tm = (TelephonyManager) cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
ssListener = new CallStateListener();
tm.listen(ssListener, PhoneStateListener.LISTEN_CALL_STATE);
callbackContext.success(name);
return true;
}
return false;
}
class CallStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone
String Number= incomingNumber;
break;
}
}
}
}
Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
...
<receiver android:name=".CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
But unfortunately returns NULL
Help me to understand.
I'm using this plugin:
https://github.com/renanoliveira/cordova-phone-call-trap
usage is very simple:
if (window.PhoneCallTrap) {
PhoneCallTrap.onCall(function(state) {
console.log("CHANGE STATE: " + state);
switch (state) {
case "RINGING":
console.log("Phone is ringing");
break;
case "OFFHOOK":
console.log("Phone is off-hook");
break;
case "IDLE":
console.log("Phone is idle");
break;
}
});
}

Android Call Application "how to"

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 !!

PhoneStateListener executing multiple times

When i try to detect incoming calls with PhoneStateListener, it executes multiple times.
This is my code. onCallStateChanged method is called multiple times.
public class CallHelper {
public String number;
private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;
private OutgoingReceiver outgoingReceiver;
SharedPreferences trackMeData;
public CallHelper(Context ctx) {
this.ctx = ctx;
number ="";
callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();
trackMeData = ctx.getSharedPreferences("LockedSIM", 0);
}
private class CallStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
number = incomingNumber;
sendsmstoph(number);
System.out.println("Incomgin");
Toast.makeText(ctx, "Incoming: " + incomingNumber,Toast.LENGTH_LONG).show();
break;
}
}
}
Using BroadcastReceiver with functionality of PhoneStateListener
In above line: The problem is that every time when the onReceive() method is called a new TelphoneManager instance is created and registers as a listener to Phoone State .
Solution :
I made every variable of the CallReceiverBroadcast class static ! and it solved the problem !! to an extent but still the service is called twice every time it means that some how there is 2 instance of the class registered as a listener but i don't know why. Although i can work around it through some condition but it is causing unnecessary overhead and Anyone having a better solution will be highly Appreciated .
package com.example.netlogger.Receiver;
import java.util.Date;
import com.example.netlogger.util.LocalDatabase;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;import android.widget.Toast;
public class CallActionsReceiver extends BroadcastReceiver {
static ThePhoneStateListener phoneStateListener;
#Override
public void onReceive(Context arg0, Intent arg1) {
TelephonyManager manager = (TelephonyManager) arg0
.getSystemService(arg0.TELEPHONY_SERVICE);
if (phoneStateListener == null) {
phoneStateListener = new ThePhoneStateListener(arg0);
manager.listen(phoneStateListener,
android.telephony.PhoneStateListener.LISTEN_CALL_STATE);
}
}
}

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.

Categories

Resources