Ultra-accurate Android phone pedometer development _android

Source: Internet
Author: User
Tags constant

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.