【问题标题】:Android - The best way to keep the broadcastreceiver workingAndroid - 保持广播接收器工作的最佳方式
【发布时间】:2012-09-21 22:10:31
【问题描述】:

我希望我的应用程序能够工作并监听传入的 Intent,即使活动已关闭。最好的方法是什么?

我在清单上注册了一个接收器:

    <receiver
        android:name="com.farawayapp.background.Receiver"
        android:exported="false" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.intent.action.PACKAGE_CHANGED" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />

            <data android:scheme="package" />
        </intent-filter>
    </receiver>

BroadcastReceiver 类是:

public class Receiver extends BroadcastReceiver implements Variables {

    CheckConexion cc;

    @Override
    public void onReceive(Context contxt, Intent intent) {

        // Cuando hay un evento, lo diferenciamos y hacemos una acción.

        if (intent.getAction().equals(SMS_RECEIVED)) {
            Sms sms = new Sms(null, contxt);
            sms.uploadNewSms(intent);
        } else if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
            /*
             * try { new PhoneState(contxt).battery(intent.getIntExtra("level",
             * 0)); } catch (JSONException e) { e.printStackTrace(); }
             */// Nothing at the moment
        } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)
                || intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED)
                || intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
            Database db = new Database(contxt);
            if (db.open().Preferences(4)) {
                Uri data = intent.getData();
                new ListApps(contxt).import_app(intent, contxt, data,
                        intent.getAction());
            }
            db.close();
        } else if (intent.getAction().equals(
                ConnectivityManager.CONNECTIVITY_ACTION)) {
            cc = new CheckConexion(contxt);

            if (cc.isOnline()) {

                Database db = new Database(contxt);
                db.open();
                if (db.move() == 1) {
                    new UploadOffline(contxt);
                }
                db.close();

            }
        }
    }
}

谢谢...

【问题讨论】:

  • 如果你的应用没有被唤醒来接收意图,系统应该“唤醒”你的应用。
  • @KristopherMicinski 我该怎么做?
  • 它已经做到了。这就是它的工作原理。你不必做任何事情。
  • @KristopherMicinski 所以这不是我的问题?? :O

标签: android broadcastreceiver manifest broadcast


【解决方案1】:

如果您注册BroadcastReceiver 来接收广播,那么即使用户当时没有使用您的应用程序,它也会被调用(您的应用程序的所有活动都不在前台)。不过,您可能需要考虑一些陷阱:

  1. 当设备处于睡眠状态时,大多数意图都不会被广播。只有几个事件会唤醒您的设备(例如收到的短信、来电、来自 Google Cloud Messaging 服务器的推送通知等)。因此,如果希望您的应用程序在设备处于睡眠状态时执行一些工作,您应该考虑使用AlarmManager 设置定期调用您的应用程序的警报。

  2. 如果你想在后台做一些工作,你可能想使用WakeLock 来防止设备休眠(和CPU 关闭)。这是来自 CommonWare 的一个很好的示例,它展示了如何使用 WakeLock,并为您提供了一个很好的库来简化您的生活:https://github.com/commonsguy/cwac-wakeful。不过,您应该小心WakeLock,因为它会耗尽电池电量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多