{"id":12526,"date":"2022-11-24T14:02:51","date_gmt":"2022-11-24T06:02:51","guid":{"rendered":"https:\/\/www.yimenapp.com\/kb-yimen\/?p=12526"},"modified":"2022-11-24T14:02:52","modified_gmt":"2022-11-24T06:02:52","slug":"%e5%b0%8f%e7%b1%b3%e8%b7%91%e6%ad%a5%e6%9c%ba%e4%bc%a0%e6%84%9f%e5%99%a8%e6%95%b0%e6%8d%ae%e9%9b%86%e6%88%90%e5%88%b0%e8%ae%a1%e6%ad%a5%e5%99%a8%e6%95%b0%e6%8d%ae%e5%ba%93%e8%af%b4%e6%98%8e","status":"publish","type":"post","link":"https:\/\/www.yimenapp.com\/kb-yimen\/12526\/","title":{"rendered":"\u5c0f\u7c73\u8dd1\u6b65\u673a\u4f20\u611f\u5668\u6570\u636e\u96c6\u6210\u5230\u8ba1\u6b65\u5668\u6570\u636e\u5e93\u8bf4\u660e"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"_0\">1.<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e5%b0%8f%e7%b1%b3%e8%b7%91%e6%ad%a5%e6%9c%ba\/\" title=\"\u5c0f\u7c73\u8dd1\u6b65\u673a\" target=\"_blank\">\u5c0f\u7c73\u8dd1\u6b65\u673a<\/a><\/span><span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e4%bc%a0%e6%84%9f%e5%99%a8\/\" title=\"\u4f20\u611f\u5668\" target=\"_blank\">\u4f20\u611f\u5668<\/a><\/span>\u7b80\u4ecb<\/h2>\n\n\n\n<p>\u5f53\u624b\u673a\u653e\u7f6e\u5728<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e8%b7%91%e6%ad%a5%e6%9c%ba\/\" title=\"\u8dd1\u6b65\u673a\" target=\"_blank\">\u8dd1\u6b65\u673a<\/a><\/span>\u4e0a\u65f6\uff0c\u6536\u96c6\u624b\u673a\u4e2d\u4f20\u611f\u5668\u7684\u6570\u636e\uff0c\u5224\u65ad\u662f\u5426\u5728\u8dd1\u6b65\u673a\u4e0a\u8fd0\u52a8\uff0c\u82e5\u8fd0\u52a8\u4e00\u6b65\u5219\u8dd1\u6b65\u673a\u4f20\u611f\u5668\u4e0a\u62a5\u4e00\u6b21\u6570\u636e1,\u4e0d\u8fd0\u52a8\u5219\u4e0d\u62a5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"_1\">2.\u5c06\u8dd1\u6b65\u673a<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e6%95%b0%e6%8d%ae%e9%9b%86%e6%88%90\/\" title=\"\u6570\u636e\u96c6\u6210\" target=\"_blank\">\u6570\u636e\u96c6\u6210<\/a><\/span>\u5230\u8ba1\u6b65\u5668<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e6%95%b0%e6%8d%ae%e5%ba%93\/\" title=\"\u6570\u636e\u5e93\" target=\"_blank\">\u6570\u636e\u5e93<\/a><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"_1_0\">2.1.\u901a\u77e5\u7cfb\u7edf\u670d\u52a1<\/h3>\n\n\n\n<p>\u5728\u6ce8\u518c\u6216\u8005\u89e3\u9664\u6ce8\u518c\u8dd1\u6b65\u673a\u4f20\u611f\u5668\u7684\u65f6\u5019\u901a\u8fc7Binder\u544a\u77e5\u7cfb\u7edf\u670d\u52a1\u3002 \u8fd9\u6b65\u9aa4\u4e3a\u5fc5\u987b\u64cd\u4f5c\uff0c\u5426\u5219\u8dd1\u6b65\u673a\u8ba1\u6b65\u5668\u6570\u636e\u65e0\u6cd5\u540c\u6b65\u81f3\u7cfb\u7edf\u8ba1\u6b65\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>public<\/strong> <strong>class<\/strong> <strong>MainActivity<\/strong> <strong>extends<\/strong> <strong>AppCompatActivity<\/strong> {\n\n    <strong>private<\/strong> SensorManager mSensorManager;\n    <strong>private<\/strong> Sensor mTreadmillSensor;\n    <strong>private<\/strong> TreadmillListener mTreadmillListener;\n    <strong>private<\/strong> <strong>static<\/strong> <strong>final<\/strong> <strong>int<\/strong> TREADMILL_SENSOR = 33171041;\n    <strong>private<\/strong> <strong>static<\/strong> <strong>final<\/strong> String SERVICE_NAME = \"miui_step_counter_service\";\n    <strong>private<\/strong> Binder mBinder;\n\n    @RequiresApi(api = Build.VERSION_CODES.Q)\n    @Override\n    <strong>protected<\/strong> <strong>void<\/strong> <strong>onCreate<\/strong>(Bundle savedInstanceState) {\n        <strong>super<\/strong>.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);\n        mTreadmillSensor = mSensorManager.getDefaultSensor(TREADMILL_SENSOR,<strong>true<\/strong>);\n        mTreadmillListener = <strong>new<\/strong> TreadmillListener();\n        <em>\/\/\u76d1\u542c\u8dd1\u6b65\u673aSensor<\/em>\n        mSensorManager.registerListener(mTreadmillListener,mTreadmillSensor,mSensorManager.SENSOR_DELAY_NORMAL);\n        <em>\/\/\u901a\u77e5\u7cfb\u7edf\u670d\u52a1\uff0c\u6ce8\u518c\u7684\u65f6\u5019\u53d1\u9001true<\/em>\n        sendMessage(<strong>true<\/strong>);\n    }\n\n    @RequiresApi(api = Build.VERSION_CODES.Q)\n    <strong>public<\/strong> <strong>void<\/strong> <strong>sendMessage<\/strong>(<strong>boolean<\/strong> is){\n        <em>\/\/\u83b7\u53d6\u7cfb\u7edf\u670d\u52a1\uff0cServiceManager\u62a5\u9519\u5e95\u4e0b\u6709\u89e3\u51b3\u65b9\u6848<\/em>\n        IBinder binder = ServiceManager.getService(SERVICE_NAME);        \n        <strong>if<\/strong> (mBinder == <strong>null<\/strong>) {\n            mBinder = <strong>new<\/strong> Binder();\n        }\n        Parcel data = Parcel.obtain();\n        Parcel reply = Parcel.obtain();\n        data.writeInterfaceToken(\"miui_step_counter_service\");\n        data.writeBoolean(is);\n        <em>\/\/\u4f20\u8fc7\u53bb\u4e00\u4e2a\u5168\u5c40Binder\uff08\u4e3a\u4e86\u611f\u77e5\u672c\u7c7b\u662f\u5426\u88ab\u9500\u6bc1\uff09<\/em>\n        data.writeStrongBinder(mBinder);\n        <strong>try<\/strong> {\n            binder.transact(0,data,reply,0);\n        } <strong>catch<\/strong> (RemoteException e) {\n            e.printStackTrace();\n        }\n    }\n\n    <strong>private<\/strong> <strong>class<\/strong> <strong>TreadmillListener<\/strong> <strong>implements<\/strong> <strong>SensorEventListener<\/strong>{\n\n        @Override\n        <strong>public<\/strong> <strong>void<\/strong> <strong>onSensorChanged<\/strong>(SensorEvent sensorEvent) {\n\n        }\n\n        @Override\n        <strong>public<\/strong> <strong>void<\/strong> <strong>onAccuracyChanged<\/strong>(Sensor sensor, <strong>int<\/strong> i) {\n\n        }\n    }\n\n    @RequiresApi(api = Build.VERSION_CODES.Q)\n    @Override\n    <strong>protected<\/strong> <strong>void<\/strong> <strong>onDestroy<\/strong>() {\n        <strong>super<\/strong>.onDestroy();\n        <em>\/\/\u89e3\u6ce8\u518c\u7684\u65f6\u5019\uff0c\u901a\u77e5\u7cfb\u7edf\u670d\u52a1\uff0c\u53d1\u9001false<\/em>\n        mSensorManager.unregisterListener(mTreadmillListener,mTreadmillSensor);\n        sendMessage(<strong>false<\/strong>);\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"_1_1\">2.2.ServiceManager\u62ff\u4e0d\u5230\u89e3\u51b3\u65b9\u6848<\/h3>\n\n\n\n<p>\u5728\u4ee3\u7801\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305\u540d\u4e3aandroid.os\uff0c\u7c7b\u540d\u4e3aServiceManager\u7684\u7c7b\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> android.os;\n\n<strong>public<\/strong> <strong>class<\/strong> <strong>ServiceManager<\/strong> {\n    <strong>private<\/strong> <strong>ServiceManager<\/strong>(){}\n\n    <strong>public<\/strong> <strong>static<\/strong> IBinder <strong>getService<\/strong>(String name){\n        <strong>return<\/strong> <strong>null<\/strong>;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"_1_2\">2.3.\u8bf4\u660e<\/h3>\n\n\n\n<p>\u76ee\u524d\u8dd1\u6b65\u673a\u8ba1\u6b65\u4f20\u611f\u5668\u652f\u6301<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/xiaomi\/\" title=\"\u5c0f\u7c73\" target=\"_blank\">\u5c0f\u7c73<\/a><\/span>12\u3001\u5c0f\u7c7312 Pro\u3001\u5c0f\u7c7312S Pro\u3001\u5c0f\u7c7312S Ultra\u56db\u6b3e\u673a\u578b\uff0c\u5982\u679c\u60a8\u7684\u5e94\u7528\u5728\u6ce8\u518c\u4e0b\u9762\u8fd9\u4e2a\u7279\u5b9a\u4f20\u611f\u5668type\u65f6\u8fd4\u56de\u7684sensor\u5bf9\u8c61\u4e3a\u7a7a\uff0c\u8bf4\u660e\u5f53\u524d\u673a\u578b\u4e0d\u652f\u6301\u8dd1\u6b65\u673a\u8ba1\u6b65\u4f20\u611f\u5668\uff0c\u5e94\u7528\u53ef\u901a\u8fc7\u6b64sensor\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u6765\u5224\u5b9a\u8be5\u529f\u80fd\u662f\u5426\u751f\u6548\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);\n        mTreadmillSensor = mSensorManager.getDefaultSensor(TREADMILL_SENSOR,<strong>true<\/strong>);\n        <em>\/\/\u6839\u636emTreadmillListener\u662f\u5426\u4e3a\u7a7a\u6765\u5224\u5b9a\u5f53\u524d\u624b\u673a\u662f\u5426\u652f\u6301\u8dd1\u6b65\u673a\u8ba1\u6b65\u7684\u529f\u80fd<\/em>\n        mTreadmillListener = <strong>new<\/strong> TreadmillListener(); <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u624b\u673a\u653e\u7f6e\u5728\u8dd1\u6b65\u673a\u4e0a\u65f6\uff0c\u6536\u96c6\u624b\u673a\u4e2d\u4f20\u611f\u5668\u7684\u6570\u636e\uff0c\u5224\u65ad\u662f\u5426\u5728\u8dd1\u6b65\u673a\u4e0a\u8fd0\u52a8\uff0c\u82e5\u8fd0\u52a8\u4e00\u6b65\u5219\u8dd1\u6b65\u673a\u4f20\u611f\u5668\u4e0a\u62a5\u4e00\u6b21\u6570\u636e1,\u4e0d\u8fd0\u52a8\u5219\u4e0d\u62a5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,94,160],"tags":[5795,862,5802,5804,5803,5801],"special":[67],"attr":[],"class_list":["post-12526","post","type-post","status-publish","format-standard","hentry","category-appsj","category-appjc","category-appcs","tag-5795","tag-xiaomi","tag-5802","tag-5804","tag-5803","tag-5801","special-xiaomikfpt","entry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/12526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/comments?post=12526"}],"version-history":[{"count":1,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/12526\/revisions"}],"predecessor-version":[{"id":12527,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/12526\/revisions\/12527"}],"wp:attachment":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/media?parent=12526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/categories?post=12526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/tags?post=12526"},{"taxonomy":"special","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/special?post=12526"},{"taxonomy":"attr","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/attr?post=12526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}