{"id":1826,"date":"2022-11-25T18:00:26","date_gmt":"2022-11-25T10:00:26","guid":{"rendered":"https:\/\/app.applebyme.cn\/cloud\/appshangjia\/1826.html"},"modified":"2022-11-25T18:00:26","modified_gmt":"2022-11-25T10:00:26","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:\/\/app.applebyme.cn\/cloud\/appshangjia\/1826.html","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":"<h2 id=\"_0\">1.<span class=\"wpcom_tag_link\">\u5c0f\u7c73\u8dd1\u6b65\u673a<\/span><span class=\"wpcom_tag_link\">\u4f20\u611f\u5668<\/span>\u7b80\u4ecb<\/h2>\n<p>\u5f53\u624b\u673a\u653e\u7f6e\u5728<span class=\"wpcom_tag_link\">\u8dd1\u6b65\u673a<\/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<h2 id=\"_1\">2.\u5c06\u8dd1\u6b65\u673a<span class=\"wpcom_tag_link\">\u6570\u636e\u96c6\u6210<\/span>\u5230\u8ba1\u6b65\u5668<span class=\"wpcom_tag_link\">\u6570\u636e\u5e93<\/span><\/h2>\n<h3 id=\"_1_0\">2.1.\u901a\u77e5\u7cfb\u7edf\u670d\u52a1<\/h3>\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<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\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>onAccu<a href=\"https:\/\/app.applebyme.cn\/shengcheng-55.html\">\u7f51\u9875\u751f\u6210app\u8f6f\u4ef6<\/a>racyChanged<\/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<h3 id=\"_1_1\">2.2.ServiceManager\u62ff\u4e0d\u5230\u89e3\u51b3\u65b9\u6848<\/h3>\n<p>\u5728\u4ee3\u7801\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305\u540d\u4e3aandroid.os\uff0c\u7c7b\u540d\u4e3aServiceManager\u7684\u7c7b\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\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<h3 id=\"_1_2\">2.3.\u8bf4\u660e<\/h3>\n<p>\u76ee\u524d\u8dd1\u6b65\u673a\u8ba1\u6b65\u4f20\u611f\u5668\u652f\u6301<span class=\"wpcom_tag_link\">\u5c0f\u7c73<\/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<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> Tr<a href=\"https:\/\/app.applebyme.cn\/shengcheng-29.html\">h5\u751f\u6210app<\/a>eadmillListener(); <\/code><\/pre>\n<p>\u7f16\u8f91\uff1ayimen\uff0c\u5982\u82e5\u8f6c\u8f7d\uff0c\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttps:\/\/www.yimenapp.com\/kb-yimen\/12526\/<\/p>\n<p>\u90e8\u5206\u5185\u5bb9\u6765\u81ea\u7f51\u7edc\u6295\u7a3f\uff0c\u5982\u6709\u4fb5\u6743\u8054\u7cfb\u7acb\u5220<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u5c0f\u7c73\u8dd1\u6b65\u673a\u4f20\u611f\u5668\u7b80\u4ecb\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\u30022.\u5c06\u8dd1\u6b65\u673a\u6570\u636e\u96c6\u6210\u5230\u8ba1\u6b65\u5668\u6570\u636e\u5e932.1.\u901a\u77e5\u7cfb\u7edf\u670d\u52a1\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<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[914,548,1281],"class_list":["post-1826","post","type-post","status-publish","format-standard","hentry","category-appshangjia","tag-914","tag-548","tag-1281"],"_links":{"self":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts\/1826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/comments?post=1826"}],"version-history":[{"count":0,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts\/1826\/revisions"}],"wp:attachment":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/media?parent=1826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/categories?post=1826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/tags?post=1826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}