我正在尝试创建电话目录或硬编码的联系人列表之类的东西。我已经用联系人姓名和电话号码创建了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);
}
答案 0 :(得分:2)
以下是示例代码,可以帮助您入门:
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不允许您“无声地”拨打电话(无需单击“通话按钮”)。这是一种安全措施,可以防止在用户不知道自己设备正在拨打电话的情况下拨打电话。