2017-02-05 11 views
1

私はこのコードを使用していますが、動作していません(SMSは送信されていません)。エラーまたは例外が表示されません。何が問題なのか教えてください。SMSが送信されていません。

コード:

SmsManager smsManager = SmsManager.getDefault(); 

String simDetails = "Your device's SIM Details Are:\n"+"\nDevice ID: "+telephony.getDeviceId()+"\nSubscriber ID: "+telephony.getSubscriberId() 
       +"\nSIM Serial Number: "+telephony.getSimSerialNumber()+"\nNetwork Operator: "+telephony.getNetworkOperator()+"\nNetwork Operator Name: "+telephony.getNetworkOperatorName() 
       +"\nNetwork Country: "+telephony.getNetworkCountryIso()+"\nSIM Country: "+telephony.getSimCountryIso()+"\nSIM Operator: "+telephony.getSimOperator()+"\nSIM Operator Name: "+telephony.getSimOperatorName() 
       +"\nSoftware Version: "+telephony.getDeviceSoftwareVersion()+"\nGroup Id Level-1: "+telephony.getGroupIdLevel1()+"\nMMS UAP: "+telephony.getMmsUAProfUrl()+"\nMMS User Agent: "+telephony.getMmsUserAgent() 
       +"\nVoice Mail Tag: "+telephony.getVoiceMailAlphaTag()+"\nVoice Mail Number: "+telephony.getVoiceMailNumber()+"\nLine-1 Number: "+telephony.getLine1Number()+"SIM Location: "+telephony.getCellLocation(); 

smsManager.sendTextMessage("receiver's number", null, simDetails, null, null); 
+1

あなたが必要な権限を持っていて、捕まったExceptionをどこかで無視していなければ、それはあなたの問題のようです送信しているテキストが 'sendTextMessage()'には長すぎるということです。それを分割し、 'sendMultipartTextMessage()'を使う必要があります。 –

答えて

0

あなたが読んでいることを確認し、SMSの許可(マシュマロ> =)

Manifest.permission.RECEIVE_SMS 
ActivityCompat.checkSelfPermission(context,Manifest.permission.RECEIVE_SMS); 
0

を送るあなたはこれらの権限が含まれていましたか?

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.SEND_SMS"/> 
<uses-permission android:name="android.permission.READ_CONTACTS" /> 

とAndroid M上記のデバイスのためにそれらを確認することを確認してください。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (context.checkSelfPermission(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) { 
      //All Permissions are granted 
      return true; 
     } else { 
      ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.SEND_SMS}, YOURPERMISSIONRESULTCODEINT); 
      //permissions are not granted so this check has failed 
      return false; 
     } 
    } 
    //The device is <Android M and so manifest declarations should be enough 
    return true; 
} 

はそれを繰り返すか(許可グループになるだろう)文字列配列内の各パーミッションについて別々のチェックを追加します。ネットワークマネージャのために

LocationManager locationManager = (LocationManager) context 
      .getSystemService(context.LOCATION_SERVICE); 
// getting network status 
boolean isNetworkEnabled = locationManager 
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

少数のSMSの詳細:

String SENT = "SMS_SENT"; 
String DELIVERED = "SMS_DELIVERED"; 
final SmsManager smsManager = SmsManager.getDefault(); 
//Allows you to detect when an SMS has been sent via an application wide broadcast 
final PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, 
       new Intent(SENT), 0); 
//Same as above but is instead when it is delivered 
final PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, 
       new Intent(DELIVERED), 0); 

その後、SMSが送信されるか、またはその送信時にされた後のために放送受信機を登録します。

context.registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context arg0, Intent arg1) { 
       switch (getResultCode()) { 
        case Activity.RESULT_OK: 
         Toast.makeText(context, "SMS sent", 
           Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
         Toast.makeText(context, "Generic failure", 
           Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_NO_SERVICE: 
         Toast.makeText(context, "No service", 
           Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_NULL_PDU: 
         Toast.makeText(context, "Null PDU", 
           Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_RADIO_OFF: 
         Toast.makeText(context, "Radio off", 
           Toast.LENGTH_SHORT).show(); 
         break; 
       } 
      } 
     }, new IntentFilter(SENT)); 

     //---when the SMS has been delivered else this code won't run--- 
     context.registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context arg0, Intent arg1) { 
       switch (getResultCode()) { 
        case Activity.RESULT_OK: 
         Toast.makeText(context, "SMS delivered", 
           Toast.LENGTH_SHORT).show(); 
         break; 
        case Activity.RESULT_CANCELED: 
         Toast.makeText(context, "SMS not delivered", 
           Toast.LENGTH_SHORT).show(); 
         break; 
       } 
      } 
     }, new IntentFilter(DELIVERED)); 
     //Then send your message 
     smsManager.sendTextMessage("phoneNumber", null, "Message", sentPI, deliveredPI); 

    } 
関連する問題