2013-04-05 54 views
7

我正在尝试使用Intent.Action类。我知道如何使用ACTION_VIEW来显示URL,但我想在启动应用程序时使用Intent.ACTION_DIAL来拨打电话号码。该文件说,你需要解析URI成一个字符串,然后把它添加到我尝试这样做的意图:Android Intent.ACTION_CALL,Uri

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

可能重复[调用Android中意图(http://stackoverflow.com/questions/10510395/call-intent-in- android) – amadib 2015-03-12 18:35:11

回答

22

tel

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> 
1

试试这个

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> 
13

要只需打开拨号应用程式(用户必须按拨号应用程式内呼叫按钮;不需要额外的权限)使用方法:

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

试试这个

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

尝试,这也

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

试试这个:

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

      startActivity(new Intent(Intent.ACTION_DIAL, 

        Uri.parse(toCall))); 
0

如果妳已经添加

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

检查调用的权限在手机上为哟你的申请。

+1

这应该是评论 – ketan 2016-03-30 06:46:08

1

另一种方法是使以后调用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));