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>
CurrentTimeService1.java
package com.example.demoboundservice;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.text.format.Time;
import android.widget.Toast;
public class CurrentTimeService1 extends Service{
public static final int CURRENT_TIME=0;
private class IncomingHandler extends Handler{
public void handleMessage(Message msg){
if(msg.what==CURRENT_TIME){
Time time = new Time();
time.setToNow();
String currentTime = time.format("%Y-%m-%d %H:%M:%S");
Toast.makeText(CurrentTimeService1.this,currentTime,Toast.LENGTH_LONG).show();
}else{
super.handleMessage(msg);
}
}
}
public IBinder onBind(Intent intent){
Messenger messenger = new Messenger(new IncomingHandler());
return messenger.getBinder();
}
}
CurrentTimeActivity.java:
package com.example.demoboundservice;
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.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
public class CurrentTimeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Messenger messenger;
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(CurrentTimeActivity.this,CurrentTimeService.class);
bindService(intent,connection,BIND_AUTO_CREATE);
if(bound){
Message message = Message.obtain(null,CurrentTimeService1.CURRENT_TIME,0,0);
try{
messenger.send(message);
}catch(RemoteException e){
e.printStackTrace();
}
}
}
});
}
protected void onStop(){
super.onStop();
if(bound){
bound = false;
unbindService(connection);
}
}
private ServiceConnection connection = new ServiceConnection(){
public void onServiceDisconnected(ComponentName name){
messenger = null;
bound = false;
}
public void onServiceConnected(ComponentName name,IBinder service){
messenger = new Messenger(service);
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.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="com.example.demoboundservice.CurrentTimeService1"/>
</application>
</manifest>
转载于:https://my.oschina.net/u/2552902/blog/543891