android Service实例解释

直接上代码
类1
package com.iceskysl.TestServiceHolder;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;


public class TestService extends Service {


	private static final String TAG = "TestService";
	private NotificationManager _nm;


	@Override
	public IBinder onBind(Intent i) {
		Log.e(TAG, "============> TestService.onBind");
		return null;
	}


	public class LocalBinder extends Binder {
		TestService getService() {
			return TestService.this;
		}
	}


	@Override
	public boolean onUnbind(Intent i) {
		Log.e(TAG, "============> TestService.onUnbind");
		return false;
	}


	@Override
	public void onRebind(Intent i) {
		Log.e(TAG, "============> TestService.onRebind");
	}


	@Override
	public void onCreate() {
		Log.e(TAG, "============> TestService.onCreate");
		_nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		showNotification();
	}


	@Override
	public void onStart(Intent intent, int startId) {
		Log.e(TAG, "============> TestService.onStart");
	}


	@Override
	public void onDestroy() {
		_nm.cancel(R.string.service_started);
		Log.e(TAG, "============> TestService.onDestroy");
	}


	private void showNotification() {
		Notification notification = new Notification(R.drawable.face_1,
				"Service started", System.currentTimeMillis());


		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, TestServiceHolder.class), 0);


		// must set this for content view, or will throw a exception
		notification.setLatestEventInfo(this, "Test Service",
				"Service started", contentIntent);


		_nm.notify(R.string.service_started, notification);
	}


}
类2
package com.iceskysl.TestServiceHolder;


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.Toast;


public class TestServiceHolder extends Activity {
	private boolean _isBound;
	private TestService _boundService;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setTitle("Service Test");


		initButtons();


	}
	
    private ServiceConnection _connection = new ServiceConnection() {  
        public void onServiceConnected(ComponentName className, IBinder service) {           
            _boundService = ((TestService.LocalBinder)service).getService();  
              
            Toast.makeText(TestServiceHolder.this, "Service connected",  
                    Toast.LENGTH_SHORT).show();  
        }  
  
        public void onServiceDisconnected(ComponentName className) {  
            // unexpectedly disconnected,we should never see this happen.  
            _boundService = null;  
            Toast.makeText(TestServiceHolder.this, "Service connected",  
                    Toast.LENGTH_SHORT).show();  
        }  
    };  
    
    private void initButtons() {  
        Button buttonStart = (Button) findViewById(R.id.start_service);  
        buttonStart.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
                startService();  
            }  
        });  
  
        Button buttonStop = (Button) findViewById(R.id.stop_service);  
        buttonStop.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
                stopService();  
            }  
        });  
  
        Button buttonBind = (Button) findViewById(R.id.bind_service);  
        buttonBind.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
                bindService();  
            }  
        });  
  
        Button buttonUnbind = (Button) findViewById(R.id.unbind_service);  
        buttonUnbind.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
                unbindService();  
            }  
        });  
    }  
  
    private void startService() {  
        Intent i = new Intent(this, TestService.class);  
        this.startService(i);  
        
    }  
    
    private void stopService() {  
        Intent i = new Intent(this, TestService.class);  
        this.stopService(i);  
    }  
  
    private void bindService() {  
        Intent i = new Intent(this, TestService.class);  
         bindService(i, _connection, Context.BIND_AUTO_CREATE);  
         _isBound = true;  
    }  
  
    private void unbindService() {  
        if (_isBound) {  
            unbindService(_connection);  
            _isBound = false;  
        }  
    }  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值