當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。