【问题标题】:Android BroadcastReceiver is not receiving anything (SMS)Android BroadcastReceiver 没有收到任何东西(短信)
【发布时间】:2014-10-15 15:55:29
【问题描述】:

我知道很多人有同样的问题,我在 stackoverflow 上阅读了很多,但对我来说仍然是 BroadcastReceivers 无法接收短信...

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.msit_hosting.bos_lage_fms"
    android:versionCode="5"
    android:versionName="0.0.1b" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <receiver android:name="de.msit_hosting.bos_lage_fms.SMSReceiver" android:exported="true" android:enabled="true" >
		  <intent-filter android:priority="2147483647">
		    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
		  </intent-filter>
		</receiver>
        
        <activity
            android:name=".FMSActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            android:configChanges="keyboardHidden|orientation|screenSize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="de.msit_hosting.bos_lage_fms.FMSActivity" />
        </activity>
        
        <service
        android:name=".PositionSendService"
        android:label="Position send Service" >
    	</service>
    	
        <receiver android:name="de.msit_hosting.bos_lage_fms.SMSReceiver" android:exported="true" android:enabled="true" >
		  <intent-filter android:priority="2000">
		    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
		  </intent-filter>
		</receiver>
    </application>

</manifest>

接收者:

package de.msit_hosting.bos_lage_fms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class SMSReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.e("SMS", "Receiver fired...");
		
		/*
		Bundle data = intent.getExtras();
		
		if(data != null) {
			Object[] sms_data = (Object[])data.get("pdus");
			
			for(int i = 0; i < sms_data.length; i++) {
				SmsMessage msg = SmsMessage.createFromPdu((byte[])sms_data[i]);
				
				String src = msg.getDisplayOriginatingAddress();
				String body = msg.getMessageBody();
			
				if(src != null && body != null) {
					Log.i("SMS", src + ": " + body);
				}
			}
		}
		*/
	}
	
}

出于测试原因,我注释掉了除日志消息之外的所有内容 - 但即使这样也没有被调用。我尝试了较低的优先级以及此处和其他论坛中推荐的其他内容,但没有任何效果。

对我来说很特别,它不是 Handcent 或类似的东西阻止了消息,因为即使在 Android 模拟器上也没有显示日志消息...

你能帮忙吗?

谢谢

梅林

【问题讨论】:

    标签: android sms broadcastreceiver broadcast


    【解决方案1】:

    请尝试以下更改:

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

    另外,根据您的操作系统版本,您可能需要安装应用程序,运行一次,然后您可能会看到结果。 如果这些更改中的任何一个解决了您的问题,则可能是:

    错误的类定位。您似乎附加了两倍的包名称。

    错误的优先级,谷歌文档声明开发者应该使用介于 -999 和 999 之间的整数值

    错误的版本启动。我相信在 3.1 版中,广播接收者需要一个活动才能正确注册。

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      相关资源
      最近更新 更多