单击按钮时如何调用数字列表

时间:2019-06-04 21:33:14

标签: android alarmmanager call android-pendingintent phone-call

我正在创建一个从文本文件中读取数字列表并显示在列表视图中的应用程序,我可以实现这一点,但是对如何安排一个接一个的呼叫一无所知

Example.xls

明智地考虑文件行中的数字列表

00000000000

62728828282

26727737338

我的应用程序应在单击按钮后使用意图一个接一个地呼叫,它应自动呼叫所有号码

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

我想要的结果

通话结束后应自动将所有号码一一拨打

我得到的结果

仅呼叫从文件中随机选择的一个号码

谁能告诉我该怎么做?我是新来的,任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

引用此链接here

遍历.xls文件中的所有数字后,请输入此代码。

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    for(int i = 0;i <phoneNumbers.size();i++)
    {
    if (!phonestate.isOnCall)
      {
       callPhone(phoneNumbers.get(i).toString());
      }
    }
}