Sms Sent Receiver未收到意图

时间:2011-12-30 10:04:49

标签: android sms broadcastreceiver

我正在开发一个sms应用程序,如果我将MessageURI作为SMS的意图中的数据传递,则无法接收“SENT”意图。发生异常并且sms处于排队状态。 OnReceive未被调用!!!

public class Sms_SendActivity extends Activity {
    PendingIntent sentPI;
    String Sent = "SENT_SMS";
    BroadcastReceiver br;
    Button btnSend;
    Context mcontext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Uri uri = Uri.parse("Content://sms/2");
        mcontext = getApplicationContext();
        sentPI = PendingIntent.getBroadcast(this,0,new Intent(Sent,uri),0);
        btnSend = (Button)findViewById(R.id.send);
br = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                Log.w("Check","Inside On Receiver");
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(mcontext,"Inside Sms sent", Toast.LENGTH_SHORT).show();
                    Log.w("Check"," URI "+intent.getData().toString());
                    break;

                default:
                    Toast.makeText(mcontext,"failure", Toast.LENGTH_SHORT).show();
                    break;
                }
                unregisterReceiver(br);
            }
        };

        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage("1-212-555-1212", null, "Hi there", sentPI, null);
                Log.w("Check","Sms Queued");
                try {
                    registerReceiver(br, new IntentFilter(Sent,"content://sms"));
                } catch (MalformedMimeTypeException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

    }
}

请你帮助我!!!

谢谢

1 个答案:

答案 0 :(得分:0)

我对你的代码进行了一些修改。就我而言,它有效:

public class DynBroadCastSMSActivity extends Activity {


private static final String TAG = DynBroadCastSMSActivity.class.getSimpleName();

PendingIntent sentPI;
String Sent = "SMS_SENT";
Button btnSend;
Context mContext;

BroadcastReceiver br = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("Check", "Inside On Receiver");
    }
};

@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction(Sent);
    filter.addDataScheme("mycontent");
    registerReceiver(br, filter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(br);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Uri uri = Uri.parse("mycontent://sms");
    Intent intent = new Intent(Sent);
    intent.setData(uri);
    mContext = getApplicationContext();
    sentPI = PendingIntent.getBroadcast(this, 0, intent, 0);
    btnSend = (Button) findViewById(R.id.send);



    btnSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage("5554", null, "Hi there", sentPI,
                    null);
            Log.w("Check", "Sms Queued");
        }
    });

}

}

问题如下。 Android无法识别称为“内容”的数据方案。如果我称它为“mycontent”,一切正常。您可以使用此代码来了解更多详细信息。