RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
继承Binder类绑定服务显示时间

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

创新互联建站于2013年开始,是专业互联网技术服务公司,拥有项目成都网站设计、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元盐亭做网站,已为上家服务,为盐亭各地企业和个人服务,联系电话:18982081108

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://down.51cto.com/data/2364291

标题名称:继承Binder类绑定服务显示时间
本文URL:http://sczitong.cn/article/gheich.html