Android之Service使用解析

来源:互联网 发布:万网域名cname解析 编辑:程序博客网 时间:2024/06/06 03:17

转载请注明出处:http://blog.csdn.net/professionit/article/details/71600788


Service是安卓四大组件之一,凡是接触安卓的人员应该都知道Service,但是具体Service怎么使用,大家可能还糊里糊涂,下面就来简单介绍下Service的使用吧

1、首先创建一个类继承自service

package com.daokou.servicetest;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;/** * Created by mapeng on 2017/5/10. * EMail:704934878@qq.com */public class MyService extends Service {    public static final String TAG = "MyService";    private MyBinder mBinder = new MyBinder();    @Override    public void onCreate() {        super.onCreate();        Log.d(TAG, "onCreate()");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.d(TAG, "onStartCommand()");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        Log.d(TAG, "onDestroy()");        super.onDestroy();    }    @Override    public IBinder onBind(Intent intent) {        return mBinder;    }    class MyBinder extends Binder {        public void startDownload() {            Log.d("TAG", "startDownload()");            // 具体做后台处理的事情,例如后台下载啊        }    }}

2、再manifest中配置此service类

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.daokou.servicetest">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service android:name=".MyService" ></service>    </application></manifest>

3、mainactivity中绑定并启用service

package com.daokou.servicetest;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private Button startService;    private Button stopService;    private Button bindService;    private Button unbindService;    private MyService.MyBinder myBinder;    private ServiceConnection connection = new ServiceConnection() {        @Override        public void onServiceDisconnected(ComponentName name) {        }        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            myBinder = (MyService.MyBinder) service;            myBinder.startDownload();        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        startService = (Button) findViewById(R.id.start_service);        stopService = (Button) findViewById(R.id.stop_service);        bindService = (Button) findViewById(R.id.bind_service);        unbindService = (Button) findViewById(R.id.unbind_service);        startService.setOnClickListener(this);        stopService.setOnClickListener(this);        bindService.setOnClickListener(this);        unbindService.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.start_service:                Intent startIntent = new Intent(this, MyService.class);                startService(startIntent);                break;            case R.id.stop_service:                Intent stopIntent = new Intent(this, MyService.class);                stopService(stopIntent);                break;            case R.id.bind_service:                Intent bindIntent = new Intent(this, MyService.class);                bindService(bindIntent, connection, BIND_AUTO_CREATE);                break;            case R.id.unbind_service:                unbindService(connection);                break;            default:                break;        }    }}

4、xml文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/start_service"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Start Service" />    <Button        android:id="@+id/stop_service"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Stop Service" />    <Button        android:id="@+id/bind_service"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Bind Service" />    <Button        android:id="@+id/unbind_service"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Unbind Service"        /></LinearLayout>

好了,以上的准备工作都做好了,下面运行起来,就可以使用了

注意:以绑定的方式启动时,当activity销毁时,service也会随之销毁,只要不销毁,service就会继续后台运行。

1 0
原创粉丝点击