您的位置:首页 > 移动开发 > Android开发

android初接触之service2

2016-01-14 21:39 561 查看
在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("服务注销");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: