android service生命周期

创建Service需要继承android.app.Service类。Service的生命周期回掉方法主要有六个。onCreat,onDestroy,onStart,onBind,onUnBind,onRebind。

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	ServiceConnection sc ;
	OnClickListener listener ;
	Handler hd = new Handler(){public void handleMessage(android.os.Message msg) {
		switch(msg.what){
		case 0 : Toast.makeText(MainActivity.this, "onBind", Toast.LENGTH_LONG).show();break ;
		case 1 : Toast.makeText(MainActivity.this, "onUnBind", Toast.LENGTH_LONG).show();break ;
		case 2 : Toast.makeText(MainActivity.this, "onDestroy", Toast.LENGTH_LONG).show();break ;
		case 3 : Toast.makeText(MainActivity.this, "onCreate", Toast.LENGTH_LONG).show();break ;
		}
	};} ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.w6_1) ;
		sc = new ServiceConnection() {
			public void onServiceDisconnected(ComponentName name) {
			}
			public void onServiceConnected(ComponentName name, IBinder service) {
			}
		};
		Button btn1 = (Button) findViewById(R.id.w6_1_btn1) ; 
		Button btn2 = (Button) findViewById(R.id.w6_1_btn2) ; 
		Button btn3 = (Button) findViewById(R.id.w6_1_btn3) ; 
		Button btn4 = (Button) findViewById(R.id.w6_1_btn4) ; 
		
		listener = new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,MyService.class) ;
				switch(v.getId()){
				case R.id.w6_1_btn1 : startService(intent);hd.sendEmptyMessage(3); break ;
				case R.id.w6_1_btn2 : stopService(intent);hd.sendEmptyMessage(2);break ;
				case R.id.w6_1_btn3 : bindService(intent,sc,BIND_AUTO_CREATE);hd.sendEmptyMessage(0);break ;
				case R.id.w6_1_btn4 : unbindService(sc) ;hd.sendEmptyMessage(1) ;break ;
				}
			}
		};
		btn1.setOnClickListener(listener);
		btn2.setOnClickListener(listener);
		btn3.setOnClickListener(listener);
		btn4.setOnClickListener(listener);
	}   
}

package com.example.a_wuyafeng;

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

public class MyService extends Service {
 
	public IBinder onBind(Intent intent) {
		System.out.println("onBind");
		return null;
	}
	@Override
	public void onCreate() {
		// TODO 自动生成的方法存根
		super.onCreate();
		System.out.println("oncreate");
	}
	
	@Override
	public void onDestroy() {
		// TODO 自动生成的方法存根
		super.onDestroy();
		System.out.println("ondestroy");
	}
	@Override
	public boolean onUnbind(Intent intent) {
		// TODO 自动生成的方法存根
		System.out.println("onunbind");
		return super.onUnbind(intent);
	}
	@Override
	public void onRebind(Intent intent) {
		// TODO 自动生成的方法存根
		super.onRebind(intent);
		System.out.println("onRebind");
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO 自动生成的方法存根
		super.onStart(intent, startId);
		System.out.println("start");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值