3

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?

Cristian Holdunu
  • 1,880
  • 1
  • 18
  • 43

1 Answers1

4

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:

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I tried with the manifest also. I updated my question. I don't think there is a problem of keeping the process in memory. Thank you. – Cristian Holdunu Dec 14 '13 at 15:30
  • @CommonsWare can you please look into this question? its a similar one but for Huawei devices. https://stackoverflow.com/questions/58304798/action-shutdown-quickboot-poweroff-broadcast-not-working-for-huawei-devices – Bilal Oct 10 '19 at 06:50