初识 Service(二) 演示: Activity 调用Service的接口

先看代码:

import com.gdp2852.demo.service.BackgroundService.MyServiceBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

public class Demo_ServiceActivity extends Activity {
	public final static String Tag = "Demo_Service";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        findViewById(R.id.bindBtn).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bindService(new Intent(Demo_ServiceActivity.this,BackgroundService.class)
					,srvConn,BIND_AUTO_CREATE);
			}
		});
        
        findViewById(R.id.unbindBtn).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {				
				unbindService(srvConn);
			}
		});
    }
        
    ServiceConnection srvConn = new ServiceConnection() {    	    
    	public void onServiceConnected(ComponentName name, IBinder service) {
			Log.d(Tag, "onServiceConnected "+name);
			MyServiceBinder binder = (MyServiceBinder) service;
			binder.doLog(" Demo_ServiceActivity_CONTENT");
		}
    	
    	public void onServiceDisconnected(ComponentName name) {
    		//在unbind和stop service都没有调用此函数
			Log.d(Tag, "onServiceDisconnected:"+name);
		}
		
	};
}

Service的代码:

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 BackgroundService extends Service {
	private MyServiceBinder binder = new MyServiceBinder();
	private NotificationManager notificationMrg;
	
	public void onCreate() {
		super.onCreate();
		notificationMrg = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		displayNotificationMessage("starting Background Service");
		doInBackground();
	}
	
	private void doInBackground() {
		new Thread(new Runnable() {
			public void run() {
				//TODO do what you wanna to do
			}
		}).start();
	}
	
	public void onDestroy() {
		displayNotificationMessage("stopping Background Service");
		super.onDestroy();
	}
	
	private void displayNotificationMessage(String message) {
		Notification notification = new Notification(R.drawable.sina, message,
				System.currentTimeMillis());
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, Demo_ServiceActivity.class), 0);
		notification.setLatestEventInfo(this, "Background Service", message,
				contentIntent);
		notificationMrg.notify(R.id.app_notification_id, notification);
	}
	
	public IBinder onBind(Intent intent) {
		return binder;
	}
	
	class MyServiceBinder extends Binder{
		void doLog(String logContent){
			Log.d(Demo_ServiceActivity.Tag, "invoke MyServiceBinder doLog:"+logContent);
		}
	}
}
ServiceConnection srvConn = new ServiceConnection() {    	    
    	public void onServiceConnected
在ServiceConnection连接的时会,可以从onServiceConnected()中拿到IBinder引用,这样就可以从通过IBinder调用Service的方法了

另外,需注意:

1 如果onBinder返回的是null, 则不会调用onServiceConnected()

2 unbindService()却不会调用onServiceDisconnected接口. unbindService只是告诉系统,你的APP已经跟这个service没什么关系了,

  系统有权回收这个Service。


工程代码下载: http://download.csdn.net/detail/gdp2852/3783686

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值