The advances is available on Huawei, the app uses back-office service meter, so as long as the app is not killed by the phone in the background can also be normal advances. Image above:
1. Need to add permissions in Androidmanifest.xml
<!--advances Required permissions-->
<uses-permission android:name= "Android.permission.VIBRATE"/>
< Uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>
<uses-feature android:name= " Android.hardware.sensor.accelerometer "/>
<uses-permission android:name=" android.permission.RECEIVE_ Boot_completed "/>
<uses-permission android:name=" Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "/>"
<uses-feature
android:name= "Android.hardware.sensor.stepcounter"
android:required= "true"/>
<uses-feature
android:name= "Android.hardware.sensor.stepdetector"
android:required= "true"/ >
2. Test whether the handset supports the advances
/**
* To determine whether the device supports advances
* *
@param context
* @return
/@TargetApi (build.version_codes. KitKat) Public
Static Boolean issupportstepcountsensor (context context) {
//Get instance of sensor manager
Sensormanager Sensormanager = (sensormanager) context
. Getsystemservice. Sensor_service);
Sensor countsensor = Sensormanager.getdefaultsensor (sensor.type_step_counter);
Sensor detectorsensor = Sensormanager.getdefaultsensor (sensor.type_step_detector);
return countsensor!= Null | | Detectorsensor!= null;
}
3. Function use
Private Boolean isbind = false;
Private Messenger Mgetreplymessenger = new Messenger (new Handler (this));
Private Messenger Messenger;
/** * Open Pedometer service * * private void Setupservice () {Intent Intent = new Intent (this, stepservice.class);
Isbind = Bindservice (Intent, Conn, context.bind_auto_create);
StartService (Intent); /** * Get steps from service services * * @param msg * @return/@Override
public boolean handlemessage (Message msg) { switch (
Msg.what) { case constant.msg_from_server: Cc.setcurrentcount (
10000, Msg.getdata (). GETINT ("step"));
break;
} return false; } /** * A interface for querying the status of application services (application service) nbsp; * More detailed information can be referenced in the service and Context.bindservice () description * And many callback methods from the system, The Serviceconnection method is called / Serviceconnection conn = new in the main thread of the process. Serviceconnection () { /** * This method is invoked when establishing a connection to a service, and Android is currently connected to the services through the ibind mechanism. * @param name is actually connected to the service component names * The ibind of the communication channel of the service @param services, which can be accessed through service * @Override Public void onserviceconnected (componentname name, IBinder service) { try {
Messenger = new Messenger (service); msg =
Message.obtain (null, constant.msg_from_client); Msg.replyto =
Mgetreplymessenger; messenger.send (msg); catch (RemoteException e) {
E.printstacktrace ();
} } /** *
Call the party when the connection to the service is lost * This often happens when the service's process crashes or is killed. * This method will not remove the connection to the service and will still invoke onserviceconnected () when it restarts * @param name missing component name for connection */ @Override public void onservicedisconnected (componentname name) { } };
GitHub Address: Https://github.com/linglongxin24/DylanStepCount
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.