【发布时间】:2016-01-25 18:51:02
【问题描述】:
我在清单中注册了一个广播接收器,如下所示:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".SMSReceiver" android:permission="android.permission.RECEIVE_SMS" android:exported="true">
<intent-filter android:priority="9999">
<action android:name="android.provider.telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
我的 SMSReceiver 类是这样的:
public class SMSReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
...
但是 onReceive 方法中的代码永远不会被调用。我试过在接收器中没有 android:exported 和 android:permission 标签,但没有任何效果。
【问题讨论】:
-
您是否在安装后至少启动了一次
MainActivity以使应用退出停止状态?另外,从<receiver>元素中删除android:permission="android.permission.RECEIVE_SMS"。 -
是的,我做到了。然后我用 android 任务管理器杀死应用程序
-
那么这很可能是强行停止您的应用程序,并将其恢复为停止状态。在您再次启动应用程序之前,您的接收器将无法再次工作。
-
实际上,我刚刚注意到,您想将该权限更改为
BROADCAST_SMS,因为您没有指定该权限。 -
telephony需要在<receiver>的<action>中大写。即"android.provider.Telephony.SMS_RECEIVED"。抱歉,我早就注意到了,但我认为您的标题意味着它在应用运行时确实工作。