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

android Bound Service使用:继续Binder类绑定服务

2015-12-14 00:00 766 查看
activity_main.xml:

<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=".MainActivity" >

<Button android:id="@+id/current_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前时间"
android:textColor="@android:color/white"
android:textSize="25dp"/>

</RelativeLayout>


CurrentTimeService.java:

package com.example.demoboundservice;

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 getService(){
return CurrentTimeService.this;
}
}

public IBinder onBind(Intent intent){
return binder;
}

public String getCurrentTime(){
Time time = new Time();
time.setToNow();
String currentTime = time.format("%Y-%m-%d %H:%M:%S");
return currentTime;
}
}


MainActivity.java:

package com.example.demoboundservice;

import com.example.demoboundservice.CurrentTimeService.LocalBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

CurrentTimeService cts ;
boolean bound;

protected void onStart(){
super.onStart();
Button button = (Button)findViewById(R.id.current_time);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {

Intent intent = new Intent(MainActivity.this,CurrentTimeService.class);
bindService(intent,sc,BIND_AUTO_CREATE);
if(bound){
Toast.makeText(MainActivity.this, cts.getCurrentTime(), Toast.LENGTH_LONG).show();
}
}
});
}

protected void onStop(){
super.onStop();
if(bound){
bound  = false;
unbindService(sc);
}
}

private ServiceConnection sc = new ServiceConnection(){
public void onServiceDisconnected(ComponentName name){
bound = false;
}
public void onServiceConnected(ComponentName name,IBinder service){
LocalBinder binder  =(LocalBinder)service;
cts = binder.getService();
bound = true;
}
};

}


AndroidManifest.xml:

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

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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.demoboundservice.MainActivity"
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="com.example.demoboundservice.CurrentTimeService"/>
</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: