一、打电话
首先需要申请电话信息权限,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中配置完权限后就可以进行打电话,打电话方式如下:
1 Intent intent = new Intent(Intent.ACTION_CALL);//android.intent.action.CALL 2 //2). 携带数据 3 String number = et_main_number.getText().toString(); 4 intent.setData(Uri.parse("tel:"+number)); 5 //3). startActivity(intent) 6 startActivity(intent);
对于Android5.1以后的版本,除了AndroidManifest.xml权限配置以外,还需要通过用户允许获取权限,代码如下所示:
1 //检测用户是否同意权限 2 private void checkPermission(int id) { 3 //判断所申请的权限是不是已经通过,没通过返回false,通过返回true,则提示出来并拨打电话 4 if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { 5 //申请权限回调函数 6 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_REQUEST_CODE); 7 } else { 8 Toast.makeText(this, "权限已被申请通过咯!", Toast.LENGTH_SHORT).show(); 9 call(id); 10 } 11 } 12 13 //权限申请回调方法 14 @Override 15 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 16 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 17 switch (requestCode){ 18 case PERMISSION_REQUEST_CODE: 19 //同意申请的权限 20 if (grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED){ 21 Toast.makeText(this,"同意通过",Toast.LENGTH_SHORT).show(); 22 // call(); 23 //拒绝申请的权限 24 }else{ 25 Toast.makeText(this,"拒绝通过",Toast.LENGTH_SHORT).show(); 26 } 27 break; 28 default: 29 break; 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 24 private static final int PERMISSION_REQUEST_CODE = 1; 25 private EditText et_main_number; 26 private Button btn_main_call1; 27 private Button btn_main_call2; 28 private View.OnClickListener onClickListener = new View.OnClickListener() { 29 @Override 30 public void onClick(View v) { 31 if(v==btn_main_call1){ 32 checkPermission(0); 33 } 34 else if(v==btn_main_call2){ 35 checkPermission(1); 36 } 37 } 38 }; 39 40 @Override 41 protected void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.activity_main); 44 45 //初始化视图对象 46 et_main_number = (EditText) findViewById(R.id.et_main_number); 47 btn_main_call1 = (Button) findViewById(R.id.btn_main_call1); 48 btn_main_call2 = (Button) findViewById(R.id.btn_main_call2); 49 //给视图对象设置点击监听 50 btn_main_call1.setOnClickListener(onClickListener); 51 btn_main_call2.setOnClickListener(onClickListener); 52 } 53 54 //检测用户是否同意权限 55 private void checkPermission(int id) { 56 //判断所申请的权限是不是已经通过,没通过返回false,通过返回true,则提示出来并拨打电话 57 if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { 58 //申请权限回调函数 59 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_REQUEST_CODE); 60 } else { 61 Toast.makeText(this, "权限已被申请通过咯!", Toast.LENGTH_SHORT).show(); 62 call(id); 63 } 64 } 65 66 private void call(int id) { 67 Intent intent = new Intent(Intent.ACTION_CALL).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//android.intent.action.CALL 68 //2). 携带数据 69 String number = et_main_number.getText().toString(); 70 intent.setData(Uri.parse("tel:" + number)); 71 72 TelecomManager telecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE); 73 if (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. 81 return; 82 } 83 List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts(); 84 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,phoneAccountHandleList.get(id)); 85 //3). startActivity(intent) 86 startActivity(intent); 87 } 88 //权限申请回调方法 89 @Override 90 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 91 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 92 switch (requestCode){ 93 case PERMISSION_REQUEST_CODE: 94 //同意申请的权限 95 if (grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED){ 96 Toast.makeText(this,"同意通过",Toast.LENGTH_SHORT).show(); 97 // call(); 98 //拒绝申请的权限 99 }else{ 100 Toast.makeText(this,"拒绝通过",Toast.LENGTH_SHORT).show(); 101 } 102 break; 103 default: 104 break; 105 } 106 107 } 108 }
activity_main.xml代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".MainActivity"> 9 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content"> 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="电话号码: " /> 17 <EditText 18 android:id="@+id/et_main_number" 19 android:layout_width="200dp" 20 android:layout_height="wrap_content" 21 android:hint="请输入号码"> 22 </EditText> 23 </LinearLayout> 24 25 <LinearLayout 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content"> 28 <Button 29 android:id="@+id/btn_main_call1" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:text="卡1" /> 33 <Button 34 android:id="@+id/btn_main_call2" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="卡2" /> 38 </LinearLayout> 39 </LinearLayout>
完成以上操作,即可实现双卡选择打电话。
二、发短信
首先需要申请电话信息权限,AndroidManifest.xml权限配置如下所示。
1 <!-- 发短信的权限 --> 2 <uses-permission android:name="android.permission.SEND_SMS"/>
除了AndroidManifest.xml权限配置以外,还需要通过用户允许获取权限,代码如下所示:
1 //检测用户是否同意权限 2 private void checkPermission(int id) { 3 //判断所申请的权限是不是已经通过,没通过返回false,通过返回true,则提示出来并发送短信 4 if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { 5 //申请权限回调函数 6 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST_CODE); 7 } else { 8 Toast.makeText(this, "权限已被申请通过咯!", Toast.LENGTH_SHORT).show(); 9 message(id); 10 } 11 } 12 13 //权限申请回调方法 14 @Override 15 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 16 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 17 switch (requestCode){ 18 case PERMISSION_REQUEST_CODE: 19 //同意申请的权限 20 if (grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED){ 21 Toast.makeText(this,"同意通过",Toast.LENGTH_SHORT).show(); 22 //拒绝申请的权限 23 }else{ 24 Toast.makeText(this,"拒绝通过",Toast.LENGTH_SHORT).show(); 25 } 26 break; 27 default: 28 break; 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 25 private static final int PERMISSION_REQUEST_CODE = 1; 26 private EditText et_main_number; 27 private EditText et_main_sms; 28 private Button btn_main_send; 29 30 private View.OnClickListener onClickListener = new View.OnClickListener() { 31 @Override 32 public void onClick(View v) { 33 if(v==btn_main_send) { 34 //id=0为卡一,id=1为卡二 35 checkPermission(0); 36 } 37 } 38 }; 39 @Override 40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 setContentView(R.layout.activity_main); 43 44 //初始化视图对象 45 et_main_number = (EditText) findViewById(R.id.et_main_number); 46 et_main_sms = findViewById(R.id.et_main_sms); 47 btn_main_send = (Button) findViewById(R.id.btn_main_send); 48 //给视图对象设置点击监听 49 btn_main_send.setOnClickListener(onClickListener); 50 } 51 52 //检测用户是否同意权限 53 private void checkPermission(int id) { 54 //判断所申请的权限是不是已经通过,没通过返回false,通过返回true,则提示出来并拨打电话 55 if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { 56 //申请权限回调函数 57 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST_CODE); 58 } else { 59 Toast.makeText(this, "权限已被申请通过咯!", Toast.LENGTH_SHORT).show(); 60 message(id); 61 } 62 } 63 private void message(int id) { 64 SubscriptionManager smanager = (SubscriptionManager) getSystemService(TELEPHONY_SUBSCRIPTION_SERVICE); 65 Class sclazz = smanager.getClass(); 66 Method getSubId = null; 67 try { 68 getSubId = sclazz.getDeclaredMethod("getSubId",int.class); 69 }catch (NoSuchMethodException e){ 70 e.printStackTrace(); 71 } 72 int[] ints = new int[1]; 73 try { 74 ints = (int[])getSubId.invoke(smanager,id); 75 }catch (IllegalAccessException e){ 76 e.printStackTrace(); 77 }catch (InvocationTargetException e){ 78 e.printStackTrace(); 79 } 80 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(ints[0]); 81 PendingIntent sentIntent = PendingIntent.getBroadcast(this,0, new Intent(Context.TELEPHONY_SUBSCRIPTION_SERVICE),PendingIntent.FLAG_ONE_SHOT); 82 String number = et_main_number.getText().toString(); 83 String sms = et_main_sms.getText().toString(); 84 smsManager.sendTextMessage(number,null,sms,sentIntent,null); 85 } 86 //权限申请回调方法 87 @Override 88 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 89 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 90 switch (requestCode){ 91 case PERMISSION_REQUEST_CODE: 92 //同意申请的权限 93 if (grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED){ 94 Toast.makeText(this,"同意通过",Toast.LENGTH_SHORT).show(); 95 //拒绝申请的权限 96 }else{ 97 Toast.makeText(this,"拒绝通过",Toast.LENGTH_SHORT).show(); 98 } 99 break; 100 default: 101 break; 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" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".MainActivity"> 9 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" > 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="电话号码: " /> 17 <EditText 18 android:id="@+id/et_main_number" 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:hint="请输入号码"> 22 </EditText> 23 </LinearLayout> 24 25 <LinearLayout 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" > 28 <TextView 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="短信内容: " /> 32 <EditText 33 android:id="@+id/et_main_sms" 34 android:layout_width="fill_parent" 35 android:layout_height="wrap_content" 36 android:hint="请输入短信"> 37 </EditText> 38 </LinearLayout> 39 <LinearLayout 40 android:layout_width="match_parent" 41 android:layout_height="wrap_content" > 42 <Button 43 android:id="@+id/btn_main_send" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:text="发短信" /> 47 </LinearLayout> 48 49 </LinearLayout>
完成以上操作后,即可实现双卡选择发送短信。