Service分类:Start启动和bound绑定
下面主要讲继承Binder类输出当前时间(主要实现onBind()回调方法)
测试如下:
其实就3步!
第一.TimeService类
package com.example.timeservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.text.format.Time;
public class TimeService extends Service {
private IBinder binder=new localBinder();
public class localBinder extends Binder{
TimeService getService(){
return TimeService.this;
}
}
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return binder;
}
public String getCurrentTime(){
Time time =new Time();
time.setToNow();
String stringtime=time.format("%Y-%m-%d %H:%M:%S");
return stringtime;
}
}
第二.MainActivity
package com.example.timeservice;
import java.util.Random;
import com.example.timeservice.TimeService.localBinder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.widget.TextView;
public class MainActivity extends Activity {
protected TimeService cts;
boolean bound;
Handler handler=null;
TextView text;
TextView text2;
private int i=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.text);
text2=(TextView)findViewById(R.id.text2);
handler=new Handler(){
public void handleMessage(Message msg){
if(msg.what==0x101){
onStart();
text2.setText("测试:"+i);
}
super.handleMessage(msg);
}
};
Thread t=new Thread(new Runnable(){
public void run() {
while(!Thread.currentThread().isInterrupted()){
i++;
Message m=handler.obtainMessage();
m.what=0x101;
handler.sendMessage(m);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t.start();
}
protected void onStart(){
super.onStart();
Intent intent=new Intent(MainActivity.this,TimeService.class);
bindService(intent, sc, BIND_AUTO_CREATE);
if(bound)
text.setText(cts.getCurrentTime());
}
protected void onStop(){
super.onStop();
if(bound){
bound=false;
unbindService(sc);
}
}
private ServiceConnection sc=new ServiceConnection() {
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
bound=false;
}
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
localBinder binder=(localBinder)arg1;
cts=binder.getService();
bound=true;
}
};
}
3.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" >
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="----------"
android:textSize="30dp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/text2"
android:layout_below="@+id/text2"
android:layout_marginTop="16dp"
android:text="----------"
android:textSize="30dp" />
</RelativeLayout>
GG!!!!就这么简单,主要是练习Service应用