java - 安卓 : sms receiver is not working

标签 java android sms toast

我正在编写一个短信接收器 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();
        }                         
    }
}

我还发布了 list 以确保代码的所有重要部分均可用
list .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>

最佳答案

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

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

此链接可能对您有帮助。

Receiving SMS on Android App

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

请检查一下。

关于java - 安卓 : sms receiver is not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16357869/

相关文章:

Java OkHttp3 只使用 Http/1.1 或 Http/2

android - 如果仅设置大图标,则不会显示通知

java - 从适配器中的模型传输日期格式

java - 为什么这个 ReadWriteLock 示例不起作用?

c# - 气泡图\注入(inject)图片而不是气泡

android - android :gravity in attrs. xml 的正确格式?

character-encoding - SIM800L空白短信问题

android - 注册过程中通过来自 api 的消息进行手机号码验证,例如 "viber"for android

javascript - 我想在 View 端 javascript 中使用 Controller 中的数据

java - 在同一个 Activity/布局中多次使用同一个 fragment