在我的 android wear 项目中...我正在尝试获取手机的电池状态并将其发送到 Wear 设备...所以我尝试运行一个将在后台运行的服务并在操作 ACTION_BATTERY_CHANGED 上注册一个 BroadcastReceiver ..onReceive 它会将味精发送到佩戴设备..但我遇到的问题是我的电池状态更改广播接收器无法正常工作,它只能通过第一次登录,然后它没有在电池状态更改时运行,请帮助我解决这个问题....以下是我的服务代码....
BatteryStatService.java
package com.rahul.androidwear;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;
import java.util.Date;
public class BatteryStatService extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
GoogleApiClient googleClient;
public BatteryStatService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
// Build a new GoogleApiClient
googleClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
//Register Reciver
final IntentFilter battChangeFilter = new IntentFilter(
Intent.ACTION_BATTERY_CHANGED);
// register our receiver
this.registerReceiver(this.batteryChangeReceiver, battChangeFilter);
Log.e("service", "broadcast receiver registered");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
googleClient.connect();
}
@Override
public void onDestroy() {
super.onDestroy();
if (null != googleClient && googleClient.isConnected()) {
googleClient.disconnect();
}
Log.e("service", "okwear disconnected");
}
@Override
public void onConnected(Bundle bundle) {
String WEARABLE_DATA_PATH = "/wearable_data";
// Create a DataMap object and send it to the data layer
DataMap dataMap = new DataMap();
dataMap.putLong("time", new Date().getTime());
dataMap.putString("hole", "1");
dataMap.putString("front", "250");
dataMap.putString("middle", "260");
dataMap.putString("back", "270");
//Requires a new thread to avoid blocking the UI
new SendToDataLayerThread(WEARABLE_DATA_PATH, dataMap).start();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e("Mobile","Failed to connect Error::"+connectionResult.getErrorMessage());
}
private final BroadcastReceiver batteryChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
checkBatteryLevel(intent);
Log.e("service","broadcast receiver called");
}
};
private void checkBatteryLevel(Intent batteryChangeIntent) {
// some calculations
final int currLevel = batteryChangeIntent.getIntExtra(
BatteryManager.EXTRA_LEVEL, -1);
final int maxLevel = batteryChangeIntent.getIntExtra(
BatteryManager.EXTRA_SCALE, -1);
final int percentage = (int) Math.round((currLevel * 100.0) / maxLevel);
Log.d("MySerive", "current battery level: " + percentage);
unregisterReceiver(batteryChangeReceiver);
//return percentage;
}
class SendToDataLayerThread extends Thread {
String path;
DataMap dataMap;
// Constructor for sending data objects to the data layer
SendToDataLayerThread(String p, DataMap data) {
path = p;
dataMap = data;
}
public void run() {
// Construct a DataRequest and send over the data layer
PutDataMapRequest putDMR = PutDataMapRequest.create(path);
putDMR.getDataMap().putAll(dataMap);
PutDataRequest request = putDMR.asPutDataRequest();
DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleClient, request).await();
if (result.getStatus().isSuccess()) {
Log.v("myTag", "DataMap: " + dataMap + " sent successfully to data layer ");
} else {
// Log an error
Log.v("myTag", "ERROR: failed to send DataMap to data layer");
}
}
}
}
提前致谢...