import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
attachUi();
//getPattern();
}
private void attachUi() {
tv = (TextView) findViewById(R.id.textview);
tv.setOnClickListener(clk);
}
private View.OnClickListener clk = new View.OnClickListener() {
@Override
public void onClick(View view) {
gotoCall();
}
};
private void gotoCall() {
ArrayList<String> list = new ArrayList<String>();
list.add("08698511503");
list.add("07666175151");
list.add("0548554552");
TelephonyManager TelephonyMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(new PhoneCallListener(this, list), PhoneStateListener.LISTEN_CALL_STATE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
//startActivity(callIntent);
}
}
// create new class
class PhoneCallListener extends PhoneStateListener {
int count = 0;
Context context;
ArrayList<String> list;
public PhoneCallListener(MainActivity mainActivity, ArrayList<String> list) {
this.context = mainActivity;
this.list = list;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Log.v(this.getClass().getSimpleName(), "List Size ::" + list);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.v(this.getClass().getSimpleName(), "Inside Ringing State::");
break;
case TelephonyManager.CALL_STATE_IDLE:
String phone_number = null;
Log.v(this.getClass().getSimpleName(), "Inside Idle State::");
if (list.size() > count) {
phone_number = list.get(count);
count++;
}
if (phone_number != null) {
nextCalling(phone_number);
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.v(this.getClass().getSimpleName(), "Inside OFFHOOK State::");
break;
default:
break;
}
}
private void nextCalling(String phone_number) {
Intent callIntent1 = new Intent(Intent.ACTION_CALL);
callIntent1.setData(Uri.parse("tel:" + phone_number));
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
context.startActivity(callIntent1);
}
}
// in manifest add permission..
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />