繁体   English   中英

Android Intent.ACTION_CALL, Uri

[英]Android Intent.ACTION_CALL, Uri

我正在尝试使用 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);

电话

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>   

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

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);

试试这个

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" />

也试试这个

Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phno);
startActivity(intent);

安卓清单

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

试试这个

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>

试试这个 :

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

startActivity(new Intent(Intent.ACTION_DIAL,

Uri.parse(toCall)));

另一种方法是让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));

如果你添加了

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

检查您的应用程序的电话呼叫权限。

对于 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);
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM