实现拨打电话功能
1.首先清单文件中添加权限
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
2.activity中的代码
package com.example.study;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
//判断版本号
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
//判断是否允许拨打电话的权限
requestPermissions(new String[]{Manifest.permission.CALL_PHONE},101);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==101&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
//"tel:"是固定格式 后面添加拨打的电话号码
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+"110"));
startActivity(intent);
}
}
}