Java tutorial
//package com.java2s; /* * Copyright (C) 2014 AChep@xda <artemchep@gmail.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.support.annotation.NonNull; import android.support.annotation.Nullable; public class Main { /** * @return true is device is plugged at this moment, false otherwise. * @see #isPlugged(android.content.Intent) */ public static boolean isPlugged(@NonNull Context context) { IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); return isPlugged(context.getApplicationContext().registerReceiver(null, intentFilter)); } /** * @return true is device is plugged at this moment, false otherwise. * @see #isPlugged(android.content.Context) */ @SuppressLint("InlinedApi") public static boolean isPlugged(@Nullable Intent intent) { if (intent == null) { return false; } final int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS; } }