1
當我Force Close
一個應用程序,並在BroadcastReceiver
保持偵聽意圖?Android的 - 廣播接收器不工作
我有這個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
工作和收聽傳入的意圖。
謝謝。
我可以實現服務? –
這不是事實。如果您使用registerReceiver()註冊接收方,則其生存時間與註冊它的對象的生存時間相關。因此,如果你殺了應用程序,接收器將不再工作。服務不會工作。您應該在您的應用清單中註冊您的接收器,使其按照您的描述工作。 –
托比亞斯,這是不作爲API版本12.真請參閱http://developer.android.com/about/versions/android-3.1.html,部分「關於停止應用程序啓動控制」。如果應用程序是力停止,即使在清單中聲明 BroadcastReceivers將無法正常工作。 – WindyB