Android短信动态广播

时间:2014-09-16 12:11:32

标签: android sms broadcast

我有一个程序正在通过短信向设备发送短信。 我希望收到短信,然后将短信发回给我的节目。 因为我只会等待一段时间才需要动态注册

这里是面包广播接收者的代码

public class SMSReceiver extends BroadcastReceiver {

    private MainActivity father = null;

    private String call_number = null;

    public void setFather (MainActivity father) {
        this.father = father;
        call_number = father.getCallNumber().trim();
        while (call_number.startsWith("0")) {
            call_number = call_number.substring(1);
        }
    }

    public void onReceive(Context cxt, Intent intent)
        {
            //if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
            {
                Bundle bundle = intent.getExtras();
                Object messages[] = (Object[]) bundle.get("pdus");
                SmsMessage smsMessage[] = new SmsMessage[messages.length];

                for (int n = 0; n < messages.length; n++)
                    smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);

                String sender = smsMessage[0].getDisplayOriginatingAddress();
                String mess = smsMessage[0].getDisplayMessageBody();

                String toast = "Received SMS from: " + sender + "\nMessage: " + mess;

                //if ((sender != null) && (sender.endsWith(call_number))) {
                    father.setSMSMessage((mess ==null ? "": mess)+ "\n from "+sender);
                //}


                Toast.makeText(cxt, toast, Toast.LENGTH_LONG).show();
            }

        }

}

这是我的注册代码

myreceiver=new SMSReceiver();
myreceiver.setFather(this);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(myreceiver, filter);

这是我的AndroidManifest.xml

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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=".SMSChecker">
            <intent-filter>
                <action android:name="SMS_SENT" />
                <action android:name="SMS_DELIVERED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

我的问题是我从设备收到短信但我的接收器什么也没做。 评论接收方发送SMS无任何问题

有人知道这个问题吗?

0 个答案:

没有答案