# バッテリー情報の取得 プラグイン

テスト環境 ( バージョン番号 ) : [2.0.2](https://github.com/apache/cordova-plugin-battery-status/releases/tag/2.0.2)

{% hint style="info" %}
このプラグインの詳細は、[ こちらの原文 ( GitHub ) ](https://github.com/apache/cordova-plugin-battery-status)をご確認ください。
{% endhint %}

このプラグインでは、旧バージョンの [Battery Status Events API](http://www.w3.org/TR/2011/WD-battery-status-20110915/) が使用されています。 `window` オブジェクトに、以下の3つのイベントを追加します。

* batterystatus
* batterycritical
* batterylow

アプリは、`deviceready` イベント発生後、`window.addEventListener` を使用して、上記のイベントのイベントリスナーをアタッチすることができます。

## プラグイン ID

```javascript
cordova-plugin-battery-status
```

## プラグインの追加方法 ( Monaca 上での処理 )

このプラグインを使用する場合には、Monaca クラウド IDE の \[ Cordova プラグインの管理 ] 上で、`Battery` プラグインを[有効](https://ja.docs.monaca.io/reference/core-cordova-plugins/cordova_9.0/broken-reference)にします。

## API の解説

### ステータス オブジェクト

このプラグインのすべてのイベントは、次のプロパティを持つオブジェクトを返します。

| プロパティ       | 型   | 解説                 |
| ----------- | --- | ------------------ |
| `level`     | 数値  | バッテリー充電率 (`0-100`) |
| `isPlugged` | 真偽値 | 端末が充電中かを示す真偽値      |

### batterystatus イベント

バッテリの充電率が少なくとも1パーセント変化したとき、または端末の充電を開始、または停止されたときに発火します。 バッテリーの状態を含む [object](#suttasu-obujekuto) を返します。

#### 例

```javascript
window.addEventListener("batterystatus", onBatteryStatus, false);

function onBatteryStatus(status) {
    console.log("Level: " + status.level + " isPlugged: " + status.isPlugged);
}
```

#### サポート対象のプラットフォーム

* iOS
* Android

#### Android 特有の動作

{% hint style="danger" %}
Android の組み合わせは、バッテリーを多く消費するため、長時間の使用には注意が必要です。&#x20;
{% endhint %}

### batterylow イベント

バッテリー残量が非常に少なくなった場合に、このイベントが発火します。しきい値は、端末によって異なります。バッテリーの状態を示すプロパティが格納された [object](#suttasu-obujekuto) を返します。

#### 例

```javascript
window.addEventListener("batterylow", onBatteryLow, false);

function onBatteryLow(status) {
    alert("Battery Level Low " + status.level + "%");
}
```

#### サポート対象のプラットフォーム

* iOS
* Android

### batterycritical イベント

バッテリー充電率が臨界充電しきい値に達した場合に、このイベントが発火します。しきい値は、端末によって異なります。バッテリーの状態を示すプロパティが格納された [object](#suttasu-obujekuto) を返します。

#### 例

```javascript
window.addEventListener("batterycritical", onBatteryCritical, false);

function onBatteryCritical(status) {
    alert("Battery Level Critical " + status.level + "%\nRecharge Soon!");
}
```

#### サポート対象のプラットフォーム

* iOS
* Android

関連項目:

* [サードパーティー製プラグイン](https://ja.docs.monaca.io/reference/third_party_phonegap)
* [基本プラグイン](https://ja.docs.monaca.io/reference/core-cordova-plugins)
