2017-04-17 54 views
0

我找到了一些code來做到這一點,但它給了我一個例外(嘗試在空對象引用上調用虛方法'java.lang.Class java.lang.Object.getClass()')。我正在測試我的Moto G 3rd Gen.下面是代碼,請讓我知道如果我缺少任何東西或任何其他方法可用。謝謝。Android:如何在雙卡手機中發送特定SIM卡的短信?

import android.app.PendingIntent; 
import android.content.Context; 
import android.os.IBinder; 
import android.util.Log; 

import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 


public class SimUtil { 

    private static final String TAG = "SimUtil"; 

    public static boolean sendSMS(Context ctx, int simID, String toNum, String centerNum, String smsText, PendingIntent sentIntent, PendingIntent deliveryIntent) { 

     SimUtil cls = new SimUtil(); 

     String name; 

     try { 
      if (simID == 0) { 
       name = "isms"; 

      } else if (simID == 1) { 
       name = "isms2"; 

      } else { 
       throw new Exception("can not get service which for sim '" + simID + "', only 0,1 accepted as values"); 
      } 

      try 
      { 
       Method method = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class}); 
       method.setAccessible(true); 
       Object param = method.invoke(null, new Object[]{name}); 

       if (param == null) 
       { 
        throw new RuntimeException("can not get service which is named '" + name + "'"); 
       } 
       method = Class.forName("com.android.internal.telephony.ISms$Stub").getDeclaredMethod("asInterface", new Class[]{IBinder.class}); 
       method.setAccessible(true); 
       Object stubObj = method.invoke(null, new Object[]{param}); 

       if (stubObj == null) 
       { 
        throw new RuntimeException("can not get telephony which is named '" + name + "'"); 
       } 


       method = stubObj.getClass().getMethod("sendText", String.class, String.class, String.class, String.class, PendingIntent.class, PendingIntent.class); 

       method.invoke(null, new Object[]{ctx.getPackageName(), toNum, null, smsText, sentIntent, deliveryIntent}); 


      } catch (ClassNotFoundException e) 
      { 
       throw new RuntimeException(e); 
      } catch (NoSuchMethodException e) 
      { 
       throw new RuntimeException(e); 
      } catch (InvocationTargetException e) 
      { 
       throw new RuntimeException(e); 
      } catch (IllegalAccessException e) 
      { 
       throw new RuntimeException(e); 
      } 

      return true; 
     } catch (ClassNotFoundException e) { 
      Log.e("Exception", "ClassNotFoundException:" + e.getMessage()); 
     } catch (NoSuchMethodException e) { 
      Log.e("Exception", "NoSuchMethodException:" + e.getMessage()); 
     } catch (InvocationTargetException e) { 
      Log.e("Exception", "InvocationTargetException:" + e.getMessage()); 
     } catch (IllegalAccessException e) { 
      Log.e("Exception", "IllegalAccessException:" + e.getMessage()); 
     } catch (Exception e) { 
      Log.e("Exception", "Exception:" + e); 
     } 
     return false; 
    } 
+0

如果您定位的Android API 22或更高版本,Android已經對雙卡設備的支持。 https://developer.android.com/about/versions/android-5.1.html – shr3jn

+0

@ shr3jn你能否給我一個例子? –

+0

我已經回答了你的問題。 – shr3jn

回答

0

如果您的目標是API 22或更高版本。您可以使用以下代碼獲取SIM卡列表。您還可以允許用戶選擇SIM卡:

final ArrayList<Integer> simCardList = new ArrayList<>(); 
SubscriptionManager subscriptionManager; 
subscriptionManager = SubscriptionManager.from(activity); 
final List<SubscriptionInfo> subscriptionInfoList = subscriptionManager 
        .getActiveSubscriptionInfoList(); 
for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) { 
    int subscriptionId = subscriptionInfo.getSubscriptionId(); 
    simCardList.add(subscriptionId); 
} 

最後使用下面的代碼發送短信:

int smsToSendFrom = simCardList.get(0); //assign your desired sim to send sms, or user selected choice 
SmsManager.getSmsManagerForSubscriptionId(smsToSendFrom) 
          .sendTextMessage(phoneNumber, null, msg, sentPI, deliveredPI); //use your phone number, message and pending intents 
相關問題