失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Android 如何调用系统直接拨打电话的功能

Android 如何调用系统直接拨打电话的功能

时间:2021-04-12 08:19:36

相关推荐

Android 如何调用系统直接拨打电话的功能

正好用到这个小方法,就自己记录下来吧!

1.新建一个Android工程

2.打开res/layout/activity_main.xml

3.编辑activity_main.xml,添加如下代码:

<Button

android:id="@+id/button1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="64dp"

android:text="移动" />

<Button

android:id="@+id/button2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button1"

android:layout_below="@+id/button1"

android:layout_marginTop="30dp"

android:text="联通" />

<Button

android:id="@+id/button3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button2"

android:layout_below="@+id/button2"

android:layout_marginTop="34dp"

android:text="电信" />

4.打开MainActivity.java,并编辑,添加如下代码:

public class MainActivity extends Activity implements OnClickListener{

private Button btn1 ;

private Button btn2 ;

private Button btn3 ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initview();

}

private void initview(){

btn1 = (Button) findViewById(R.id.button1);

btn2 = (Button) findViewById(R.id.button2);

btn3 = (Button) findViewById(R.id.button3);

btn1.setOnClickListener(this);

btn2.setOnClickListener(this);

btn3.setOnClickListener(this);

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()) {

case R.id.button1:

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:10086"));

startActivity(intent);

break;

case R.id.button2:

Intent intent1 = new Intent(Intent.ACTION_CALL,Uri.parse("tel:10010"));

startActivity(intent1);

break;

case R.id.button3:

Intent intent2 = new Intent(Intent.ACTION_CALL,Uri.parse("tel:10000"));

startActivity(intent2);

break;

}

}

}

5.打开AndroidManifest.xml,添加拨打电话的权限:

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

必须添加这条权限,否则无法拨打电话!!!

这样就结束了,就是这么简单

如果觉得《Android 如何调用系统直接拨打电话的功能》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。