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

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值