Cristian Holdunu
Cristian Holdunu

Reputation: 1918

Android shutdown/power off broadcast receiver not starting

I want to create a traffic widget, and store the used traffic at each device shutdown. The problem is that I can't get the shutdown receiver to trigger.

I used the following code:

    IntentFilter actionShutdown = new IntentFilter("android.intent.action.ACTION_SHUTDOWN");
    IntentFilter quickPOFF = new IntentFilter("android.intent.action.QUICKBOOT_POWEROFF");
    TrafficDataUpdate trafficDataUpdate = new TrafficDataUpdate();
    getContext().registerReceiver(trafficDataUpdate, actionShutdown);
    getContext().registerReceiver(trafficDataUpdate, quickPOFF);

I also tried with the receiver declared in manifest, but no success:

        <receiver android:name=".receivers.TrafficDataUpdate">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_SHUTDOWN"/>
            <action android:name="android.intent.action.QUICKBOOT_POWEROFF"/>
        </intent-filter>
    </receiver>

Do you have suggestions?

Can someone recommend me another way of counting the data traffic, without the shutdown receiver?

Upvotes: 3

Views: 10495

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007296

Use one IntentFilter, not two, teaching it to listen for both actions. Your second registerReceiver() is implicitly unregistering the first receiver.

Also note that this will only work so long as your process is still in memory. Usually, your process is not in memory, and it is in the user's best interests for your process to not be in memory. Please replace this implementation with one where the receiver is registered in the manifest, as is shown in the following StackOverflow threads:

Upvotes: 4

Related Questions