实例:创建、启动、停止和绑定一个Service

创建MainActivity:

import android.app.Activity;
  import android.app.Service;
  import android.content.ComponentName;
  import android.content.Intent;
  import android.content.ServiceConnection;
  import android.os.Bundle;
  import android.os.IBinder;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.Toast;

  import layout.MyService;

  public class MainActivity extends Activity implements View.OnClickListener {

    private Button btn_start,btn_stop,btn_bind,btn_unbind;
      private Intent intent;

      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_start = (Button) findViewById(R.id.btn_start);
        btn_stop = (Button) findViewById(R.id.btn_stop);
        btn_bind = (Button) findViewById(R.id.btn_bind);
        btn_unbind = (Button) findViewById(R.id.btn_unbind);

        intent = new Intent(MainActivity.this, MyService.class);

        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
        btn_bind.setOnClickListener(this);
        btn_unbind.setOnClickListener(this);
    }
      @Override
      public void onClick(View v) {
          switch (v.getId()){
              case R.id.btn_start:
                  startService(intent);
                  break;
              case R.id.btn_stop:
                  stopService(intent);
                  break;
              case R.id.btn_bind:
                  bindService(intent, conn, Service.BIND_AUTO_CREATE);
                  break;
              case R.id.btn_unbind:
                  unbindService(conn);
                  break;
          }
      }
      private ServiceConnection conn = new ServiceConnection() {
          @Override
          public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
              Log.i("SERVICE","连接成功");
              Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_LONG).show();
          }

          @Override
          public void onServiceDisconnected(ComponentName componentName) {
              Log.i("SERVICE","断开连接");
              Toast.makeText(MainActivity.this,"断开连接",Toast.LENGTH_LONG).show();
          }
      };
  }

创建MyService:

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        Log.i("SERVICE","<---------onBind--------->");
        Toast.makeText(MyService.this,"<---------onBind--------->",Toast.LENGTH_LONG).show();
        return null;  //可以返回null,通常返回一个有aidl定义的接口
    }

    // Service创建时调用
    @Override
    public void onCreate() {
        Log.i("SERVICE","<---------onCreate--------->");
        Toast.makeText(MyService.this,"<---------onCreate--------->",Toast.LENGTH_LONG).show();
    }

    //当客户端调用startService()方法启动Service时,该方法被调用

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("SERVICE","<---------onStartCommand--------->");
        Toast.makeText(MyService.this,"<---------onStartCommand--------->",Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    //当Service不再使用时调用

    @Override
    public void onDestroy() {
        Log.i("SERVICE","<---------onDestroy--------->");
        Toast.makeText(MyService.this,"<---------onDestroy--------->",Toast.LENGTH_LONG).show();
    }
}

layout中activity_main是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="绑定Service练习"
        android:textSize="20dp"/>

    <Button
        android:text="@string/btn_start"
        android:id="@+id/btn_start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="@string/btn_stop"
        android:id="@+id/btn_stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="@string/btn_bind"
        android:id="@+id/btn_bind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="@string/btn_unbind"
        android:id="@+id/btn_unbind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

结果如图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值