實例:創建、啓動、停止和綁定一個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>

結果如圖:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章