android BroadCastReceiver无法在某个活动中运行

时间:2012-08-09 08:57:55

标签: android sms broadcastreceiver

我想开发一个短信通知器。事情就是当SMS收到活动中的文本框时,只需更改为“SMS Received”文本。 SMS已成功发送但BroadCastReceiver无法正常工作,请提供帮助。

我的代码:

package com.shahid.todolist;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        /*
         * SmsManager smsManager = SmsManager.getDefault();
         * 
         * String sendTo = "03129912287"; String myMessage =
         * "Android supports programmatic SMS messaging!";
         * smsManager.sendTextMessage(sendTo, null, myMessage, null, null);
         */

        final EditText phoneNo = (EditText) findViewById(R.id.phoneNo);
        final TextView display = (TextView) findViewById(R.id.txtDisplay);
        Button buttonOne = (Button) findViewById(R.id.button1);
        buttonOne.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                SmsManager smsManager = SmsManager.getDefault();
                display.setText("Sending SMS...");
                String sendTo = phoneNo.getText().toString();
                String myMessage = "This is Shahid from Android";
                smsManager.sendTextMessage(sendTo, null, myMessage, null, null);
            }
        });

        // ---
        final BroadcastReceiver receiver = new BroadcastReceiver() {

            private static final String queryString = "@echo ";
            private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

            @Override
            public void onReceive(Context context, Intent _intent) {
                if (_intent.getAction().equals(SMS_RECEIVED)) {
                    display.setText("SMS Received");
                }
            }
        };
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

3 个答案:

答案 0 :(得分:1)

您必须在活动中注册广播。

@Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BroadcastReceiver.SMS_RECEIVED);  // Provide you intent filter for message received.
        registerReceiver(receiver, filter);

        super.onResume();
    }

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

答案 1 :(得分:0)

使用此代码:

buttonOne.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {

                display.setText("Sending SMS...");
                String sendTo = phoneNo.getText().toString();
                String myMessage = "This is Shahid from Android";
               sendSMS(sendTo , myMessage );
            }
        });



private void sendSMS(String phoneNumber, String message)
    {        
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

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

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

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

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                       display.setText("SMS Received");

                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();


                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
    }  

答案 2 :(得分:0)

如果这对某人有帮助,在我的情况下问题是我在文本中使用括号之类的特殊字符。一旦我删除了这些字符,它就开始工作了