新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

继承Binder类绑定服务显示时间-创新互联

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


标题名称:继承Binder类绑定服务显示时间-创新互联
新闻来源:http://www.wjwzjz.com/article/dppgci.html
在线咨询
服务热线
服务热线:028-86922220
TOP