版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bbtianshi/article/details/83540755
首先看下布局xml ,我的事一个button按钮 点击 打电话 ,手机号是自己定义,这个你可以根据自己逻辑写
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".call.CallphoneActivity">
<Button
android:id="@+id/btn"
android:text="打电话"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
下面看 activity 对应 的 定义一个string ==123456789,假设手机号 ,对应还有电话权限 ,6.0要动态权限这里就不写了
//<uses-permission android:name="android.permission.CALL_PHONE"/>电话权限
在button按钮点击触发
public class CallphoneActivity extends AppCompatActivity {
Button btn;
String ss= "123456789";
//<uses-permission android:name="android.permission.CALL_PHONE"/>电话权限
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_callphone);
btn=findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建打电话的意图
Intent intent = new Intent();
//设置拨打电话的动作
intent.setAction(Intent.ACTION_CALL);
//设置拨打电话的号码
intent.setData(Uri.parse("tel:" + ss));
//开启打电话的意图
startActivity(intent);
}
});
}
}