I am creating an app which reads a list of numbers from a text file and shown in a list view, I can achieve that but dont have idea on how to schedule calls one after one
Example.xls
Consider a list of numbers in a file row wise
00000000000
62728828282
26727737338
My app should call one after one using intent after a button is clicked it should automatically call all the numbers
public class ScheduledFragment extends Fragment {
String[] strings;
RecyclerView listView;
Button button;
List<PhoneNumber> phoneNumbers;
public ScheduledFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_scheduled, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = view.findViewById(R.id.list);
button = view.findViewById(R.id.btn);
FloatingActionButton fab = view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
requestPermission();
} else {
readExcelFileFromAssets();
initialiseAdapter();
}
}
});
final PhoneState phonestate = new PhoneState();
TelephonyManager telephonyManager = (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phonestate,PhoneStateListener.LISTEN_CALL_STATE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
InputStream myInput;
AssetManager assetManager = getActivity().getAssets();
myInput = assetManager.open("phone.xls");
Workbook workbook = Workbook.getWorkbook(myInput);
final Sheet sheet = workbook.getSheet(0);
final int row = sheet.getRows();
final int col = sheet.getColumns();
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
String number = "";
Cell cell;
cell = sheet.getCell(i,j);
number =number + cell.getContents();
Toast.makeText(getContext(),number+"",Toast.LENGTH_LONG).show();
if (!phonestate.isOnCall) {
callPhone(number);
}
}
}
} catch (Exception e) {
}
}
});
}
//Methods
private void requestPermission() {
ActivityCompat.requestPermissions(getActivity(), new String[]
{Manifest.permission.CALL_PHONE}, 1);
}
private void initialiseAdapter() {
listView.setHasFixedSize(true);
listView.setLayoutManager(new LinearLayoutManager(getContext()));
RecyclerAdapter recyclerAdapter = new RecyclerAdapter(phoneNumbers);
listView.setAdapter(recyclerAdapter);
}
public void callPhone(String number) {
Intent call = new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:" + number));
startActivity(call);
}
public void readExcelFileFromAssets() {
phoneNumbers = new ArrayList<>();
try {
InputStream myInput;
AssetManager assetManager = getActivity().getAssets();
myInput = assetManager.open("phone.xls");
Workbook workbook = Workbook.getWorkbook(myInput);
Sheet sheet = workbook.getSheet(0);
int row = sheet.getRows();
int col = sheet.getColumns();
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
String number = "";
Cell cell;
cell = sheet.getCell(i, j);
number = number + cell.getContents();
phoneNumbers.add(new PhoneNumber(number));
}
}
} catch (Exception e) {
}
}
}
class PhoneState extends PhoneStateListener {
boolean isOnCall = true;
@Override
public void onCallStateChanged(int state, String phoneNumber) {
super.onCallStateChanged(state, phoneNumber);
if (TelephonyManager.CALL_STATE_IDLE == state) {
isOnCall = false;
} else if (TelephonyManager.CALL_STATE_RINGING == state) {
isOnCall = true;
} else if (TelephonyManager.CALL_STATE_OFFHOOK == state){
isOnCall = true;
}
}
}
class PhoneNumber {
String number;
public PhoneNumber() {
}
public PhoneNumber(String number) {
this.number = number;
}
}
Result I want
Should Call all the number one by one automatically after ending the call
Result I am getting
Calling only a single number which is randomly selected from file
Can anyone please tell me how to do this? I am new to this any help will be appreciated thanks