Java android studio 调用按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32654580/
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
android studio calling button
提问by Drew Bennett
I am trying to make an on click button which makes phone calls when pressed. Here is my code for java:
我正在尝试制作一个点击按钮,该按钮在按下时拨打电话。这是我的Java代码:
public void CampusSafClick(View view){
Intent callIntent =new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:6038994210"));
startActivity(callIntent);
}
I understand how to make onclick buttons, so that is not the issue.
我了解如何制作 onclick 按钮,所以这不是问题。
I have this code in the manifest:
我在清单中有这个代码:
<uses-permission android:name="android.permision.CALL_PHONE"></uses-permission>
I keep getting the error Unfortunately your app has stop working.
我不断收到错误 不幸的是,您的应用程序已停止工作。
采纳答案by Pranav Barot
ivCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (serviceListDates.get(position).getUser_mobile().length() != 0) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("NKA SERVICE");
alertDialog.setMessage("Do you want to Call ?");
alertDialog.setIcon(R.drawable.call_icon);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
((DeliveredServiceOilActivity) mContext).callPhoneNumber
(serviceListDates.get(position).getUser_mobile());
}
});`enter code here
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
} else
AlertUtils.SHOW_TOAST(mContext, mContext.getString(R.string.please_add_number));
}
});
回答by Nikola Milutinovic
Here is the working code:
这是工作代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialContactPhone("123123123");
}
});
}
private void dialContactPhone(final String phoneNumber) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phoneNumber, null)));
}
回答by Drew Bennett
I need to enter code above the application list on the manifest:
我需要在清单上的应用程序列表上方输入代码:
<uses-permission android:name="android.permission.CALL_PHONE"/>
回答by Tabish khan
You need Action_Dial
,
你需要Action_Dial
,
use below code it will open Dealer with number specified.
使用下面的代码,它将打开指定编号的经销商。
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
startActivity(intent);
The tel:
prefix is required, otherwhise the following exception will be thrown: java.lang.IllegalStateException: Could not execute method of the activity.
该tel:
前缀是必需的,otherwhise以下将引发异常:java.lang.IllegalStateException: Could not execute method of the activity.
Action_Dial
doesn't require any permission.
Action_Dial
不需要任何许可。
If you want to initiate the call directly without user's interaction, you can use action Intent.ACTION_CALL
. In this case, you must add the following permission in your AndroidManifest.xml
:
如果您想在没有用户交互的情况下直接发起呼叫,您可以使用 action Intent.ACTION_CALL
。在这种情况下,您必须在您的 中添加以下权限AndroidManifest.xml
:
<uses-permission android:name="android.permission.CALL_PHONE" />
回答by Codemaker
For make a call using android, It can be implement using Intents.
对于使用android拨打电话,可以使用Intents来实现。
public void MakePhoneCall(View view){
Intent callIntent =new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9961907453"));
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
I have this code in the manifest:
我在清单中有这个代码:
<uses-permission android:name="android.permision.CALL_PHONE"></uses-permission>
If you are using SDK version greater than Lolipop then you should include request permission.
如果您使用的 SDK 版本高于 Lolipop,那么您应该包括请求权限。
回答by Abdulhakim Zeinu
You can use the following code
您可以使用以下代码
intent =new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:+251999999999"));
startActivity(intent);
and include in manifest file
并包含在清单文件中
<uses-permission android:name="android.permission.CALL_PHONE"/>