0

我正在编写一个短信接收器 android 代码,但它不工作。它会编译,但是当我收到一条短信时,它并没有像使用 Toast 时那样显示。这是我的代码:

package com.example.homecontrolingbysms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String messageReceived = "";            
        if (bundle != null)
        {
            //---retrieve the SMS message received---
           Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length]; 

            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);  
                messageReceived +="From "+ msgs[i].getOriginatingAddress();
                messageReceived+=" : ";
                messageReceived += msgs[i].getMessageBody().toString();
                messageReceived += "\n";        
            }

            //---display the new SMS message---
            Toast.makeText(context, messageReceived, Toast.LENGTH_SHORT).show();
        }                         
    }
}

我还发布了清单以确保代码的所有重要部分都可用
manifest.xml:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.homecontrolingbysms.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>

       <activity
            android:name="com.example.homecontrolingbysms.Door"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.example.homecontrolingbysms.Window"
            android:label="@string/app_name" >

        </activity>

        <activity
            android:name="com.example.homecontrolingbysms.Light"
            android:label="@string/app_name" >

        </activity>

    </application>

</manifest>
4

3 回答 3

2

通过在清单文件中添加以下代码来注册 smsreceiver

<receiver android:name="com.shreymalhotra.smsreceiver.SmsReceiver">
    <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

此链接可能会对您有所帮助。

在 Android 应用上接收短信

http://shreymalhotra.me/blog/tutorial/receive-sms-using-android-broadcastreceiver-inside-an-activity/

请检查一下。

于 2013-05-03T11:51:26.233 回答
0

我认为您的清单看起来不错;问题出在这条线上:错误在这部分我想你能改变这个吗

    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String messageReceived = "";            
    if (bundle != null)

像这样

    public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "Intent recieved: " + intent.getAction());
    intent.getAction().equals(ACTION) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[])bundle.get("pdus");

(您需要对操作字符串进行对象比较,或者不进行比较),在您的清单文件中,您拼错了 SMS_RECEIVED。

于 2013-05-03T11:55:03.640 回答
0

如果你想接收一些广播,必须做自爆的事情。1.在manifest.xml中,你必须用intent filter exp声明你的receiver: 2.如果你要接收的广播是有序的,你必须确定你的优先级。安卓:优先级=“1000”

于 2013-05-03T12:00:31.000 回答