当前位置: 首页>>代码示例>>Java>>正文


Java BatteryManager类代码示例

本文整理汇总了Java中android.os.BatteryManager的典型用法代码示例。如果您正苦于以下问题:Java BatteryManager类的具体用法?Java BatteryManager怎么用?Java BatteryManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


BatteryManager类属于android.os包,在下文中一共展示了BatteryManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: onReceive

import android.os.BatteryManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    // 接收电量变化信息
    if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
        int level = intent.getIntExtra("level", 0);
        int scale = intent.getIntExtra("scale", 100);
        // 电量百分比
        int curPower = level * 100 / scale;
        int status = intent.getIntExtra("status", BatteryManager.BATTERY_HEALTH_UNKNOWN);
        // SecondaryProgress 用来展示低电量,Progress 用来展示正常电量
        if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
            mPbBatteryLevel.setSecondaryProgress(0);
            mPbBatteryLevel.setProgress(curPower);
            mPbBatteryLevel.setBackgroundResource(R.mipmap.ic_battery_charging);
        } else if (curPower < BATTERY_LOW_LEVEL) {
            mPbBatteryLevel.setProgress(0);
            mPbBatteryLevel.setSecondaryProgress(curPower);
            mPbBatteryLevel.setBackgroundResource(R.mipmap.ic_battery_red);
        } else {
            mPbBatteryLevel.setSecondaryProgress(0);
            mPbBatteryLevel.setProgress(curPower);
            mPbBatteryLevel.setBackgroundResource(R.mipmap.ic_battery);
        }
    }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:26,代码来源:IjkPlayerView.java

示例2: setCharging

import android.os.BatteryManager; //导入依赖的package包/类
@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);
}
 
开发者ID:Doist,项目名称:JobSchedulerCompat,代码行数:9,代码来源:DeviceTestUtils.java

示例3: onReceive

import android.os.BatteryManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
	if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
		// 0 indicates that we're on battery
		boolean onBatteryNow = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) <= 0;
		if (onBatteryNow) {
			InactivityTimer.this.onActivity();
		} else {
			InactivityTimer.this.cancel();
		}
	}
}
 
开发者ID:wp521,项目名称:MyFire,代码行数:13,代码来源:InactivityTimer.java

示例4: onReceive

import android.os.BatteryManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
        // 0 indicates that we're on battery
        boolean onBatteryNow = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) <= 0;
        if (onBatteryNow) {
            InactivityTimer.this.onActivity();
        } else {
            InactivityTimer.this.cancel();
        }
    }
}
 
开发者ID:alibaba,项目名称:LuaViewPlayground,代码行数:13,代码来源:InactivityTimer.java

示例5: batteryLevelUpdater

import android.os.BatteryManager; //导入依赖的package包/类
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);
}
 
开发者ID:sdrausty,项目名称:buildAPKsApps,代码行数:18,代码来源:Main.java

示例6: getBatteryLevel

import android.os.BatteryManager; //导入依赖的package包/类
private float getBatteryLevel() {
    Intent batteryStatus = registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int batteryLevel = -1;
    int batteryScale = 1;
    if (batteryStatus != null) {
        batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, batteryLevel);
        batteryScale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, batteryScale);
    }
    return batteryLevel / (float) batteryScale * 100;
}
 
开发者ID:ayushghd,项目名称:iSPY,代码行数:12,代码来源:TrackerService.java

示例7: onReceive

import android.os.BatteryManager; //导入依赖的package包/类
@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;
            }
        }
    }
}
 
开发者ID:SKT-ThingPlug,项目名称:thingplug-sdk-android,代码行数:17,代码来源:SensorListener.java

示例8: handleBattery

import android.os.BatteryManager; //导入依赖的package包/类
private void handleBattery(Intent intent){
    int level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
    int scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
    int levelPercent = (int)(((float)level / scale) * 100);
    boolean charging = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
            BatteryManager.BATTERY_STATUS_UNKNOWN) == BatteryManager.BATTERY_STATUS_CHARGING;
    if(levelPercent < BATTERY_LOW_LIMIT && !charging)
        AppModel.sDevBatteryLow = true;
    else
        AppModel.sDevBatteryLow = false;
    LogUtils.i("battery:" + level +"%, BatteryLow=" + AppModel.sDevBatteryLow);
}
 
开发者ID:XndroidDev,项目名称:Xndroid,代码行数:13,代码来源:XndroidReceiver.java

示例9: getBatteryLevel

import android.os.BatteryManager; //导入依赖的package包/类
/**
 * Returns the current device battery level.
 */
static String getBatteryLevel(Context context) {
    try {
        Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        if(batteryIntent != null) {
            int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

            // Error checking that probably isn't needed but I added just in case.
            if (level > -1 && scale > 0) {
                return Float.toString(((float) level / (float) scale) * 100.0f);
            }
        }
    }
    catch(Exception e){
        if (Countly.sharedInstance().isLoggingEnabled()) {
            Log.i(Countly.TAG, "Can't get batter level");
        }
    }

    return null;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:25,代码来源:CrashDetails.java

示例10: recordBatteryLevel

import android.os.BatteryManager; //导入依赖的package包/类
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));
}
 
开发者ID:rofferom,项目名称:gpstracker-android,代码行数:13,代码来源:BatteryRecorder.java

示例11: onReceive

import android.os.BatteryManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
        int remain = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        String remainString = Integer.toString(remain) + "%";
        currentStatus.setText(remainString);

        int plugIn = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
        switch (plugIn) {
            case 0:
                adapterStatus.setText("No Connection");
                break;

            case BatteryManager.BATTERY_PLUGGED_AC:
                adapterStatus.setText("Adapter Connected");
                break;

            case BatteryManager.BATTERY_PLUGGED_USB:
                adapterStatus.setText("USB Connected");
                break;
        }
    }
}
 
开发者ID:Jamjomjara,项目名称:snu-artoon,代码行数:26,代码来源:MainActivity.java

示例12: registerBatInforReceiver

import android.os.BatteryManager; //导入依赖的package包/类
private void registerBatInforReceiver() {
    mBatInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent intent) {
            if ((System.currentTimeMillis() - lastUpdateSensorTemperature) < Utils.SAMPLE_MILLIS) {
                return;
            }
            int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10;
            temperature_value = "" + temperature;
            bt_temperature_sample.setEnabled(true);
            bt_temperature_sample.setText(temperature_value + getResources().getString(R.string.battery_temp));
            lastUpdateSensorTemperature = System.currentTimeMillis();
        }
    };
    registerReceiver(mBatInfoReceiver,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
 
开发者ID:feup-infolab,项目名称:labtablet,代码行数:18,代码来源:FieldModeActivity.java

示例13: filterCommand

import android.os.BatteryManager; //导入依赖的package包/类
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean filterCommand(InterruptionCommand command) {
    if (command.shouldNotVibrateOnCharger()) {
        Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        boolean pluggedIn = batteryIntent != null && batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
        if (pluggedIn) {
            Timber.d("Filter - Charger!");
            return false;
        }
    }

    if (command.shouldNotVibrateInTheater() &&
            WatchUtils.isWatchInTheaterMode(this)) {
        Timber.d("Filter - Theater mode!");
        return false;
    }

    return true;
}
 
开发者ID:matejdro,项目名称:WearVibrationCenter,代码行数:20,代码来源:PhoneCommandListener.java

示例14: onReceive

import android.os.BatteryManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    if (level != -1) {
        _batteryTextView.setText(String.format(Locale.getDefault(), "%d %%", level));
        _batteryAlarmView.setVisibility(View.VISIBLE);
        if (level > BatterySocketController.UPPER_BATTERY_LIMIT) {
            _batteryAlarmView.setBackgroundResource(R.drawable.circle_green);
        } else if (level < BatterySocketController.LOWER_BATTERY_LIMIT) {
            _batteryAlarmView.setBackgroundResource(R.drawable.circle_red);
        } else {
            _batteryAlarmView.setBackgroundResource(R.drawable.circle_green);
        }
    } else {
        _batteryTextView.setText("Error!");
        _batteryAlarmView.setVisibility(View.GONE);
    }
}
 
开发者ID:GuepardoApps,项目名称:LucaHome-AndroidApplication,代码行数:19,代码来源:BottomInfoViewController.java

示例15: getBatteryPercent

import android.os.BatteryManager; //导入依赖的package包/类
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;
}
 
开发者ID:TeamThresh,项目名称:Lantern-sdk,代码行数:10,代码来源:RylaInstrumentation.java


注:本文中的android.os.BatteryManager类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。