广播接收器不工作 [英] BroadcastReceiver isn't working

查看:125
本文介绍了广播接收器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该广播接收机没有不会被触发,但在另一个我的应用程序的工作,我用来检查网络状态。我没有别的要说一下这个问题。

None of this broadcast receivers are not triggered, but in another my application is working where I used to check network state. I have nothing else to say about this problem.

package monitor;

import android.content.BroadcastReceiver;

public class Monitor extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {
          System.out.println("jupi");
    }
}



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="monitor"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk
    android:minSdkVersion="9" 
    android:targetSdkVersion="15"/>

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application 
    android:icon="@drawable/icon"
    android:label="@string/app_name">
    <service 
        android:name=".MonitorService">
    </service>
    <receiver 
        android:name=".Monitor">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

</application>

推荐答案

如果您看到了这个现象在Android 3.1或更高版本,你可能看到这种变化的结果:

If you are seeing this behaviour on Android 3.1 or later, you are probably seeing the results of this change:

http://developer.android.com/about/versions/ Android的3.1.html#launchcontrols

特别是这样说的:

请注意,该系统增加了FLAG_EXCLUDE_STOPPED_PACKAGES所有
  广播意图。它这样做是从后台prevent广播
  从无意或不必要的启动组件服务
  stoppped应用程序。

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications.

由于您的应用程序只包含一个服务,接收器,用户将不会开始此应用程序,因此,如果将保持在停止状态,$ P $看不到任何广播意图pventing它。

Since your application only contains a service and receivers, the user will never start this application and therefore if will remain in the "stopped" state, preventing it from seeing any broadcast intents.

当您添加活动到您的应用程序,运行它的一次足以除去从停止状态的应用程序,使你的接收机当时有资格接收广播意图。你其实并不需要动态注册接收器。这是不够的,只是让用户在安装后启动应用一次。

When you added the Activity to your application, running it once was enough to remove the application from the "stopped" state so that your receivers were then eligible to receive broadcast intents. You actually don't need to dynamically register the receivers. It is enough to just have the user "start" the application once after installation.

这篇关于广播接收器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆