一对一拨打电话

时间:2019-06-25 14:55:32

标签: java android

我正在开发一个正在调用的android应用程序,该应用程序会使用excel工作表提供的数据自动地一个一地调用数字。

然后该应用将呼叫重定向到自定义android拨号程序屏幕(从此处获取:https://github.com/Abror96/CustomPhoneDialer

这是我的代码的一部分:

public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CALL = 1;
String storedData = "";

//for csv file
InputStream inputStream;
String[] data;

@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView img = (ImageView) findViewById(R.id.imageView);


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

            //makePhoneCall(storedData);
            String file1 = getIntent().getExtras().getString("keyName", "defaultKey");       //get excel file path

            //sharing file paths data between methods in different classes                
            SharedPreferences sharedPref = getSharedPreferences("fileinfo", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString("file1", file1);
            editor.apply();
            //file1 is an excel file which contains all the phone numbers to be called in first column
            //end of shared preferences


            File file = new File(file1);
            FileInputStream inputStream = null;


            try {
                inputStream = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));


              try {
                String csvLine;
                while ((csvLine = reader.readLine()) != null) {

                    data = csvLine.split(",");
                    try {
                        storedData = data[0];

                        Log.e("call status", storedData);


                        //call number (ISSUE IS HERE, I want to wait till phone call made by this method ends)
                        makePhoneCall(storedData);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } catch (IOException ex) {
                throw new RuntimeException("Error in reading CSV file: " + ex);
            }
        }
    });
}

private void makePhoneCall(String phoneno) {
    final String number = phoneno;
    if (number.trim().length() > 0) {
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "Calling Permission was Denied!\nPlease restart app.", Toast.LENGTH_SHORT).show();

        } else {
            String dial = "tel:" + number;
            startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
        }
    } else {
        Toast.makeText(MainActivity.this, "Enter Phone Number", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_CALL) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            makePhoneCall(storedData);
        } else {
            Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
        }
    }
}

}

我的问题是makePhoneCall(storedData)方法不等待调用结束。它立即开始while循环的下一次迭代。这样会导致在上一个呼叫仍在进行中时发生另一个呼叫。

我已经尝试在makePhoneCall(storedData)方法之后使用PhoneStateListener冻结UI线程,直到上一次调用结束。

此代码将一直处于OFFHOOK状态(拨号/呼叫状态),直到呼叫结束(即退出OFFHOOK状态)

telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
                                while (true) {
                                    if (telephonyManager.getCallState() == 2) {
                                        Log.e("OFFHOOK DATA", "Still in offhook");
                                    } else {
                                        break;
                                    }
                                }

(我知道这是一个非常糟糕的方法。) 仍然不会重定向到我的“自定义电话拨号程序”屏幕。它冻结UI线程(而调用确实在后台进行)。

因此,是否有任何方法可以在发生呼叫时停止UI线程(但不冻结它,因为从那以后,我的自定义电话拨号程序不会打开)和/或仅允许一个接一个地进行呼叫(等待以前的通话结束了吗?

任何帮助将不胜感激。谢谢。

0 个答案:

没有答案