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
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" >
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" />
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" />
GG!!!!就这么简单,主要是练习Service应用
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款