Android Intent.ACTION_CALL, Uri

12

我正在尝试使用Intent.Action类。我知道如何使用ACTION_VIEW来显示URL,但我想在启动应用程序时使用Intent.ACTION_DIAL来拨打电话号码。文档说您需要将URI解析为字符串,然后将其添加到Intent中。我尝试了以下代码:

Uri call = Uri.parse("7777777777");             
Intent surf = new Intent(Intent.ACTION_DIAL, call); 
startActivity(surf);

这不起作用,我收到一个错误信息,上面写着:

很遗憾,项目已停止。我尝试调试代码,看起来它指向了意图行,如果我只是这样做,它就能正常工作并打开拨号器。

//Uri call = Uri.parse("7777777777");               
Intent surf = new Intent(Intent.ACTION_DIAL);   
startActivity(surf);

可能是在Android中调用意图的重复问题。 - amadib
9个回答

24

电话

String number = "23454568678";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +number));
startActivity(intent);

使用权限

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

2
不应该使用Intent.ACTION_CALLManifest.permission.CALL_PHONE,而应该使用Intent.ACTION_DIAL,请参见https://support.google.com/googleplay/android-developer/answer/9047303。 - childno͡.de

20

要打开拨号应用程序(用户必须在拨号应用程序内按“呼叫”按钮;无需额外的权限),请使用:

String number = "7777777777";
Uri call = Uri.parse("tel:" + number);             
Intent surf = new Intent(Intent.ACTION_DIAL, call); 
startActivity(surf);

要自动打开拨号应用并拨打电话(需要android.permission.CALL_PHONE权限),请使用:

String number = "7777777777";
Uri call = Uri.parse("tel:" + number);             
Intent surf = new Intent(Intent.ACTION_CALL, call); 
startActivity(surf);

4

试试这个

String url="tel:777777777"
if (url.startsWith("tel:")) { 
  Intent intent = new Intent(Intent.ACTION_DIAL,
  Uri.parse(url)); 
  startActivity(intent);
}

将以下内容添加到您的AndroidManifest.xml文件中。
<uses-permission android:name="android.permission.CALL_PHONE" />

拨号意图无需获得权限。 - Ostkontentitan

4
尝试这个也可以。
Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phno);
startActivity(intent);

Android清单文件

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

2

另一种方法是创建一个稍后调用的PendingIntent。当您希望从通知操作直接将用户重定向到电话呼叫时,这种方法特别有用。

String number = "551191111113";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +number));
PendingIntent pendingIntentForCall = PendingIntent.getActivity(mContext, 0 /* Request code */, intent,PendingIntent.FLAG_ONE_SHOT);

您可以按照以下方式在通知中使用它:
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext)
                .setContentTitle(title)
                .setContentText(message)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setTicker(tickerText)
                .setColor(Color.BLACK)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_directions_bus_white_48dp))
                .setSmallIcon(R.mipmap.ic_directions_bus_white_24dp)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .addAction(new NotificationCompat.Action(R.mipmap.ic_directions_bus_white_24dp,"Call to " + number,pendingIntentForCall));

2

试试这个

String no = "536171839";
Intent callintent = new Intent(android.intent.action.CALL);
callintent.setData(Uri.parse("tel:" +no));
startActivity(callintent);

将此内容添加到您的AndroidManifest.xml文件中

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

2
尝试这个:

试一下:

String toCall = "tel:" + number.getText().toString();

startActivity(new Intent(Intent.ACTION_DIAL,

Uri.parse(toCall)));

1
如果您已经添加了。
 <uses-permission android:name="android.permission.CALL_PHONE" />

检查您的应用程序对手机通话的权限。

2
这应该是注释 - ketan
问题是关于ACTION_DIAL的,因此您不需要CALL_PHONE权限来执行该操作。 - Hadži Lazar Pešić

0

对于ACTION_DIAL,您只需要创建Intent对象,将该操作作为第一个参数和Uri对象作为第二个参数构建成字符串格式的电话号码。之后只需调用startActivity()方法,并将先前创建的意图对象作为参数传递。例如:

private String phoneNumber = "123456";

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

    Button dial_number = findViewById(R.id.button);
    dial_number.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
            startActivity(intent);
        }
    });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接