{"id":990,"date":"2017-03-23T18:05:03","date_gmt":"2017-03-23T10:05:03","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=990"},"modified":"2017-03-23T18:05:03","modified_gmt":"2017-03-23T10:05:03","slug":"android%e5%bc%80%e5%8f%91%e4%b9%8bservice%e7%9a%84%e4%b8%a4%e7%a7%8d%e5%ae%9e%e7%8e%b0%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/android\/2017\/03\/23\/180503990.html","title":{"rendered":"Android\u5f00\u53d1\u4e4bService\u7684\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f"},"content":{"rendered":"<pre line=\"1\" lang=\"java\">package com.yusian.service;\n\nimport android.app.Service;\nimport android.content.ComponentName;\nimport android.content.Intent;\nimport android.content.ServiceConnection;\nimport android.os.Binder;\nimport android.os.IBinder;\nimport android.support.annotation.Nullable;\nimport android.support.v7.app.AppCompatActivity;\nimport android.os.Bundle;\nimport android.util.Log;\nimport android.view.View;\nimport android.widget.Button;\n\npublic class MainActivity extends AppCompatActivity implements View.OnClickListener{\n    private Button btnStartService, btnStopService, btnBindService, btnUnBindService;\n    private MyService.MyBinder mBinder;\n    private ServiceConnection connection;\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        btnStartService = (Button)findViewById(R.id.btn_start_service);\n        btnStopService = (Button) findViewById(R.id.btn_close_service);\n        btnBindService = (Button) findViewById(R.id.btn_bind_service);\n        btnUnBindService = (Button) findViewById(R.id.btn_unbind_service);\n        btnStartService.setOnClickListener(this);\n        btnStopService.setOnClickListener(this);\n        btnBindService.setOnClickListener(this);\n        btnUnBindService.setOnClickListener(this);\n\n        connection = new ServiceConnection() {\n            @Override\n            \/\/ Service\u4e0eActivity\u5efa\u7acb\u8fde\u63a5\n            public void onServiceConnected(ComponentName name, IBinder service) {\n                \/\/ \u8fd9\u91cc\u7684IBinder\u5c31\u662fMyService\u4e2donBind()\u8fd4\u56de\u7684IBinder\n                mBinder = (MyService.MyBinder) service;\n            }\n            @Override\n            \/\/ Service\u4e0eActivity\u5931\u53bb\u8fde\u63a5\u65f6\u8c03\u7528\n            public void onServiceDisconnected(ComponentName name) {\n                mBinder = null;\n            }\n        };\n    }\n\n    @Override\n    public void onClick(View v) {\n        Intent intent = new Intent(this, MyService.class);\n        switch (v.getId()){\n            \/\/ \u5f00\u542f\u670d\u52a1\n            case R.id.btn_start_service:{\n                startService(intent);\n            }break;\n            \/\/ \u5173\u95ed\u670d\u52a1\n            case R.id.btn_close_service:{\n                stopService(intent);\n            }break;\n            \/\/ \u7ed1\u5b9a\u670d\u52a1\n            case R.id.btn_bind_service:{\n                bindService(intent, connection, BIND_AUTO_CREATE);\n            }break;\n            \/\/ \u89e3\u7ed1\u670d\u52a1\n            case R.id.btn_unbind_service:{\n                unbindService(connection);\n            }break;\n            default:break;\n        }\n    }\n}\n\nclass MyService extends Service {\n    private final static String TAG = \"SALog\";\n    private MyBinder mBinder;\n    public MyService() {\n\n    }\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        mBinder = new MyBinder();\n        Log.d(TAG, \"onCreate: \");\n    }\n    @Override\n    public int onStartCommand(Intent intent, int flags, int startId) {\n        Log.d(TAG, \"onStartCommand: \");\n        return super.onStartCommand(intent, flags, startId);\n    }\n    @Nullable\n    @Override\n    public IBinder onBind(Intent intent) {\n        Log.d(TAG, \"onBind: \");\n        return mBinder;\n    }\n    @Override\n    public void onDestroy() {\n        super.onDestroy();\n        Log.d(TAG, \"onDestroy: \");\n    }\n\n    class MyBinder extends Binder{\n\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>package com.yusian.service; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btnStartService, btnStopService, btnBindService, btnUnBindService; private MyService.MyBinder mBinder; private ServiceConnection connection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStartService = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[33,20],"class_list":["post-990","post","type-post","status-publish","format-standard","hentry","category-android","tag-33","tag-basics"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=990"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/990\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}