2017-04-17 7 views
0

이 작업을 수행하려면 code이 발견되었지만 예외가 발생했습니다 (null 객체 참조에서 가상 메소드 'java.lang.Class java.lang.Object.getClass()'호출). 내 Moto G 3 Gen와 테스트 중입니다. 코드가 있습니다. 누락 된 부분이 있거나 다른 방법이 있으면 알려주십시오. 고맙습니다.Android : 듀얼 시뮬레이션 모바일에서 특정 SIM을 사용하여 SMS를 보내려면 어떻게해야하나요?

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

타겟팅하는 경우 안드로이드 API 22 이상, 안드로이드는 듀얼 시뮬레이션 장치에 대한 지원을하고있다 . https://developer.android.com/about/versions/android-5.1.html – shr3jn

+0

@ shr3jn 예제를 제공해 주시겠습니까? –

+0

귀하의 질문에 답변했습니다. – shr3jn

답변

0

API 22 이상을 타겟팅하는 경우. 다음 코드를 사용하여 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); 
} 

마지막으로 다음 코드를 사용하여 SMS를 보낼 : 당신은 또한 사용자가 SIM 카드를 선택하도록 할 수 있습니다

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