Reputation: 493
I'm trying to handle an android device shut down : When the device is shutting down, a singleton, named PostManager
, sends a POST request.
To do so, i'm using a BroadcastReceiver
:
public class DeviceOffReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
PostManager.getInstance().onDeviceOff();
}
}
}
public class PostManager{
public void onDeviceOff() {
sendRequest();
}
}
Here is the BroadcastReceiver
initialization :
IntentFilter filter = new IntentFilter(".DeviceOffReceiver");
filter.addAction(Intent.ACTION_SHUTDOWN);
mShutDownReceiver= new DeviceOffReceiver ();
app.registerReceiver(mShutDownReceiver, filter);
This code works perfectly.. but only the first time. By "first time", I mean the first time the app is run. Has anyone ever experienced the same issue ?
EDIT : I register the intent ACTION_SCREEN_OFF
. I receive it every time. It has something to do with the ACTION_SHUTDOWN
intent.
Upvotes: 2
Views: 920
Reputation: 493
My device version is 4.3. I tried to run the app with another device (on 5.0.2) and it works every time...
So it has something to do with 4.3 (maybe ?).
Upvotes: 2
Reputation: 571
Make sure you put this code into Manifest. You can use this code to solve issue:
<receiver android:name=".DeviceOffReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
Upvotes: 1