android四大组件之服务

温故而知新 梳理一下android知识


Service的基本用法

  启动和关闭服务有两种方法代码如下:

package servicetest.baoxin.com.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        findViewById(R.id.btn3).setOnClickListener(this);
        findViewById(R.id.btn4).setOnClickListener(this);
    }

    ServiceConnection connection = new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            MyServiece.MyBind bind = (MyServiece.MyBind) iBinder;
            bind.download("下载");
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:
                Intent intent  = new Intent(this,MyServiece.class);
                startService(intent);
                break;
            case R.id.btn2:
                Intent intent2  = new Intent(this,MyServiece.class);
                stopService(intent2);
                break;
            case R.id.btn3:
                Intent intent3  = new Intent(this,MyServiece.class);
                bindService(intent3,connection, BIND_AUTO_CREATE);
                break;
            case R.id.btn4:
                unbindService(connection);
                break;
            default:
                break;
        }
    }
}


package servicetest.baoxin.com.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by weibinbin on 2017/8/28.
 */

public class MyServiece extends Service {
   private static final String TAG = "MyService";
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG,"...onCreate...");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG,"...onStartCommand...");
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        Log.e(TAG,"...onDestroy...");
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBind();
    }

    class MyBind extends Binder{
         public void download(String p){
             Log.e(TAG,"...download..."+p);
         }
    }
}


使用第一种启动方式,第一次启动走onCreate和onStartCommand,启动后再次点击启动只走,onStartCommand

          //第一种service启动方式
            Intent intent  = new Intent(this,MyServiece.class);
            startService(intent);

使用第一种关闭方式,根据日志看到走onDestory方法

            Intent intent2  = new Intent(this,MyServiece.class);
            stopService(intent2);
 

使用第二种启动方式,根据日志可知,走了onCreate和onBind方法,再次点击启动,如果已经启动不做任何方法了

            Intent intent2  = new Intent(this,MyServiece.class);
            stopService(intent2);
使用第二种关闭方式,根据日志可知,走了onDestory方法

            unbindService(connection);

如果需要和Service通信,使用第二种方法

Servic后台运行和Thread线程容易产生的误会

  其实service和Thread没有任何关系,Service也是在主线程跑的,注意:不要把Service也看成一个特殊线程。

Service一个标准用法


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e(TAG,"...onStartCommand...");
    new Thread(new Runnable() {
        @Override
        public void run() {
            Log.e(TAG,"...执行任务...");
        }
    });
    return super.onStartCommand(intent, flags, startId);

}
class MyBind extends Binder{
     public void download(String p){
         new Thread(new Runnable() {
             @Override
             public void run() {
                 Log.e(TAG,"...执行任务...");
             }
         });
         Log.e(TAG,"...download..."+p);
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值