在Android Manifest.xml中接收短信

4

我尝试在我的应用程序中接收短信,我认为问题出在我的AndroidManifest.xml文件中。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.amin_amini.smstestmobi"
      android:versionCode="1"
      android:versionName="1.0.0">
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:name=".SMS"
                  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=".SmsReceiver"
                  android:enabled="true"
                  android:exported="true"
                  android:permission="android.permission.BROADCAST_SMS"> 
            <intent-filter android:priority="1000"> 
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
            </intent-filter> 
        </receiver> 
    </application>
</manifest>

我的SmsReceive.java文件如下:
package com.amin_amini.smstestmobi;

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 str = "";            
    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]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }                   
    Toast.makeText(context, "Hey", Toast.LENGTH_SHORT).show();
    }
}

我相信我的问题在manifest.xml中,因为

 Toast.makeText(context, "Hey", Toast.LENGTH_SHORT).show();

不要显示任何东西。
我该怎么办?

编辑: 正如我们下面的朋友所说,我使用了

    Log.i("Hey","Hey" );

我的函数中没有任何东西显示在LogCat中,因此我确定我的问题在于我的AndroidManifest.xml文件。注意:我正在使用HTC Sensation上的Android 4.0.3。


不要使用Toast来调试广播接收器,而应该使用日志消息或断点。我甚至不确定你是否可以从其中之一触发Toast。 - Thomas Dignan
是的,我非常确定context是应用程序上下文,而不是活动上下文。也无法显示对话框。请使用Log.i(*)。 - edthethird
我正在这里做类似的事情!!! https://dev59.com/j-o6XIcBkEYKwwoYORwV - Etienne Lawlor
2个回答

4

将您的接收器代码更改为以下内容,这将有助于您。如果仍然无法正常工作,则可能是您安装了其他短信应用程序,这些应用程序具有更高的优先级并将中止广播服务。

     <receiver android:name=".SmsReceiver"> 
        <intent-filter android:priority="1000">
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="PACKAGE_NAME.android.action.broadcast"/> 
             <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter> 
    </receiver>

谢谢你的帮助,但我已经尝试了那个方法,它在真实设备上仍然不起作用,我也没有另一个短信应用程序。我已经在5个Android设备上测试过,但它在其中都无法工作。我在onReceive的第一行中加入了Log.i("Received","Received"),但在LogCat中没有任何日志记录:( - Amin
感谢您的帮助,最终我发现我的问题出在我的手机上。当我看到它不工作时,我改变了我的AndroidManifest.xml文件,然后在其他设备上进行了测试,但它仍然无法工作。我的手机有一个定制的ROM(MIUI),我认为它的短信应用程序优先级比任何其他应用程序都要高,所以我将我的sendMessage更改为sendDataMessage,并更改了我的接收器,使我可以在我的应用程序中接收它 :) 感谢你的帮助,伙计 ;) - Amin
@user1905965,太好了,你做得很棒,继续保持并在stackoverflow.com上发布任何问题,因为这个论坛是我们所有人中最好的一个。 - Pir Fahim Shah

1

是的,你的代码在这里运行良好。

这是我的Android清单文件。

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

我已经添加了这些权限

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

或者下载这个代码。我已经制作好了它,它可以接收短信并在对话框中显示。


感谢您的帮助,但我之前已经添加了这些权限,而且我从Dropbox下载了您的代码,但它没有显示任何内容,我只在我的短信应用程序中看到了我的短信 :( - Amin
尝试使用日志而不是Toast。并在广播接收器的不同位置放置一些日志,这将有助于您进行调试。尝试一下吧。 - Zohaib
尝试在Android模拟器中运行它。检查它是否正常工作。 - Zohaib
它在模拟器(Android 2.2)上运行良好,但在真实设备上无法运行:( - Amin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接