android 继承binder,继承Binder类绑定服务显示时间

本文详细介绍了如何在Android应用中,通过自定义Binder类实现客户端与本地服务的高效通信,包括创建Binder实例、在服务端的 onBind()方法中返回、客户端的onServiceConnected()回调和实际调用服务方法的过程。适合开发者理解并实践服务间通信的高级技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.如果服务仅仅用于本地应用程序并且不必跨进程工作,则开发人员可以实现自己的Binder类来为客户端提供访问服务公共方法的方式,(注意:这仅仅当客户端与服务位于同一个应用程序和进程时才有效,这也是最常见的情况,例如,音乐播放器需要绑定Activity到自己的服务来在后台播放音乐)

2.实现步骤如下:

21.在服务这种,创建Binder类实例来完成下列操作之一;

211:包含客户端能调用的公共方法

212:返回当前Service实例,其中包含客户端能调用的 公共方法

213:返回服务管理的其他类实例,其中包含客户端能调用的公共方法

22.从onBind()回调方法中返回Binder类实例

3.在客户端,从onServiceConnected()回调方法接受Binder类实例,并且使用提供的方法调用绑定的服务

4:在Activity类中的代码

package com.example.binderservice;

import com.example.binderservice.CurrentTimeService.LocalBinder;

import android.os.Bundle;

import android.os.IBinder;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class CurrentTimeActivity extends Activity {

CurrentTimeService cts;

boolean bound;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

protected void onStart() {

super.onStart();

Button btn=(Button)findViewById(R.id.button1);

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent = new Intent(CurrentTimeActivity.this, CurrentTimeService.class);

bindService(intent, sc, BIND_AUTO_CREATE);//绑定服务

if (bound) {

Toast.makeText(CurrentTimeActivity.this, cts.getCurrentTime(),Toast.LENGTH_LONG).show();

}

}

});

}

protected void onStop() {

super.onStop();

bound=false;

unbindService(sc);

}

private ServiceConnection sc=new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName arg0) {

// TODO Auto-generated method stub

bound=false;

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

// TODO Auto-generated method stub

LocalBinder binder=(LocalBinder)service;//获得自定义的localBinder对象

cts= binder.geTimeService();//获得CurrentTimeService对象

bound=true;

}

};

}

5.Service类中的代码

package com.example.binderservice;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.text.format.Time;

public class CurrentTimeService extends Service{

private final IBinder binder = new LocalBinder();

public class LocalBinder extends Binder{

CurrentTimeService geTimeService(){

return CurrentTimeService.this;}

}

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return binder;

}

public String getCurrentTime() {

Time time = new Time();

time.setToNow();

String currentTimeString=time.format("%Y-%m-%d %H:%M:%S");

return currentTimeString;

}

}

7.配置文件

package="com.example.binderservice"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="16" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".CurrentTimeActivity"

android:label="@string/app_name" >

原文:http://7129486.blog.51cto.com/7119486/1394784

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值