第四十二篇----拨打电话
界面:
MainActivity.java
package com.example.aimee.phone; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { public static final int REQUEST_CALL_PERMISSION = 10111; private Button bt_phone; private EditText et_phone; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_phone = (Button) findViewById(R.id.button); et_phone = (EditText) findViewById(R.id.editview); bt_phone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (checkReadPermission(Manifest.permission.CALL_PHONE, REQUEST_CALL_PERMISSION)) { Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + et_phone.getText())); startActivity(intent); } } }); } public boolean checkReadPermission(String string_permission,int request_code) { boolean flag = false; if (ContextCompat.checkSelfPermission(MainActivity.this, string_permission) == PackageManager.PERMISSION_GRANTED) {//已有权限 flag = true; } else {//申请权限 ActivityCompat.requestPermissions(MainActivity.this, new String[]{string_permission}, request_code); } return flag; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case REQUEST_CALL_PERMISSION: //拨打电话 if (permissions.length != 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {//失败 Toast.makeText(MainActivity.this, "请允许拨号权限后再试", Toast.LENGTH_SHORT).show(); } else {//成功 // call("tel:"+"10086"); } break; } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context=".MainActivity"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/phonetext" /> <EditText android:id="@+id/editview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/phonenumber" app:layout_constraintTop_toBottomOf="@+id/textview"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/callphone" app:layout_constraintTop_toBottomOf="@+id/editview"/> </android.support.constraint.ConstraintLayout>
最后,不要忘记在清单文件中添加权限
<uses-permission android:name="android.permission.CALL_PHONE"/>
小提示
1.可以自己设置快捷输入方式.
Settings-->Editor-->Live Templates,查看常用语句的快捷输入法,比如output里面的sout,当你在代码中输入sout按回车,那么就会自动补全System.out.println();如果点击右边的加号可以自己定义输入快捷方式。
2.常用快捷键Alt+Enter
一般代码飘红,可以尝试使用Alt+Enter
另外,输入(Button)你打印find会出现findViewById();然后填入R.id.button后将鼠标定位到括号后面,Alt+Enter,会自动补全成Button button = (Button) findViewById(R.id.button);此时你可以更换button名字,或者直接按enter就行了。如果你想要Button button在onCreate外面定义,可以按Ctrl+alt+f,它就会自动生成变量。