知识点
1:安卓6.0以上的版本如何获取拨打电话和发送短信的“权限”
2.如何进入手机通讯录并返回联系号码
3.如何实现发送短信的功能
//实现拨打电话功能的Activity
package com.example.administrator.phone;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
private Button btn_dial;
private EditText et_number;
private Button ml_btn;
private Button ss_btn;
private static final int PICK_CONTACT = 1;
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件
et_number = (EditText) findViewById(R.id.number_et);
btn_dial = (Button) findViewById(R.id.dial_btn);
ml_btn=findViewById(R.id.ml_btn);
ss_btn=findViewById(R.id.ss_btn);
ss_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ActivityB.class);
intent.putExtra("number",et_number.getText().toString());
//启动Activity B
startActivity(intent);
//结束MainActivity
MainActivity.this.finish();
}
});
ml_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectConnection();
}
});
// 给按钮设置监听(点击事件)
btn_dial.setOnClickListener(new View.OnClickListener() { // 匿名内部类
// 按钮点击时回调
@Override
public void onClick(View view) {
// 检查是否获得了权限(Android6.0运行时权限)
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
// 没有获得授权,申请授权
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.CALL_PHONE)) {
// 返回值:
// 如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
// 如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
// 如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
// 弹窗需要解释为何需要该权限,再次请求授权
Toast.makeText(MainActivity.this, "请授权!", Toast.LENGTH_LONG).show();
// 帮跳转到该应用的设置界面,让用户手动授权
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}else{
// 不需要解释为何需要该权限,直接请求授权
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
}
}else {
// 已经获得授权,可以打电话
CallPhone();
}
}
});
}
private void CallPhone() {
String number = et_number.getText().toString();
if (TextUtils.isEmpty(number)) {
// 提醒用户
// 注意:在这个匿名内部类中如果用this则表示是View.OnClickListener类的对象,
// 所以必须用MainActivity.this来指定上下文环境。
Toast.makeText(MainActivity.this, "号码不能为空!", Toast.LENGTH_SHORT).show();
} else {
// 拨号:激活系统的拨号组件
Intent intent = new Intent(); // 意图对象:动作 + 数据
intent.setAction(Intent.ACTION_CALL); // 设置动作
Uri data = Uri.parse("tel:" + number); // 设置数据
intent.setData(data);
startActivity(intent); // 激活Activity组件
}
}
// 处理权限申请的回调
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode){
case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 授权成功,继续打电话
CallPhone();
} else {
// 授权失败!
Toast.makeText(this, "授权失败!", Toast.LENGTH_LONG).show();
}
break;
}
}
}
private void selectConnection() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_CONTACT:
Intent mIntent;
mIntent = data;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
//申请授权,第一个参数为要申请用户授权的权限;第二个参数为requestCode 必须大于等于0,主要用于回调的时候检测,匹配特定的onRequestPermissionsResult。
//可以从方法名requestPermissions以及第二个参数看出,是支持一次性申请多个权限的,系统会通过对话框逐一询问用户是否授权。
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
}else{
//如果该版本低于6.0,或者该权限已被授予,它则可以继续读取联系人。
getContacts(data);
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
private void getContacts(Intent data) {
if (data == null) {
return;
}
Uri contactData = data.getData();
if (contactData == null) {
return;
}
String name = "";
String phoneNumber = "";
Uri contactUri = data.getData();
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
if (cursor.moveToFirst()) {
name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String id = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
if (hasPhone.equalsIgnoreCase("1")) {
hasPhone = "true";
} else {
hasPhone = "false";
}
if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
while (phones.moveToNext()) {
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
cursor.close();
et_number.setText(phoneNumber);
}
}
}
//实现发送短信功能的Activity
package com.example.administrator.phone;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.List;
public class ActivityB extends Activity {
EditText ss_number_et;
Button cs_btn;
Button ss_btn;
EditText ss_content_et;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
ss_number_et=findViewById(R.id.ss_number_et);
cs_btn=findViewById(R.id.cs_btn);
ss_btn=findViewById(R.id.ss_btn);
ss_content_et=findViewById(R.id.ss_content_et);
ss_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = ss_content_et.getText().toString();//短信内容
String phoneNo = ss_number_et.getText().toString();//电话号码
Log.i("Send SMS", "");
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
}
}
});
cs_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(ActivityB.this,MainActivity.class);
startActivity(intent);
ActivityB.this.finish();
}
});
Intent i=getIntent();
String number=i.getStringExtra("number");
if(number==null){
ss_number_et.setText("请输入手机号码");
}else{
ss_number_et.setText(number);
}
}
}
运行结果:
初始界面
进入通讯录界面
获取联系人返回主界面
拨打电话界面
编辑短信界面
参考博客
https://blog.csdn.net/u010356768/article/details/79352323
权侵删