44import android .content .BroadcastReceiver ;
55import android .content .Context ;
66import android .content .Intent ;
7+ import android .os .Handler ;
8+ import android .os .Looper ;
79import android .os .PowerManager ;
8- import android .util .Log ;
910import android .widget .Toast ;
11+ import android .util .Log ;
1012
1113public class WakeLockReceiver extends BroadcastReceiver {
1214 private static PowerManager .WakeLock wakeLock ;
@@ -20,13 +22,14 @@ public void onReceive(Context context, Intent intent) {
2022 wakeLock = pm .newWakeLock (PowerManager .PARTIAL_WAKE_LOCK , "WakeLockApp::ReceiverLock" );
2123 }
2224
25+ Handler handler = new Handler (Looper .getMainLooper ());
2326 if ("on" .equals (action ) && !wakeLock .isHeld ()) {
2427 wakeLock .acquire ();
25- Toast .makeText (context , "Wake Lock đã BẬT" , Toast .LENGTH_SHORT ).show ();
28+ handler . post (() -> Toast .makeText (context , "Wake Lock đã BẬT" , Toast .LENGTH_SHORT ).show () );
2629 Log .i ("WakeLockReceiver" , "WakeLock acquired via broadcast" );
2730 } else if ("off" .equals (action ) && wakeLock .isHeld ()) {
2831 wakeLock .release ();
29- Toast .makeText (context , "Wake Lock đã TẮT" , Toast .LENGTH_SHORT ).show ();
32+ handler . post (() -> Toast .makeText (context , "Wake Lock đã TẮT" , Toast .LENGTH_SHORT ).show () );
3033 Log .i ("WakeLockReceiver" , "WakeLock released via broadcast" );
3134 }
3235 }
0 commit comments