我在5.1 OS以上尝试过Android SDK。我想使用SIM选择选项发送短信。我试过这个程序。它只接受sim1。它不需要第二个sim。
我的计划
public class MainActivity extends AppCompatActivity {
Context mContext = MainActivity.this ;
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendSMS("XXXXXXXX77","Sent by Dual Sim Support Testing");
}
public void sendSMS(final String paramString1,final String paramString2)
{
final PendingIntent localPendingIntent1 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.SENT), 0);
final PendingIntent localPendingIntent2 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.DELIVERED), 0);
Log.v("ppi", "DUal SIm:Build.VERSION.SDK_INT:" + Build.VERSION.SDK_INT);
if (Build.VERSION.SDK_INT >= 22)
{
SubscriptionManager localSubscriptionManager = SubscriptionManager.from(mContext);
if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1)
{
List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
final String[] arrayOfString = new String[localList.size()];
int i = 0;
Iterator localIterator = localList.iterator();
while (localIterator.hasNext())
{
SubscriptionInfo localSubscriptionInfo = (SubscriptionInfo)localIterator.next();
localSubscriptionInfo.getSubscriptionId();
//log.d("22 api level ", "got dual sim: ");
int j = i + 1;
arrayOfString[i] = (localSubscriptionInfo.getCarrierName().toString() + " " + localSubscriptionInfo.getNumber());
i = j;
}
Log.v("ppi","DUal SIm:arrayOfString:"+arrayOfString.length);
for(i = 0; i < arrayOfString.length; i++ ){
Log.v("ppi","DUal SIm:arrayOfString:"+arrayOfString[i]);
}
final AlertDialog.Builder localBuilder = new AlertDialog.Builder(mContext);
localBuilder.setTitle("Choose SIM ");
localBuilder
.setCancelable(false)
.setPositiveButton("SIM1",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
SmsManager.getDefault().sendTextMessage(paramString1, null, paramString2, localPendingIntent1, localPendingIntent2);
//dialog.cancel();
}
})
.setNegativeButton("SIM2", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
SmsManager.getDefault().sendTextMessage(paramString1, null, paramString2, localPendingIntent1, localPendingIntent2);
// dialog.cancel();
}
});
localBuilder.setCancelable(false);
localBuilder.show();
}
}
SmsManager.getDefault().sendTextMessage(paramString1, null, paramString2, localPendingIntent1, localPendingIntent2);
}
}
答案 0 :(得分:0)
那是因为你有:
SmsManager.getDefault()
这是SIM1,而不是:
SmsManager.getSmsManagerForSubscriptionId(int subscriptionId)
在对话框按钮中发送短信时,单击事件。
答案 1 :(得分:0)
我查看了SubscriptionManager类,发现了一个获取特定sim插槽的subsCription信息的方法
这对我有用:
public void sendSMS(final String number,final String text)
{
final PendingIntent localPendingIntent1 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.SENT), 0);
final PendingIntent localPendingIntent2 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.DELIVERED), 0);
if (Build.VERSION.SDK_INT >= 22)
{
SubscriptionManager subscriptionManager=((Activity)mContext).getSystemService(SubscriptionManager.class);
SubscriptionInfo subscriptionInfo=subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(simIndex);
SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.getSubscriptionId()).sendTextMessage(number, null, text, localPendingIntent1, localPendingIntent2);
}
SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.getSubscriptionId()).sendTextMessage(number, null, text, localPendingIntent1, localPendingIntent2);
}
其中simIndex对于sim1为0,对于sim2为1
答案 2 :(得分:0)
最后,我找到了以上android os 5.1及以上版本的精确模拟选择选项。
public void sendDualSimSMSOption(final String ToaddrmobileNo, final String message) {
// detect os is below 22 or not
if (Build.VERSION.SDK_INT >= 22) {
final PendingIntent PendingIntent1 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.SENT), 0);
final PendingIntent PendingIntent2 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.DELIVERED), 0);
SubscriptionManager localSubscripManager = SubscriptionManager.from(mContext);
ArrayList<Integer> saveSubscripID = new ArrayList<Integer>();
ArrayList<String> saveSimOperatorNames = new ArrayList<String>();
ArrayList<String> saveSimSerialNumbers = new ArrayList<String>();
final int[] subscripInformationArray = new int[2];
if (localSubscripManager.getActiveSubscriptionInfoCount() > 1) {
List localList = localSubscripManager.getActivesubscripInformationList();
final String[] arrayOfStringData = new String[localList.size()];
int i = 0;
Iterator localIterator = localList.iterator();
while (localIterator.hasNext()) {
SubscriptionInfo localSubscriptionInfo = (SubscriptionInfo) localIterator.next();
localSubscriptionInfo.getSubscriptionId();
Log.v("SIM", "22 api level..got dual sim: ");
int j = i + 1;
String SimCarrierName = (localSubscriptionInfo.getCarrierName().toString().trim());
String SimDisplayName = (localSubscriptionInfo.getDisplayName().toString().trim());
String SimSerialNo = (localSubscriptionInfo.getIccId().toString().trim());
if( SimCarrierName != null && SimCarrierName.length() > 0)
{
arrayOfStringData[i] = SimCarrierName ;
Log.v('SIM', "SIM.getCarrierName:", arrayOfStringData[i]);
}
else {
arrayOfStringData[i] = SimDisplayName ;
Log.v('SIM', "SIM.SimDisplayName:", arrayOfStringData[i]);
}
i = j;
}
SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext());
List<SubscriptionInfo> subscripInformationList = subscriptionManager.getActivesubscripInformationList();
for (SubscriptionInfo subscriptionInfo : subscripInformationList) {
int subscriptionId = subscriptionInfo.getSubscriptionId();
Log.v('SIM', "SIM.subscriptionId:",""+subscriptionId);
}
for (i = 0; i < subscripInformationList.size(); i++) {
Log.v("SIM", "DUal SIm:subscripInformationList:" + subscripInformationList.get(i));
Log.v("SIM", "DUal SIm:getSubscriptionId:" + subscripInformationList.get(i).getSubscriptionId());
subscripInformationArray[i] = subscripInformationList.get(i).getSubscriptionId();
saveSubscripID.add(subscripInformationList.get(i).getSubscriptionId());
saveSimSerialNumbers.add(subscripInformationList.get(i).getIccId());
}
for (i = 0; i < arrayOfStringData.length; i++) {
Log.v("SIM", "DUal SIm:arrayOfStringData:" + arrayOfStringData[i]);
Log.v("SIM", "DUal SIm:subscripInformationArray:"+ subscripInformationArray[i]);
if( arrayOfStringData[i] != null && arrayOfStringData[i].length() > 0)
{
saveSimOperatorNames.add(arrayOfStringData[i]);
}
else{
saveSimOperatorNames.add("Sim "+(i+1));
}
}
// this is the method to show sim selection dialog with above sim serials info.
SimSelection_ShowDialogBox(context, ToAddrMobNo, message,saveSubscripId, saveSimOperaNames,saveSimSerialNumbers);
}
}
else{
// else part. default sim taking to send sms
}
}