如何在Android中直接拨打电话
我知道这很简单,但我遇到了一个非常奇怪的问题。在危险的情况下,我只要按一下按钮就可以报警。所以我使用了下面的代码来调用如何在Android中直接拨打电话,android,android-intent,phone-call,Android,Android Intent,Phone Call,我知道这很简单,但我遇到了一个非常奇怪的问题。在危险的情况下,我只要按一下按钮就可以报警。所以我使用了下面的代码来调用 Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:100")); callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.star
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:100"));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(callIntent);
在AndroidManifest.xml
中添加了CALL\u PHONE
权限。问题是它正在打开拨号盘上的100,但没有直接拨打电话。我希望当用户单击按钮时立即发生这种情况
当我尝试将+91置于100之前时,它会自动呼叫号码,但为什么这样的号码需要加号呢。所以有人来帮我解决这个问题可能会有一个问题,android系统无法识别
100
为有效电话号码,相反,如果您将国家代码放在它前面,那么它工作正常。要解决此问题,请查看此库。你可以这样用
public static ArrayList<String> extractPhoneNumber(String content) {
ArrayList<String> numbers = new ArrayList<String>(0);
PhoneNumberUtil instance = PhoneNumberUtil.getInstance();
//Change IT with your contry code
Iterable<PhoneNumberMatch> matches = instance.findNumbers(content, "IT");
Iterator<PhoneNumberMatch> iterator = matches.iterator();
while (iterator.hasNext()) {
numbers.add(instance.format(iterator.next().number(), PhoneNumberFormat.INTERNATIONAL));
}
return numbers;
}
publicstaticarraylistextractPhoneNumber(字符串内容){
ArrayList编号=新的ArrayList(0);
PhoneNumberUtil实例=PhoneNumberUtil.getInstance();
//用你的国家代码更改它
Iterable matches=instance.findNumbers(内容,“IT”);
迭代器迭代器=matches.Iterator();
while(iterator.hasNext()){
add(instance.format(iterator.next().number(),PhoneNumberFormat.INTERNATIONAL));
}
返回号码;
}
来自行动调用的文档
:
注意:对哪些应用程序可以发起调用有限制;大多数应用程序都应该使用操作拨号
注:此目的不能用于拨打紧急电话号码。然而,应用程序可以使用操作拨号拨打紧急电话
因此,这种行为似乎是故意的。无需用户干预直接呼叫的最佳方式
private void phoneCall()
{
String phoneCallUri = "tel:91";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
有两种呼叫/开始呼叫的意图:动作呼叫和动作拨号。
ACTION_DIAL将只打开已填号码的拨号器,但允许用户实际呼叫或拒绝呼叫。ACTION_CALL将立即拨打该号码,并需要额外的权限。
因此,请确保您拥有该权限很长一段时间。但可能会帮助其他人。
如果您想直接调用,应该使用方法
1.将此行添加到清单文件:
<uses-permission android:name="android.permission.CALL_PHONE" />
3.将这些行添加到活动的onCreate方法中:
final String permissionToCall = Manifest.permission.CALL_PHONE;
//Assume that you have a phone icon.
(findViewById(R.id.menuBarPhone)).setOnClickListener(new OnClickListener(){
public void onClick(View view) {
phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(getString(R.string.callNumber))); //Uri.parse("tel:your number")
if (ActivityCompat.checkSelfPermission(MainFrame.this, permissionToCall) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainFrame.this, new String[]{permissionToCall}, 1);
return;
}
startActivity(phoneCallIntent);
}
});
4.要在单击允许按钮后立即拨打电话,覆盖方法:
@覆盖
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
if(requestCode==1){
final int permissionsLength=permissions.length;
对于(int i=0;i
当用户给予许可时,下次将不会出现对话框,将直接拨打电话。我怀疑这可能与“100”不是美国电话系统上的有效电话号码有关。(这可能是一个错误)您好,您找到了解决方案吗?仍然在发生。具体来说,您需要CALL\u PRIVILEGED
权限,这是普通SDK应用程序所不具备的。您是否首先在@user69226处收到了我的问题。我知道ACTION\u CALL会直接拨打手机号码,但不会拨打100等紧急号码。
final String permissionToCall = Manifest.permission.CALL_PHONE;
//Assume that you have a phone icon.
(findViewById(R.id.menuBarPhone)).setOnClickListener(new OnClickListener(){
public void onClick(View view) {
phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(getString(R.string.callNumber))); //Uri.parse("tel:your number")
if (ActivityCompat.checkSelfPermission(MainFrame.this, permissionToCall) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainFrame.this, new String[]{permissionToCall}, 1);
return;
}
startActivity(phoneCallIntent);
}
});
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1){
final int permissionsLength = permissions.length;
for (int i = 0; i < permissionsLength; i++) {
if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
startActivity(phoneCallIntent);
}
}
}