Android - BroadcastReceiver不起作用

时间:2012-09-21 20:19:15

标签: android broadcastreceiver forceclose

当我Force Close一个应用程序时,BroadcastReceiver是否一直在监听意图?

我有这个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_LOW)) {
            // st.batterylow(contxt);
        } 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_POWER_CONNECTED)) {
            Log.i("******", "Battery on");
        } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
            // st.power(0, contxt);
        } 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();

            }
        }

    }

    public void register(Context c) {
        IntentFilter i = new IntentFilter();
        i.addAction(SMS_RECEIVED);
        i.addAction(Intent.ACTION_BATTERY_LOW);
        i.addAction(Intent.ACTION_POWER_CONNECTED);
        i.addAction(Intent.ACTION_POWER_DISCONNECTED);
        i.addAction(Intent.ACTION_CALL_BUTTON);
        i.addAction(Intent.ACTION_CAMERA_BUTTON);
        i.addAction(Intent.ACTION_BATTERY_CHANGED);
        i.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        c.registerReceiver(this, i);
        IntentFilter apps = new IntentFilter();
        apps.addAction(Intent.ACTION_PACKAGE_ADDED);
        apps.addAction(Intent.ACTION_PACKAGE_CHANGED);
        apps.addAction(Intent.ACTION_PACKAGE_REMOVED);
        apps.addDataScheme("package");
        c.registerReceiver(this, apps);
    }

    public void unregister(Context c) {
        c.unregisterReceiver(this);
    }
}

我呼叫注册,然后是Force Close。我想让BroadcastRecever保持工作并倾听传入的意图。

感谢。

2 个答案:

答案 0 :(得分:2)

强制关闭您的应用后,广播接收器将无法工作。这是通过设计内置到操作系统中的。

答案 1 :(得分:0)

manifest注册您的接收器。然后它将被永久激活(如果您喜欢使用包管理器,可以手动启用/禁用它。)