从列表视图数组中调用 phone 个号码
Call phone numbers from list view array
我正在尝试创建类似于 phone 目录或硬编码联系人列表的内容。我已经创建了带有联系人姓名和号码的 ListView,但我需要帮助才能在按下时拨打 phone 号码。示例编码将是完美的。谢谢
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());
}
}
用户必须在设置中授予拨打电话的权限。并且您必须对 Manifest 文件进行适当的更改。
<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 将不允许您拨打电话 'silently'(不单击 'call button')。这是一项安全措施,可防止在用户不知道正在从其设备拨打电话的情况下拨打电话。
我正在尝试创建类似于 phone 目录或硬编码联系人列表的内容。我已经创建了带有联系人姓名和号码的 ListView,但我需要帮助才能在按下时拨打 phone 号码。示例编码将是完美的。谢谢
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());
}
}
用户必须在设置中授予拨打电话的权限。并且您必须对 Manifest 文件进行适当的更改。
<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 将不允许您拨打电话 'silently'(不单击 'call button')。这是一项安全措施,可防止在用户不知道正在从其设备拨打电话的情况下拨打电话。