Search code examples
androidbroadcastreceivershutdown

Receive notification of Android's thermal shutdown in my application


I want to receive a notification in my app before Android is going to shutdown due to thermal environment.

Here is a logcat of the shutdown itself:

[ 07-26 11:59:16.191   521: 1067 I/system_monitor ]

alarm_callback - Received MAX alarm: batt_therm at Level:1

[ 07-26 11:59:16.195   968: 6013 I/libsysmon ]

handle_notification - Called

[ 07-26 11:59:16.195   968: 6013 I/libsysmon ]

handle_notification - Dumping notification

[ 07-26 11:59:16.195   968: 6013 I/libsysmon ]

name: batt_therm

[ 07-26 11:59:16.195   968: 6013 I/libsysmon ]

current_level: 0

[ 07-26 11:59:16.195   968: 6013 I/libsysmon ]

prev_level: 1

[ 07-26 11:59:16.195   968: 6013 I/libsysmon ]

shutdown_in: 20

[ 07-26 11:59:16.195   968: 6013 I/libsysmon ]

Adding action: SHUTDOWN

[ 07-26 11:59:16.195   968: 6013 I/libsysmon ]

handle_notification - Going to callback function

[ 07-26 11:59:16.196   968: 6013 I/libsysmon ]

[ 07-26 11:59:16.196   968: 6013 I/libsysmon ]

[ 07-26 11:59:16.276   521: 1067 E/system_monitor ]

write_miscta_sysmon_log - read error[10022].

[ 07-26 11:59:16.284   186:  186 I/tad      ]

MiscTA Unit 10022: 

[ 07-26 11:59:16.284   186:  186 I/tad      ]

NOT WRITTEN (Identical)

[ 07-26 11:59:16.284   186:  186 I/tad      ]

MiscTA: Write of unit 10022 completed - OK

[ 07-26 11:59:16.311   186:  186 I/tad      ]

MiscTA Unit 10025: 

[ 07-26 11:59:16.312   186:  186 I/tad      ]

WRITTEN (Size 50)

[ 07-26 11:59:16.409   186:  186 I/tad      ]

Actual erase/write to flash done (count 1, time 1564135156).

[ 07-26 11:59:16.409   186:  186 I/tad      ]

MiscTA: Write of unit 10025 completed - OK

[ 07-26 11:59:16.431   186:  186 I/tad      ]

MiscTA Unit 10024: 

[ 07-26 11:59:16.431   186:  186 I/tad      ]

WRITTEN (Size 50)

[ 07-26 11:59:16.494   186:  186 I/tad      ]

Actual erase/write to flash done (count 2, time 1564135156).

[ 07-26 11:59:16.494   186:  186 I/tad      ]

MiscTA: Write of unit 10024 completed - OK

[ 07-26 11:59:16.501   186:  186 I/tad      ]

MiscTA Unit 10023: 

[ 07-26 11:59:16.501   186:  186 I/tad      ]

WRITTEN (Size 50)

[ 07-26 11:59:16.574   186:  186 I/tad      ]

Actual erase/write to flash done (count 3, time 1564135156).

[ 07-26 11:59:16.574   186:  186 I/tad      ]

MiscTA: Write of unit 10023 completed - OK

[ 07-26 11:59:16.576   521: 1067 I/system_monitor ]

Shutting down

[ 07-26 11:59:16.996  1930: 1930 I/GPSManager ]

location.getAccuracy() 4.0

[ 07-26 11:59:16.996  1930: 1930 I/AdressUpdate ]

AdressUpdate

[ 07-26 11:59:18.009  1930: 1930 I/GPSManager ]

location.getAccuracy() 4.0

[ 07-26 11:59:18.009  1930: 1930 I/AdressUpdate ]

AdressUpdate

[ 07-26 11:59:18.221  2217: 2231 I/art      ]

Background sticky concurrent mark sweep GC freed 64885(3MB) AllocSpace objects, 0(0B) LOS objects, 10% free, 27MB/30MB, paused 9.626ms total 105.405ms

[ 07-26 11:59:18.336   968: 1034 W/PackageManager ]

Invalid verification token 0 received

[ 07-26 11:59:18.993  1930: 1930 I/GPSManager ]

location.getAccuracy() 4.0

[ 07-26 11:59:18.993  1930: 1930 I/AdressUpdate ]

AdressUpdate

[ 07-26 11:59:19.984  1930: 1930 I/GPSManager ]

location.getAccuracy() 4.0

[ 07-26 11:59:19.984  1930: 1930 I/AdressUpdate ]

AdressUpdate

[ 07-26 11:59:20.983  1930: 1930 I/GPSManager ]

location.getAccuracy() 4.0

[ 07-26 11:59:20.983  1930: 1930 I/AdressUpdate ]

AdressUpdate

[ 07-26 11:59:21.666   968: 1572 W/InputMethodManagerService ]

Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@1cafeb50 attribute=null, token = android.os.BinderProxy@224ee282

[ 07-26 11:59:21.727   968: 1028 I/MediaFocusControl ]

 AudioFocus  requestAudioFocus() from android.media.AudioManager@2079c149 req=1flags=0x0

[ 07-26 11:59:22.064   968: 5233 W/ShutdownThread ]

Disabling Bluetooth...

[ 07-26 11:59:22.069   968: 5232 I/ShutdownThread ]

Sending shutdown broadcast...

[ 07-26 11:59:22.078   968: 5232 I/ShutdownThread ]

Shutting down activity manager...

[ 07-26 11:59:22.084   968:  968 W/SyncManager ]

Writing sync state before shutdown...

[ 07-26 11:59:22.097  1930: 1930 I/GPSManager ]

location.getAccuracy() 5.0

[ 07-26 11:59:22.097  1930: 1930 I/AdressUpdate ]

AdressUpdate

[ 07-26 11:59:22.240  4674: 4750 I/BluetoothAdapterState ]

Bluetooth adapter state changed: 12-> 13

[ 07-26 11:59:22.274   968: 5233 W/ShutdownThread ]

Turning off cellular radios...

[

How can I receive a broadcast of the shutdown event? Is it possible at all? There is a line in the log which states the shutdown will be happen in 20 seconds. I would like to get a notification in my application when that event happens - so the app can do the necessary actions before actually shutting down.

Thank you.


Solution

  • Check the following in your manifest file:

    Add permission:

    <uses-permission android:name="android.permission.DEVICE_POWER" />
    

    Add Receiver:

    <receiver android:name=".ShutdownReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_SHUTDOWN" />
        </intent-filter>
    </receiver>
    

    Make a BroadcastReceiver:

    private class ShutDownReceiver extends BroadcastReceiver
    {
    
        @Override
        public void onReceive(Context context, Intent intent)
        {
            if(Intent.ACTION_SHUTDOWN.equals(intent.getAction()))
            {
                Log.i(TAG, "System shutting down");
                context.stopService(new Intent(context, BluetoothPanService.class));
            }
        }
    
    }
    

    In the main activity's onResume() method instantiate and register the receiver:

    public void onResume()
    {
        super.onResume();
        Log.i(TAG, "Measurement Log Activity has Resumed.");
    
        IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
        mReceiver = new ShutDownReceiver();
        registerReceiver(mReceiver, filter);
    }
    

    And finally in the main activity's onPause() unregister the receiver:

    public void onPause()
    {
        super.onPause();
        Log.i(TAG, "Measurement Log Activity has Paused.");
        this.unregisterReceiver(mReceiver);
    }
    

    Also see this and this. I hope it helps.