即使我的应用未开启,我也想跟踪电池信息(是充电,电平等)。我想也许服务会好吗?或者也许有其他解决方案?我全都准备好了。
答案 0 :(得分:1)
需要在AndroidManifest.xml中声明服务,并且实现类必须扩展Service类或其子类之一。以下代码显示了服务声明及其实现的示例。
<service
android:name="MyService"
android:icon="@drawable/icon"
android:label="@string/service_name"
>
</service>
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}
}
答案 1 :(得分:1)
这实际上是一个非常古老的帖子,但由于接受的答案似乎是一个存根服务,我认为有必要更深入。
您需要在Android清单中声明该服务。如果你在某个包中有声明,那么声明就是:
<service android:name="packagename.servicename" />
然后您需要实现服务类。我为我的一个应用程序创建了一个。希望这会对你有所帮助。
我在传感器的背景下有这个。传感器是我想要监控的东西所以我创建了一个如下界面:
public interface Sensor {
SensorType getSensorType();
SensorName getSensorName();
}
我必须有ENUM
个类,它们代表我所拥有的传感器的类型和名称:
public enum SensorName {
ENVIRONMENTAL_SENSOR, AUDIO_SENSOR, SOUND_SENSOR, SOCIAL_SENSOR,
LOCATION_SENSOR, PHOTO_SENSOR, ACCELEROMETER_SENSOR, BATTERY_SENSOR;
}
public enum SensorType {
HARDWARE_SENSOR, SOFTWARE_SENSOR, HYBRID_SENSOR;
}
由于我有几个传感器,我可能希望列出我实现此接口所需的所有传感器。接下来,我创建了一个抽象类来实现与我的所有传感器类似的行为。
public abstract class SensorElement extends Service implements Sensor{
protected SensorType type;
protected SensorName name;
@Override
public abstract IBinder onBind(Intent arg0);
@Override
public int onStartCommand(Intent intent, int flags, int startId){
return super.onStartCommand(intent, flags, startId);
}
@Override
public abstract void onCreate();
@Override
public void onDestroy(){
super.onDestroy();
}
}
我的传感器需要这种方法才能正常工作。在我的情况下,我只需要绑定器将服务绑定到我的活动。
现在你想要的是电池传感器本身:
public class BatterySensor extends SensorElement {
// Binder given to clients
private final IBinder mBinder = new BatteryLocalBinder();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class BatteryLocalBinder extends Binder {
public BatterySensor getService() {
// Return this instance of MotionLightOrientationSensor so clients
// can call
// public
// methods
return BatterySensor.this;
}
}
private static final String TAG = "BatterySensor";
public static final SensorType type = SensorType.SOFTWARE_SENSOR;
public static final SensorName name = SensorName.BATTERY_SENSOR;
private int status;
private boolean isCharging;
private int chargePlug;
private boolean usbCharge;
private boolean acCharge;
private int level;
private int scale;
private double batteryPct;
private Battery battery;
@Override
public SensorType getSensorType() {
return type;
}
@Override
public SensorName getSensorName() {
return name;
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(powerconnectionreceiver, ifilter);
battery = new Battery();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
unregisterReceiver(powerconnectionreceiver);
}
private BroadcastReceiver powerconnectionreceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL;
chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
batteryPct = level / (float) scale;
battery.setStatus(status);
battery.setCharging(isCharging);
battery.setChargePlug(chargePlug);
battery.setUsbCharge(usbCharge);
battery.setAcCharge(acCharge);
battery.setLevel(level);
battery.setScale(scale);
battery.setBatteryPct(batteryPct);
}
};
public Battery getBatteryReading() {
return this.battery;
}
public int getStatus() {
return status;
}
public boolean isCharging() {
return isCharging;
}
public int getChargePlug() {
return chargePlug;
}
public boolean isUsbCharge() {
return usbCharge;
}
public boolean isAcCharge() {
return acCharge;
}
public int getLevel() {
return level;
}
public int getScale() {
return scale;
}
}
要从此传感器访问数据,您只需在以下活动中启动它:
private BatterySensor batterySensor;
private boolean mBatteryBound = false;
private Intent intentBattery;
intentBattery = new Intent(this.context, BatterySensor.class);
然后我开始服务:
context.startService(intentBattery);
并绑定它:
if(this.context.bindService(intentBattery, mBatteryConnection, Context.BIND_AUTO_CREATE)){
this.numBoundedSensors++;
}
要绑定它,您需要在您的活动中使用此类:
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mBatteryConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
BatteryLocalBinder BatteryBinder = (BatteryLocalBinder) service;
batterySensor = BatteryBinder.getService();
mBatteryBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBatteryBound = false;
}
};
然后,如果它成功绑定,你可以调用传感器中的所有方法,如下所示:
public Battery getBatteryData(){
return mBatteryBound ? batterySensor.getBatteryReading() : null;
}