Android双卡打电话和发短信

相逢意气为君饮,系马高楼垂柳边。这篇文章主要讲述Android双卡打电话和发短信相关的知识,希望能为你提供帮助。
一、打电话
首先需要申请电话信息权限,androidManifest.xml权限配置如下所示。

1 < !-- 打电话的权限 --> 2< uses-permission android:name="android.permission.CALL_PHONE"/> 3< uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Android5.1及以前版本只需要在AndroidManifest.xml中配置完权限后就可以进行打电话,打电话方式如下:
1Intent intent = new Intent(Intent.ACTION_CALL); //android.intent.action.CALL 2//2). 携带数据 3String number = et_main_number.getText().toString(); 4intent.setData(Uri.parse("tel:"+number)); 5//3). startActivity(intent) 6startActivity(intent);

对于Android5.1以后的版本,除了AndroidManifest.xml权限配置以外,还需要通过用户允许获取权限,代码如下所示:
1//检测用户是否同意权限 2private void checkPermission(int id) { 3//判断所申请的权限是不是已经通过,没通过返回false,通过返回true,则提示出来并拨打电话 4if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { 5//申请权限回调函数 6ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_REQUEST_CODE); 7} else { 8Toast.makeText(this, "权限已被申请通过咯!", Toast.LENGTH_SHORT).show(); 9call(id); 10} 11} 12 13//权限申请回调方法 14@Override 15public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 16super.onRequestPermissionsResult(requestCode, permissions, grantResults); 17switch (requestCode){ 18case PERMISSION_REQUEST_CODE: 19//同意申请的权限 20if (grantResults.length> 0& & grantResults[0]== PackageManager.PERMISSION_GRANTED){ 21Toast.makeText(this,"同意通过",Toast.LENGTH_SHORT).show(); 22 //call(); 23//拒绝申请的权限 24}else{ 25Toast.makeText(this,"拒绝通过",Toast.LENGTH_SHORT).show(); 26} 27break; 28default: 29break; 30} 31 32}

获取到电话权限后,可以使用以下代码进行双卡选择打电话,id为0表示卡一,id为1表示卡二,适用于Android5.1以后的方法,代码如下:
1 package com.example.phone; 2 3 import android.Manifest; 4 import android.app.Activity; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.content.pm.PackageManager; 8 import android.net.Uri; 9 import android.os.Bundle; 10 import android.support.annotation.NonNull; 11 import android.support.v4.app.ActivityCompat; 12 import android.support.v4.content.ContextCompat; 13 import android.telecom.PhoneAccountHandle; 14 import android.telecom.TelecomManager; 15 import android.view.View; 16 import android.widget.Button; 17 import android.widget.EditText; 18 import android.widget.Toast; 19 20 import java.util.List; 21 22 public class MainActivity extends Activity { 23 24private static final int PERMISSION_REQUEST_CODE = 1; 25private EditText et_main_number; 26private Button btn_main_call1; 27private Button btn_main_call2; 28private View.OnClickListener onClickListener = new View.OnClickListener() { 29@Override 30public void onClick(View v) { 31if(v==btn_main_call1){ 32checkPermission(0); 33} 34else if(v==btn_main_call2){ 35checkPermission(1); 36} 37} 38}; 39 40@Override 41protected void onCreate(Bundle savedInstanceState) { 42super.onCreate(savedInstanceState); 43setContentView(R.layout.activity_main); 44 45//初始化视图对象 46et_main_number = (EditText) findViewById(R.id.et_main_number); 47btn_main_call1 = (Button) findViewById(R.id.btn_main_call1); 48btn_main_call2 = (Button) findViewById(R.id.btn_main_call2); 49//给视图对象设置点击监听 50btn_main_call1.setOnClickListener(onClickListener); 51btn_main_call2.setOnClickListener(onClickListener); 52} 53 54//检测用户是否同意权限 55private void checkPermission(int id) { 56//判断所申请的权限是不是已经通过,没通过返回false,通过返回true,则提示出来并拨打电话 57if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { 58//申请权限回调函数 59ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_REQUEST_CODE); 60} else { 61Toast.makeText(this, "权限已被申请通过咯!", Toast.LENGTH_SHORT).show(); 62call(id); 63} 64} 65 66private void call(int id) { 67Intent intent = new Intent(Intent.ACTION_CALL).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //android.intent.action.CALL 68//2). 携带数据 69String number = et_main_number.getText().toString(); 70intent.setData(Uri.parse("tel:" + number)); 71 72TelecomManager telecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE); 73if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { 74// TODO: Consider calling 75//ActivityCompat#requestPermissions 76// here to request the missing permissions, and then overriding 77//public void onRequestPermissionsResult(int requestCode, String[] permissions, 78//int[] grantResults) 79// to handle the case where the user grants the permission. See the documentation 80// for ActivityCompat#requestPermissions for more details. 81return; 82} 83List< PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts(); 84intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,phoneAccountHandleList.get(id)); 85//3). startActivity(intent) 86startActivity(intent); 87} 88//权限申请回调方法 89@Override 90public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 91super.onRequestPermissionsResult(requestCode, permissions, grantResults); 92switch (requestCode){ 93case PERMISSION_REQUEST_CODE: 94//同意申请的权限 95if (grantResults.length> 0& & grantResults[0]== PackageManager.PERMISSION_GRANTED){ 96Toast.makeText(this,"同意通过",Toast.LENGTH_SHORT).show(); 97 //call(); 98//拒绝申请的权限 99}else{ 100Toast.makeText(this,"拒绝通过",Toast.LENGTH_SHORT).show(); 101} 102break; 103default: 104break; 105} 106 107} 108 }

activity_main.xml代码如下:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7android:orientation="vertical" 8tools:context=".MainActivity"> 9 10< LinearLayout 11android:layout_width="match_parent" 12android:layout_height="wrap_content"> 13< TextView 14android:layout_width="wrap_content" 15android:layout_height="wrap_content" 16android:text="电话号码: " /> 17< EditText 18android:id="@+id/et_main_number" 19android:layout_width="200dp" 20android:layout_height="wrap_content" 21android:hint="请输入号码"> 22< /EditText> 23< /LinearLayout> 24 25< LinearLayout 26android:layout_width="match_parent" 27android:layout_height="wrap_content"> 28< Button 29android:id="@+id/btn_main_call1" 30android:layout_width="wrap_content" 31android:layout_height="wrap_content" 32android:text="卡1" /> 33< Button 34android:id="@+id/btn_main_call2" 35android:layout_width="wrap_content" 36android:layout_height="wrap_content" 37android:text="卡2" /> 38< /LinearLayout> 39 < /LinearLayout>

完成以上操作,即可实现双卡选择打电话。
二、发短信
首先需要申请电话信息权限,AndroidManifest.xml权限配置如下所示。
1 < !-- 发短信的权限 --> 2< uses-permission android:name="android.permission.SEND_SMS"/>

除了AndroidManifest.xml权限配置以外,还需要通过用户允许获取权限,代码如下所示:
1//检测用户是否同意权限 2private void checkPermission(int id) { 3//判断所申请的权限是不是已经通过,没通过返回false,通过返回true,则提示出来并发送短信 4if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { 5//申请权限回调函数 6ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST_CODE); 7} else { 8Toast.makeText(this, "权限已被申请通过咯!", Toast.LENGTH_SHORT).show(); 9message(id); 10} 11} 12 13 //权限申请回调方法 14@Override 15public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 16super.onRequestPermissionsResult(requestCode, permissions, grantResults); 17switch (requestCode){ 18case PERMISSION_REQUEST_CODE: 19//同意申请的权限 20if (grantResults.length> 0& & grantResults[0]== PackageManager.PERMISSION_GRANTED){ 21Toast.makeText(this,"同意通过",Toast.LENGTH_SHORT).show(); 22//拒绝申请的权限 23}else{ 24Toast.makeText(this,"拒绝通过",Toast.LENGTH_SHORT).show(); 25} 26break; 27default: 28break; 29} 30 31}

获取到短信权限后,可以使用以下代码进行双卡选择打电话,id为0表示卡一,id为1表示卡二,代码如下:
1 package com.example.message; 2 3 import android.Manifest; 4 import android.app.Activity; 5 import android.app.PendingIntent; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.content.pm.PackageManager; 9 import android.os.Bundle; 10 import android.support.annotation.NonNull; 11 import android.support.v4.app.ActivityCompat; 12 import android.support.v4.content.ContextCompat; 13 import android.telephony.SmsManager; 14 import android.telephony.SubscriptionManager; 15 import android.view.View; 16 import android.widget.Button; 17 import android.widget.EditText; 18 import android.widget.Toast; 19 20 import java.lang.reflect.InvocationTargetException; 21 import java.lang.reflect.Method; 22 23 public class MainActivity extends Activity{ 24 25private static final int PERMISSION_REQUEST_CODE = 1; 26private EditText et_main_number; 27private EditText et_main_sms; 28private Button btn_main_send; 29 30private View.OnClickListener onClickListener = new View.OnClickListener() { 31@Override 32public void onClick(View v) { 33if(v==btn_main_send) { 34//id=0为卡一,id=1为卡二 35checkPermission(0); 36} 37} 38}; 39@Override 40protected void onCreate(Bundle savedInstanceState) { 41super.onCreate(savedInstanceState); 42setContentView(R.layout.activity_main); 43 44//初始化视图对象 45et_main_number = (EditText) findViewById(R.id.et_main_number); 46et_main_sms = findViewById(R.id.et_main_sms); 47btn_main_send = (Button) findViewById(R.id.btn_main_send); 48//给视图对象设置点击监听 49btn_main_send.setOnClickListener(onClickListener); 50} 51 52//检测用户是否同意权限 53private void checkPermission(int id) { 54//判断所申请的权限是不是已经通过,没通过返回false,通过返回true,则提示出来并拨打电话 55if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { 56//申请权限回调函数 57ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST_CODE); 58} else { 59Toast.makeText(this, "权限已被申请通过咯!", Toast.LENGTH_SHORT).show(); 60message(id); 61} 62} 63private void message(int id) { 64SubscriptionManager smanager = (SubscriptionManager) getSystemService(TELEPHONY_SUBSCRIPTION_SERVICE); 65Class sclazz = smanager.getClass(); 66Method getSubId = null; 67try { 68getSubId = sclazz.getDeclaredMethod("getSubId",int.class); 69}catch (NoSuchMethodException e){ 70e.printStackTrace(); 71} 72int[] ints = new int[1]; 73try { 74ints = (int[])getSubId.invoke(smanager,id); 75}catch (IllegalAccessException e){ 76e.printStackTrace(); 77}catch (InvocationTargetException e){ 78e.printStackTrace(); 79} 80SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(ints[0]); 81PendingIntent sentIntent = PendingIntent.getBroadcast(this,0, new Intent(Context.TELEPHONY_SUBSCRIPTION_SERVICE),PendingIntent.FLAG_ONE_SHOT); 82String number = et_main_number.getText().toString(); 83String sms = et_main_sms.getText().toString(); 84smsManager.sendTextMessage(number,null,sms,sentIntent,null); 85} 86//权限申请回调方法 87@Override 88public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 89super.onRequestPermissionsResult(requestCode, permissions, grantResults); 90switch (requestCode){ 91case PERMISSION_REQUEST_CODE: 92//同意申请的权限 93if (grantResults.length> 0& & grantResults[0]== PackageManager.PERMISSION_GRANTED){ 94Toast.makeText(this,"同意通过",Toast.LENGTH_SHORT).show(); 95//拒绝申请的权限 96}else{ 97Toast.makeText(this,"拒绝通过",Toast.LENGTH_SHORT).show(); 98} 99break; 100default: 101break; 102} 103 104} 105 }

activity_main.xml代码如下:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7android:orientation="vertical" 8tools:context=".MainActivity"> 9 10< LinearLayout 11android:layout_width="match_parent" 12android:layout_height="wrap_content" > 13< TextView 14android:layout_width="wrap_content" 15android:layout_height="wrap_content" 16android:text="电话号码: " /> 17< EditText 18android:id="@+id/et_main_number" 19android:layout_width="fill_parent" 20android:layout_height="wrap_content" 21android:hint="请输入号码"> 22< /EditText> 23< /LinearLayout> 24 25< LinearLayout 26android:layout_width="match_parent" 27android:layout_height="wrap_content" > 28< TextView 29android:layout_width="wrap_content" 30android:layout_height="wrap_content" 31android:text="短信内容: " /> 32< EditText 33android:id="@+id/et_main_sms" 34android:layout_width="fill_parent" 35android:layout_height="wrap_content" 36android:hint="请输入短信"> 37< /EditText> 38< /LinearLayout> 39< LinearLayout 40android:layout_width="match_parent" 41android:layout_height="wrap_content" > 42< Button 43android:id="@+id/btn_main_send" 44android:layout_width="wrap_content" 45android:layout_height="wrap_content" 46android:text="发短信" /> 47< /LinearLayout> 48 49 < /LinearLayout>

【Android双卡打电话和发短信】完成以上操作后,即可实现双卡选择发送短信。

    推荐阅读