Android 中的呼叫意图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10510395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call intent in Android
提问by user1383729
How can I make call
by pressing button? I get my number as a string from EditText
. Here is my sample code:
我怎样才能call
通过按下按钮来制作?我从EditText
. 这是我的示例代码:
String phone = editPhone.getText().toString();
btnPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
call();
}
});
public void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(phone));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("myphone dialer", "Call failed", e);
}
}
I added all permissions
to manifest file.
我将所有内容添加permissions
到清单文件中。
but I am getting NullPointerexception
但我得到 NullPointerexception
回答by MKJParekh
This simple approach should work for you.
这种简单的方法应该适合您。
Ex.
前任。
public class CallActivity extends Activity{
String phone = "";
onCreate()
{
btnPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
phone = editPhone.getText().toString();
call();
}
});
}
public void call() {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+phone));
startActivity(callIntent);
}
}
You might be using String variable phone
out of scope.
您可能正在使用phone
超出范围的String 变量。
回答by Shashank Kadne
I think you missed the "tel:" part in the URI.
我认为您错过了 URI 中的“电话:”部分。
Replace the following..
替换以下..
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(phone));
startActivity(callIntent);
with
和
Intent callIntent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone));
startActivity(callIntent);
or
或者
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+phone));
startActivity(callIntent);
回答by Dhaval Parmar
see below code it may help you.
请参阅下面的代码,它可能对您有所帮助。
for call
打电话
EditText num = (EditText)findViewById(R.id.number_edit);
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
for open dialer
用于打开拨号器
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:"));
startActivity(dial);
回答by M.Saleh
String PhoneNo="+923341234567"
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + PhoneNo));
startActivity(intent);
and add a permission in manifest
并在清单中添加权限
<uses-permission android:name="android.permission.CALL_PHONE" />
回答by Shams Ud Din
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(phone));
startActivity(callIntent);
above is the only method you use for calling a phone number in android, when you click on the call button a 'dilerpad' activty is started with perpopulated phone number, and the call will go if you press the call button on the dialer pad.
以上是您在 android 中拨打电话号码的唯一方法,当您单击通话按钮时,会使用填充的电话号码启动“dilerpad”活动,如果您按下拨号键盘上的通话按钮,通话就会开始。
回答by Parveen Sharma
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" +"93");
intent.setData(Uri.parse(uri));
startActivity(intent);
回答by Mwongera808
Try this
尝试这个
EditText num = (EditText)findViewById(R.id.phone_number);
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(intent);