How to detect power connected state?

前端 未结 5 1407
庸人自扰
庸人自扰 2020-11-30 13:24

Is there an easy way to be notified when USB or AC power is connected to an Android phone?

5条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 14:12

    In AndroidManifest.xml

    
        
            
                
                
            
        
    
    

    In Code

    public class PlugInControlReceiver extends BroadcastReceiver {
       public void onReceive(Context context , Intent intent) {
           String action = intent.getAction();
    
           if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
               // Do something when power connected
           }
           else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
               // Do something when power disconnected
           }
       }
    }
    

提交回复
热议问题