android初接觸之service2

在android初接觸之service1基礎上繼續學習
將服務綁定在調用的service的客戶類上:
在activity_main.xml中增加連個button用來控制啓動綁定服務

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test.MainActivity" >

    <Button
        android:id="@+id/startService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="啓動服務" />

    <Button
        android:id="@+id/destoryService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/startService"
        android:layout_alignRight="@+id/startService"
        android:layout_below="@+id/startService"
        android:text="註銷服務" />

    <Button
        android:id="@+id/stopBind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/destoryService"
        android:layout_alignRight="@+id/destoryService"
        android:layout_below="@+id/destoryService"
        android:text="解除綁定" />

    <Button
        android:id="@+id/startBind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/stopBind"
        android:layout_alignRight="@+id/stopBind"
        android:layout_below="@+id/stopBind"
        android:text="綁定服務" />

</RelativeLayout>

在MainActivity.java中實現了ServiceConnection

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity implements OnClickListener, ServiceConnection {
    private Button startService,destroyService,startBind,stopBind;
    private Intent IntentService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //創建調用服務的Intent
        IntentService = new Intent(this,ServiceTest.class);
        //獲取button按鈕
        startService = (Button) findViewById(R.id.startService);
        destroyService = (Button) findViewById(R.id.destoryService);
        startBind = (Button) findViewById(R.id.startBind);
        stopBind = (Button) findViewById(R.id.stopBind);
        startService.setOnClickListener(this);
        destroyService.setOnClickListener(this);
        startBind.setOnClickListener(this);
        stopBind.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.startService:
            //啓動服務
            startService(IntentService);
            break;
        case R.id.destoryService:
            //停止服務
            stopService(IntentService);
            break;
        case R.id.startBind:
            //綁定服務
            bindService(IntentService, this, Context.BIND_AUTO_CREATE);
            break;
        case R.id.stopBind:
            //解除綁定
            unbindService(this);
            break;
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub
        System.out.println("綁定服務");
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
        //這個方法作用還不清楚,請讀者自行使用,在這裏不使用
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

在ServiceTest實現了OnBind方法

package com.example.test;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class ServiceTest extends Service {
    //必須有返回值才能調用綁定方法
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("綁定");
        return new IBinderTest();
    }
    private class IBinderTest extends Binder{

    }
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        System.out.println("服務啓動");
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println("服務註銷");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章