ServiceDemo

来源:互联网 发布:免费交换机网管软件 编辑:程序博客网 时间:2024/06/05 05:27

Service

package com.syl.basicsummary.service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.support.annotation.Nullable;import android.util.Log;import android.widget.Toast;/** * Created by j3767 on 2017/3/1. * * @Describe 服务 * @Called */public class MyService extends Service {    public static final String TAG = MyService.class.getSimpleName();    private MyBinder mMyBinder = new MyBinder();    @Nullable    @Override    public IBinder onBind(Intent intent) {        Toast.makeText(this, "MyService---onBind()被调用---", Toast.LENGTH_SHORT).show();        Log.d(TAG, "onBind() ----绑定服务");        return mMyBinder;    }    @Override    public void onCreate() {        Toast.makeText(this, "MyService---onCreate()被调用---", Toast.LENGTH_SHORT).show();        Log.d(TAG, "onCreate() ----创建服务");        super.onCreate();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Toast.makeText(this, "MyService---onStartCommand()被调用---", Toast.LENGTH_SHORT).show();        Log.d(TAG, "onStartCommand() ----");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        Toast.makeText(this, "MyService---onDestroy()被调用---", Toast.LENGTH_SHORT).show();        Log.d(TAG, "onDestroy() ----销毁服务");        super.onDestroy();    }    @Override    public boolean onUnbind(Intent intent) {        Toast.makeText(this, "MyService---onUnbind()被调用---", Toast.LENGTH_SHORT).show();        Log.d(TAG, "onUnbind() ----你解绑服务");        return super.onUnbind(intent);    }    /**     * author   j3767     * date     2017/3/2 0:14     * desc     * 必须使用public修饰符,否则外部无法调用     */    public class MyBinder extends Binder {        public void callMethodInService() {            methodInService();        }    }    /**     * 创建服务的代理,调用服务中的方法     * 服务中的方法     */    public void methodInService() {        Log.d(TAG, "methodInService----服务中的方法");    }}

与Service交互的Activity

package com.syl.basicsummary.activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.content.res.AssetManager;import android.os.Bundle;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.webkit.WebSettings;import android.webkit.WebView;import android.widget.Button;import com.syl.basicsummary.R;import com.syl.basicsummary.service.MyService;import com.syl.basicsummary.utils.WebUtil;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;/** * author   j3767 * date     2017/3/1 20:54 * desc * 和Service进行交互的界面 * <p> * Service的使用步骤: * 1.创建Service的子类MyService,实现对应的生命周期方法 * 2.在MyService中创建一个内部类MyBinder,继承自Binder,提供给外界访问 * 3.在Activity中创建一个MyConnection,实现ServiceConnection接口,将回调参数IBinder service赋值给 * mMyBinder = (MyService.MyBinder) service; */public class ServiceActivity extends AppCompatActivity {    public static final String TAG = ServiceActivity.class.getSimpleName();    @BindView(R.id.btn_start_service)    Button mBtnStartService;    @BindView(R.id.btn_stop_service)    Button mBtnStopService;    @BindView(R.id.webView)    WebView mWebView;    private MyService.MyBinder mMyBinder;    private MyConn mMyConn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_service);        ButterKnife.bind(this);        WebSettings settings = mWebView.getSettings();        settings.setJavaScriptEnabled(true);//        settings.setUseWideViewPort(true);//适应分辨率        settings.setLoadWithOverviewMode(true);    }    /**     * 开启服务     *     * @param view     */    @OnClick(R.id.btn_start_service)    public void btn_start_service(View view) {        Intent intent = new Intent(this, MyService.class);        startService(intent);    }    /**     * 停止服务     *     * @param view     */    @OnClick(R.id.btn_stop_service)    public void btn_stop_service(View view) {        Intent intent = new Intent(this, MyService.class);        stopService(intent);    }    /**     * 绑定服务     *     * @param view     */    @OnClick(R.id.btn_bind_service)    public void btn_bind_service(View view) {        if (mMyConn == null) {            mMyConn = new MyConn();        }        Intent intent = new Intent(this, MyService.class);        bindService(intent, mMyConn, BIND_AUTO_CREATE);    }    /**     * 解绑服务     *     * @param view     */    @OnClick(R.id.btn_unbind_service)    public void btn_unbind_service(View view) {        if (mMyConn == null) {            mMyConn = new MyConn();        }        unbindService(mMyConn);    }    /**     * 调用服务中的方法     *     * @param view     */    @OnClick(R.id.btn_call_in_service)    public void btn_call_in_service(View view) {        mMyBinder.callMethodInService();    }    /**     * 实现回调接口ServiceConnection的的类     */    private class MyConn implements ServiceConnection {        /**         * 连接成功         *         * @param name         * @param service         */        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            mMyBinder = (MyService.MyBinder) service;            Log.d(TAG, "服务成功绑定---" + service.toString());        }        /**         * 断开连接         *         * @param name         */        @Override        public void onServiceDisconnected(ComponentName name) {        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.menu_menu, menu);        return super.onCreateOptionsMenu(menu);    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {            case R.id.item_load_code:                loadCodeInHtml();//加载html中的代码                break;            default:                break;        }        return super.onOptionsItemSelected(item);    }    /**     * 加载html     */    private void loadCodeInHtml() {        AssetManager assetManager = getResources().getAssets();        String url = WebUtil.getFromAssets("serviceDemo.htm", assetManager);        mWebView.loadDataWithBaseURL(null, url, "text/html", "utf-8", null);    }}
0 0
原创粉丝点击