网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Service绑定日期

Service分类:Start启动和bound绑定

成都创新互联公司专注于鄂州网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供鄂州营销型网站建设,鄂州网站制作、鄂州网页设计、鄂州网站官网定制、成都微信小程序服务,打造鄂州网络公司原创品牌,更为您提供鄂州网站排名全网营销落地服务。

下面主要讲继承Binder类输出当前时间(主要实现onBind()回调方法)

测试如下:

Service绑定日期

其实就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

    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" >

            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" />

            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" />


GG!!!!就这么简单,主要是练习Service应用


网站标题:Service绑定日期
本文网址:http://bjjierui.cn/article/ipgpgi.html

其他资讯