android关于service()

MainActivity:
package com.example.myservice1;
import com.example.myservice1.Myservice.MyBinder;
import android.os.Bundle;
import android.os.IBinder;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class MainActivity extends Activity implements OnClickListener,ServiceConnection{
    private Button start,stop,bind,unbind,get;
     private Intent intent;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start=(Button) findViewById(R.id.start);
        stop=(Button) findViewById(R.id.stop);
        bind=(Button) findViewById(R.id.bind);
        unbind=(Button) findViewById(R.id.unbind);
        get=(Button) findViewById(R.id.get);
        start.setOnClickListener(this);
        stop.setOnClickListener(this);
        bind.setOnClickListener(this);
        unbind.setOnClickListener(this);
        get.setOnClickListener(this);
        intent=new Intent(MainActivity.this,Myservice.class);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.start:
            startService(intent);            
            break;
        case R.id.stop:
            stopService(intent);
            break;
        case R.id.bind:
            bindService(intent, this, BIND_AUTO_CREATE);
            break;
        case R.id.unbind:
            unbindService(this);
            break;
        case R.id.get:
            Toast.makeText(MainActivity.this,
                    "当前Service的值为:" + my.getIndex(), Toast.LENGTH_LONG).show();
            break;            
        default:
            break;
        }
        
        
    }
    private Myservice my;
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {        
        System.out.println("onServiceConnected");
        Myservice.MyBinder m=(MyBinder) service;
        my=m.getMyService();        
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {                
    }

}

 




Myservice:
package com.example.myservice1;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class Myservice extends Service {

    private Timer timer;
    private TimerTask task;
    private int index = 0;

    // 与Activicy进行绑定
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
    //得到binder对象
    private MyBinder binder = new MyBinder();

    //自定义已给MyBinder继承Binder实现通信
    public class MyBinder extends Binder{
        //获得当前Service的状态
        public Myservice getMyService(){
            
            return Myservice.this;
        }
    }
    
    
    @Override
    public void onCreate() {
        super.onCreate();
        startTimer();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    // 开始执行Timer
    public void startTimer() {
        timer = new Timer();
        task = new TimerTask() {

            @Override
            public void run() {
                index++;
                System.out.println(index);
            }
        };
        timer.schedule(task, 1000, 1000);
    }

    // 停止Timer的执行
    public void stopTimer() {
        timer.cancel();
    }
    //实现可读性(Index)
    public int getIndex() {
        return index;
    }
}
    

 

 

 

 

posted @ 2013-04-21 16:24  佬D  阅读(250)  评论(0编辑  收藏  举报