Service更新UI

来自:http://www.cnblogs.com/gqsblog/archive/2012/04/17/2453378.html

直接贴代码:

 

package com.gqs.activity;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import com.gqs.service.UpdateService;import com.gqs.service.UpdateService.MyBinder;import com.gqs.service.UpdateService.UpdateData;public class ServiceToActivityActivity extends Activity {
    /** Called when the activity is first created. */     private TextView tv;
    private UpdateService.MyBinder binder;
    private Button btnStart;
    private ServiceConnection conn=new ServiceConnection() {
        
        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub             
        }
        
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub             binder=(MyBinder) service;
            tv.setText("已连接");
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnStart=(Button)findViewById(R.id.btnStart);
        tv=(TextView)findViewById(R.id.textView);
        Intent intent=new Intent(this,UpdateService.class);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
        btnStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub                 if(binder!=null)
                {
                    binder.setDate(tv, new UpdateData() {
                        
                        @Override
                        public void update(TextView tv, int data) {
                            // TODO Auto-generated method stub                             tv.setText(data+"");
                        }
                    });
                }
                else                 {
                    Toast.makeText(getApplicationContext(), "连接失败", 1).show();
                }
            }
        });
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub         unbindService(conn);
        super.onDestroy();
    }
}
  package com.gqs.service; 
 import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.widget.TextView; 
 public class UpdateService extends Service {     private int data;     private Handler handler; 
     @Override     public IBinder onBind(Intent intent) {         // TODO Auto-generated method stub          return new MyBinder();     } 
     public class MyBinder extends Binder {         public void setDate(final TextView tv, final UpdateData updata) {             new Thread(new MyThread()).start();             handler = new Handler() {                 public void handleMessage(Message msg) {                     updata.update(tv, data);                 }             };         }     } 
     public class MyThread implements Runnable { 
         @Override         public void run() {             while (true) {                 data++;                 Message msg = handler.obtainMessage();                 msg.arg1 = data;                 handler.sendMessage(msg);                 try {                     Thread.sleep(1000);                 } catch (InterruptedException e) {                     // TODO Auto-generated catch block                      e.printStackTrace();                 }             }         } 
     } 
     public interface UpdateData {         public void update(TextView tv, int data); 
     } }

你可能感兴趣的:(android,service)