广播接收器发送消息和获取发送报告不起作用

时间:2014-01-05 22:02:50

标签: android sms broadcastreceiver

我正在创建使用广播接收器发送消息并返回发送报告的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;
            }

        }

    }

1 个答案:

答案 0 :(得分:0)

您应该更改IntentFilter以匹配action名称:http://developer.android.com/reference/android/content/IntentFilter.html#IntentFilter(java.lang.String)

  

public IntentFilter(String action):要匹配的操作,即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);

在Manifest中没有任何内容。