从列表视图数组呼叫电话号码



我正在尝试创建诸如电话簿或硬编码联系人列表之类的东西。我已经使用联系人姓名和号码创建了ListView,但是在按下时需要帮助拨打电话号码。示例编码将是完美的。谢谢

  ListView listView;
    String [ ]stations = {"911", "999","Jack", "James", "Terror"};
    String pNumbers [] = {"911", "999", "444", "554", "664"
            "};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_police_numbers);
        ListView listView = (ListView) findViewById(R.id.numbersP);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stations);
        listView.setAdapter(adapter);
        listView= (ListView)findViewById(R.id.numbersP);
    }

下面是一个示例代码,可以帮助您入门:

   private void makeTelCall(String telNumber){
        try{
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:"+telNumber));
            startActivity(intent);
        }
        catch(SecurityException se){
            openAppDetailSettings();
            String mess = "Permission for telephone calls has not been granted!";
            Toast.makeText(this, mess, Toast.LENGTH_LONG).show();
            Log.e(TAG, se.getMessage());
        }
        catch (Exception ex){
            Log.e(TAG, ex.getMessage());
        }
    }

用户必须在设置中授予进行呼叫的权限。并且您必须对清单文件进行适当的更改。

<uses-permission android:name="android.permission.CALL_PHONE"/>

为了从代码直接进入设置页面,您可以添加以下内容:

private void openAppDetailSettings(){
    String s =  Settings.ACTION_APPLICATION_DETAILS_SETTINGS;
    Intent intent = new Intent();
    intent.setAction(s);
    Uri uri = Uri.fromParts("package", BuildConfig.APPLICATION_ID, null);
    intent.setData(uri);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

请注意,我添加了来自异常的方法调用。

请注意,Android 将不允许您"静默"拨打电话(无需单击"通话按钮"(。这是一项安全措施,可防止在用户不知道正在从其设备进行呼叫的情况下进行呼叫。

最新更新