Android Service服务的生命周期

与activity类似,服务也存在生命周期回调方法,你可以实现这些方法来监控服务的状态变化,并在适当的时机执行一些操作。 以下代码提纲展示了服务的每个生命周期回调方法: 

public class ExampleService extends Service {
    int mStartMode;       // 标识服务被杀死后的处理方式
    IBinder mBinder;      // 用于客户端绑定的接口
    boolean mAllowRebind; // 标识是否使用onRebind

    @Override
    public void onCreate() {
        // 服务正被创建
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 服务正在启动,由startService()调用引发
        return mStartMode;
    }
    @Override
    public IBinder onBind(Intent intent) {
        // 客户端用bindService()绑定服务
        return mBinder;
    }
    @Override
    public boolean onUnbind(Intent intent) {
        // 所有的客户端都用unbindService()解除了绑定
        return mAllowRebind;
    }
    @Override
    public void onRebind(Intent intent) {
        // 某客户端正用bindService()绑定到服务,
        // 而onUnbind()已经被调用过了
    }
    @Override
    public void onDestroy() {
        // 服务用不上了,将被销毁
    }
}

代码实例:

  LocalService.java

package com.app.servicetest;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class LocalService extends Service { 
	boolean flag = true;
	int count=0; 
	@Override//创建一个服务 继承自Service  重写 onBind方法
	public IBinder onBind(Intent arg0) { 
		//  不支持绑定,所以返回null
		return null;
	} 
	//创建Hander
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			Toast.makeText(LocalService.this, count+"", Toast.LENGTH_SHORT).show();
		};
	};
	@Override
	public void onCreate() {
		super.onCreate();  
		new Thread(new Runnable() {

			@Override
			public void run() {
				while(flag){  
					try {
						 Log.d("--------------", count+" ");   
						 Message msg = new Message();
						 handler.sendMessage(msg);	
						 count++; 
						 Thread.sleep(3000); 
					} catch (InterruptedException e) { 
						e.printStackTrace();
					}
				} 
			}
		}).start() ; 
	}
	
	@Override
	public void onDestroy() {	
		Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
		flag = false;
		super.onDestroy();
	}
}
MainActivity.java

package com.app.servicetest;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	Intent intent ;
	Button button_start,button_stop;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.activity_main);

		button_start =   (Button) findViewById(R.id.button1);
		button_stop =  (Button) findViewById(R.id.button2);

		button_start.setText("创建服务");
		button_stop.setText("Stop"); 

		button_start.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				intent = new Intent(MainActivity.this, LocalService.class); 
				//启动服务
				startService(intent);
				Toast.makeText(MainActivity.this, "启动", 0).show();
			}

		});
		button_stop.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) { 
				//停止 服务
				stopService(intent);
				Toast.makeText(MainActivity.this, "Stop", 0).show();
			} 
		});
	} 
}



posted @ 2013-07-15 11:58  一个学渣  阅读(228)  评论(0编辑  收藏  举报

 

CSDNGitHub 联系邮箱:aikongmeng@gmail.com


Copyright 2008-2019 Government All Rights Reserved