0

我想开发一个短信通知器。问题是当收到短信时,活动中的文本框只需更改为“收到短信”文本。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;
    }
}
4

3 回答 3

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();
    }
于 2012-08-09T09:02:38.737 回答
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);        
    }  
于 2012-08-09T09:23:26.453 回答
0

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

于 2015-11-27T04:07:54.773 回答