2014-01-05 31 views
0

我正在創建使用廣播接收器發送消息並獲取發送報告的android應用程序。 應用程序發送消息,但我沒有收到任何遞送報告,通知我消息已發送。廣播接收器發送消息並獲取發送報告不起作用

誰能幫助我解決這個問題

清單

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 

    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <uses-permission android:name="android.permission.SEND_SMS" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.devleb.smssenddemo.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

     <receiver android:name=".SMS" > 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_SEND" > 
       </action> 
      </intent-filter> 
     </receiver> 

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

    </application> 

</manifest> 

MainActivity.java

package com.devleb.smssenddemo; 

import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.telephony.gsm.SmsManager; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    EditText edit_txt_phone; 
    EditText edit_txt_SMS; 
    Button btn; 

    BroadcastReceiver smsDelivered = new SMSdelivered(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     edit_txt_phone = (EditText) findViewById(R.id.editText1); 

     edit_txt_SMS = (EditText) findViewById(R.id.editText2); 

     btn = (Button) findViewById(R.id.btn); 
     btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       String strphone = edit_txt_phone.getText().toString(); 
       String strSMS = edit_txt_SMS.getText().toString(); 

       if (strphone.length() > 0 && strSMS.length() > 0) { 
        sendSMS(strphone, strSMS); 
       } else { 
        Toast.makeText(getBaseContext(), 
          "plz enter the phone or the sms", 
          Toast.LENGTH_SHORT).show(); 
       } 

      } 

      private void sendSMS(String phoneNo, String SMS) { 
       // TODO Auto-generated method stub 

       PendingIntent pi = PendingIntent.getActivity(getBaseContext(), 
         0, new Intent(getBaseContext(), SMS.class), 0); 

       PendingIntent piDelevered = PendingIntent.getActivity(
         getBaseContext(), 0, new Intent(getBaseContext(), 
           SMSdelivered.class), 0); 

       registerReceiver(smsDelivered, 
         new IntentFilter("SMS DELIVERED")); 


       SmsManager sms = SmsManager.getDefault(); 
       sms.sendTextMessage(phoneNo, null, SMS, pi, null); 

      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

SMS.java

package com.devleb.smssenddemo; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.gsm.SmsMessage; 
import android.widget.Toast; 

public class SMS extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     Bundle bundle = intent.getExtras(); 
     SmsMessage[] msgs = null; 
     String str = ""; 

     if (bundle != null) { 
      Object[] pdus = (Object[]) bundle.get("pdus"); 
      msgs = new SmsMessage[pdus.length]; 

      for (int i = 0; i < pdus.length; i++) { 
       msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
       str += "SMS from" + msgs[i].getOriginatingAddress(); 
       str += " :"; 
       str += msgs[i].getMessageBody().toString(); 
       str += "\n"; 

      } 
      Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

SMSdelivered.java

package com.devleb.smssenddemo; 

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 

public class SMSdelivered extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 

      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; 
      } 

     } 

    } 

回答

0

你應該改變的IntentFilter以匹配action名稱:http://developer.android.com/reference/android/content/IntentFilter.html#IntentFilter(java.lang.String)

公衆的IntentFilter(字符串操作):要匹配的作用,即Intent.ACTION_MAIN。

然後,您註冊了一個廣播,但您是否在某處發送廣播以便稍後接收?

我經常這樣做:

Intent broadcastIntent = new Intent(); 
broadcastIntent.setAction("com.your.package.broadcast"); 
broadcastIntent.putExtra("result", result); 
sendBroadcast(broadcastIntent); 

這種接受它:

IntentFilter filter = new IntentFilter(); 
filter.addAction("com.your.package.broadcast"); 
registerReceiver(new MyBroadcast(), filter); 

並沒有在清單。