本文整理汇总了Java中android.content.Intent.ACTION_BATTERY_CHANGED属性的典型用法代码示例。如果您正苦于以下问题:Java Intent.ACTION_BATTERY_CHANGED属性的具体用法?Java Intent.ACTION_BATTERY_CHANGED怎么用?Java Intent.ACTION_BATTERY_CHANGED使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.content.Intent
的用法示例。
在下文中一共展示了Intent.ACTION_BATTERY_CHANGED属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onReceive
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
if(action == Intent.ACTION_BATTERY_CHANGED){
handleBattery(intent);
}else if(action == ConnectivityManager.CONNECTIVITY_ACTION){
AppModel.getNetworkState();
}else if(action == Intent.ACTION_SCREEN_ON){
AppModel.sDevScreenOff = false;
LogUtils.i("screen on");
}else if(action == Intent.ACTION_SCREEN_OFF){
AppModel.sDevScreenOff = true;
LogUtils.i("screen off");
}
}catch (Exception e){
LogUtils.e("onReceive error ", e);
}
}
示例2: setCharging
@SuppressWarnings("deprecation")
public static void setCharging(Context context, boolean charging) {
Intent chargingIntent = new Intent(Intent.ACTION_BATTERY_CHANGED);
if (charging) {
chargingIntent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_AC);
}
context.sendStickyBroadcast(chargingIntent);
}
示例3: create
/**
* Create a PowerMonitor instance if none exists.
* @param context The context to register broadcast receivers for. The application context
* will be used from this parameter.
*/
public static void create(Context context) {
context = context.getApplicationContext();
if (sInstance == null) {
sInstance = LazyHolder.INSTANCE;
ApplicationStatus.registerApplicationStateListener(sInstance);
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatusIntent = context.registerReceiver(null, ifilter);
if (batteryStatusIntent != null) onBatteryChargingChanged(batteryStatusIntent);
}
}
示例4: onResume
protected void onResume() {
super.onResume();
updateMemoryStatus();
// register common receiver
IntentFilter filter = mCommonIntentFilter;
if (filter == null) {
filter = mCommonIntentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(LedFlashlightReceiver.ACTION_FLASHLIGHT);
mCommonIntentReceiver = new CommonIntentReceiver();
}
registerReceiver(mCommonIntentReceiver, filter);
// create/initialize/activate handlers
createInitializeActivateHandlers();
mLayout.updateLayout(this);
updateFlashlightView();
if (mRestartRequired) {
finish();
Intent intent = new Intent(Constants.ACTION_START_QS);
sendBroadcast(intent);
}
}
示例5: batteryLevelUpdater
private void batteryLevelUpdater()
{
batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
mBatteryLevelView.setLevel(level);
}
};
batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
示例6: battery
/**
* 获取手机剩余电量
*
* @return
*/
private static String battery() {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent intent = NimUIKit.getContext().registerReceiver(null, filter);
int level = intent.getIntExtra("level", -1);
int scale = intent.getIntExtra("scale", -1);
if(scale == -1) {
return "--";
} else {
return String.format(Locale.US, "%d %%", (level*100) / scale);
}
}
示例7: onReceive
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == Intent.ACTION_BATTERY_CHANGED) {
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
float temperature = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)) / 10;
float chargeLevel = ((float) (level * 100)) / scale;
for (SensorInfo sensorInfo : sensorInfos) {
if (sensorInfo.getType() == SensorType.BATTERY) {
sensorInfo.setValues(new float[]{temperature, chargeLevel});
break;
}
}
}
}
示例8: recordBatteryLevel
private void recordBatteryLevel() {
Log.i(TAG, "Record battery level");
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = mContext.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = (level / (float) scale) * 100;
mTelemetry.write(Telemetry.BATTERY_TAG, String.valueOf(batteryPct));
}
示例9: onStart
@Override
protected void onStart() {
super.onStart();
IntentFilter f=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(onBattery, f);
}
示例10: getBatteryPercent
private float getBatteryPercent() {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = RYLA.getInstance().getContext().registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
return level / (float)scale * 100;
}
示例11: registerBatteryUpdates
private Intent registerBatteryUpdates(Context context) {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
return context.registerReceiver(null, filter);
}
示例12: getDeviceConditionsImpl
/** Returns the current device conditions. May be overridden for testing. */
protected DeviceConditions getDeviceConditionsImpl(Context context) {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
// Note this is a sticky intent, so we aren't really registering a receiver, just getting
// the sticky intent. That means that we don't need to unregister the filter later.
Intent batteryStatus = context.registerReceiver(null, filter);
if (batteryStatus == null) return null;
// Get the connection type from chromium's internal object.
int connectionType = NetworkChangeNotifier.getInstance().getCurrentConnectionType();
// Sometimes the NetworkConnectionNotifier lags the actual connection type, especially when
// the GCM NM wakes us from doze state. If we are really connected, report the connection
// type from android.
if (connectionType == ConnectionType.CONNECTION_NONE) {
// Get the connection type from android in case chromium's type is not yet set.
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected) {
connectionType = convertAndroidNetworkTypeToConnectionType(activeNetwork.getType());
}
}
return new DeviceConditions(
isPowerConnected(batteryStatus), batteryPercentage(batteryStatus), connectionType);
}
示例13: start
@Override
public void start() {
if(!isRegistered) {
isRegistered = true;
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
application.registerReceiver(batteryReceiver, filter);
if(informationPlugin != null) {
CmdArg cmd = new CmdArg(0, "Registered Battery.");
informationPlugin.sendReplyCommand(PluginService.INFORMATION, cmd);
}
}
}
示例14: onReceive
@Override
public void onReceive(Context context, Intent intent) {
String message = null;
switch (intent.getAction()) {
case Intent.ACTION_BATTERY_LOW:
if (!isSentBatteryLow) {
message = botService.getString(R.string.battery_low);
isSentBatteryLow = true;
}
break;
case Intent.ACTION_BATTERY_OKAY:
isSentBatteryLow = false;
break;
case Intent.ACTION_POWER_DISCONNECTED:
message = botService.getString(R.string.power_disconnected) + getStatus();
break;
case Intent.ACTION_POWER_CONNECTED:
message = botService.getString(R.string.power_connected);
break;
case Intent.ACTION_BATTERY_CHANGED:
mBatteryLevel = getBatteryLevel(intent);
mPowerStatus = getBatteryStatus(intent);
ReceiverStorage.getInstance().setBatteryLevel(mBatteryLevel);
int segment = getSegment(mBatteryLevel);
if (segment < lastSegmentNotify) {
String attention = mIsCharging ? botService.getString(R.string.battery_discharging) : "";
message = attention + getStatus();
}
lastSegmentNotify = segment;
break;
}
if (message != null) {
botService.getTelegramService().sendMessageToAll(message);
}
}
示例15: initBatteryReceiver
private void initBatteryReceiver(Context context) {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
batteryReceiver = new BatteryReceiver();
if(context!=null){
context.registerReceiver(batteryReceiver,filter);
}
}