温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-28 13:15:15 来源:网络 阅读:494 作者:王琦123456 栏目:移动开发

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.配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.binderservice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".CurrentTimeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".CurrentTimeService"></service>
    </application>

</manifest>

 


 

 

附件:http://down.51cto.com/data/2364291
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI