2010-10-06 26 views
11

Android, metin mesajı gönderirken teslimat amacını tetiklemiyor. Bunu HTC EVO 4G'de Android 2.2'de test ediyorum.Android SMS Mesajı yayınlanma bildirimi amacı

Geçerli kod budur. "SMS gönderilen niyet alındı" görüyorum. günlüklerde, ancak "SMS iletilen amaç alındı" değil.

// Constants 
String SENT_ACTION = "SMS_SENT_ACTION"; 
String DELIVERED_ACTION = "SMS_DELIVERED_ACTION"; 
String CELL_NUMBER = "0000000000"; 
String MESSAGE = "Hello World!"; 

// SMS sent pending intent 
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, 
     new Intent(SENT_ACTION), 0); 

// SMS delivered pending intent 
PendingIntent deliveredIntent = PendingIntent.getBroadcast(this, 0, 
     new Intent(DELIVERED_ACTION), 0); 

// SMS sent receiver 
registerReceiver(new BroadcastReceiver(){ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "SMS sent intent received."); 
    } 
}, new IntentFilter(SENT_ACTION)); 

// SMS delivered receiver 
registerReceiver(new BroadcastReceiver(){ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "SMS delivered intent received."); 
    } 
}, new IntentFilter(DELIVERED_ACTION)); 

// Send the SMS message 
SmsManager sms = SmsManager.getDefault(); 
sms.sendTextMessage(CELL_NUMBER, null, MESSAGE, sentIntent, deliveredIntent); 
+2

Thunderbolt'ta kurulu olan mevcut işletim sisteminden HTC, gönderilen ve gönderilen tüm SMS sonuçlarını tamamen geçersiz kılar. Mesaj başarılı olursa sonuç kodları yanıtlanabilir, ancak mesaj başarısız olursa HTC otomatik olarak tüm sonuç kodlarını geçersiz kılar, kodunuz yanmaz ve mesajı otomatik olarak tekrar gönderir. Temel olarak, bir SMS uygulaması için kod göndermeye ve bildirimler almaya çalışırken zamanınızı HTC cihazlarında harcayabiliyorsanız! HTC'ye bir e-posta göndermeye çalışacağım ve eğer bir cevap alırsam şok olacağım. –

+0

Herhangi bir cevap veya yeni haber var mı? – cprcrack

+0

bu tartışmaya bir göz atın: http://code.google.com/p/android/issues/detail?id=2305 – mtekeli

cevap

6

Çağrı SMS

private String SimState = ""; 
private String address = ""; // Recipient Phone Number 
private String message = ""; // Message Body 

private void sendSms() { 
    if (isSimExists()) { 
     try { 
      String SENT = "SMS_SENT"; 

      PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); 

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

      SmsManager smsMgr = SmsManager.getDefault(); 
      smsMgr.sendTextMessage(address, null, message, sentPI, null); 
     } catch (Exception e) { 
      Toast.makeText(this, e.getMessage() + "!\n" + "Failed to send SMS", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
    } else { 
     Toast.makeText(this, SimState + " " + "Cannot send SMS", Toast.LENGTH_LONG).show(); 
    } 
} 


// For receiving sms 

class SMSReceiver extends BroadcastReceiver { 
    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0) { 
      // Sms Received Your code here 
     } 
    } 
} 

Not göndermek istediğiniz bu yöntem: Ayrıca alıcıyı android.permission.SEND_SMS ve android.permission.RECEIVE_SMS manifest dosyasında izin ve belirtmek zorunda

<receiver android:name=".SMSReceiver" android:enabled="true"> 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.tekeli.order" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="11" /> 
    <uses-permission android:name="android.permission.SEND_SMS" ></uses-permission> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".ActivityOrderActivity" 
      android:label="@string/app_name"> 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".B" ></activity> 
     <activity android:name=".C"></activity> 
    </application> 

</manifest> 
+0

isSimExists() nedir? , isSimExists() yöntemi için kod snippet'ini de koyun. –

+2

@kirtiavaiya Plz http://androidsnippets.wordpress.com/2012/08/07/how-to-check-whether-sim-card-exists-or-not-in -android/isSimExists yöntemi için –

+0

Bu, sorduğu soru değil. SendTextMessage() işlevine sağlamadığınız teslim edilen PendingIntent hakkında soru soruyordu. – ocross