android service 的交互

just posted @ 2011年9月11日 10:21 in android , 2399 阅读

这个例子演示了activity调用本地服务,点击按钮就会显示结果 

源码下载

 

结果:

步骤如下:

1 创建服务接口

服务接口提供了可以调用的方法

IService.java

 

package com.demo.service;

public interface IService {
	public String getName();
}

2 实现服务

myService.java

 

package com.demo.service;

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

public class myService extends Service {

	/*
	 * binder 起到个桥梁的作用,
	 */
	private MyServiceBinder myServiceBinder = new MyServiceBinder();  
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return myServiceBinder;
	}

	public String getName(){
		return "你好啊 返回的结果";
	}
	
	public class MyServiceBinder extends Binder implements IService{

		public String getName() {
			// TODO Auto-generated method stub
			return myService.this.getName();
		}
		
	}
}

3 调用服务的activity

mainActivity.java

 

package com.demo.service;

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

/*
 * service 的交互
 */
public class mainActivity extends Activity {
    /** Called when the activity is first created. */
	
	private myConn conn=new myConn();
	private IService serviceInstance;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Intent intent = new Intent(this, myService.class);
        /*
         * 激活服务 会调用 myConn 返回桥梁 serviceImpl
         */
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
        
         final TextView nameTextView = (TextView) findViewById(R.id.name);
        this.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				//点击时调用服务中的方法
				String nameString = serviceInstance.getName();
				nameTextView.setText(nameString);
			}
		});
        
    }
    
    private final class myConn implements ServiceConnection{

    	/*
    	 * (non-Javadoc)
    	 * @see android.content.ServiceConnection#onServiceConnected(android.content.ComponentName, android.os.IBinder)
    	 * 这个IBinder 是 myServece中的返回的IBinder
    	 */
		public void onServiceConnected(ComponentName name, IBinder service) {
			serviceInstance = (IService) service;
			
		}

		public void onServiceDisconnected(ComponentName name) {
			serviceInstance=null;
			
		}
    	
    }
    
    @Override
    protected void onDestroy() {
    	//退出时 解除绑定
    	unbindService(conn);
    	super.onDestroy();
    }
}

 

 

  • 无匹配
  • 无匹配
light novel 说:
2019年1月04日 01:55

I enjoyed over read your blog post. This was actually what i was looking for and i am glad to came here!

HREX Haryana 说:
2022年11月09日 18:48

The Indian Government initiated the Employment Exchange programs. Through the Employment Exchange Act 1959. The program has over the year’s assisted unemployed youth in a different state in India. HREX Haryana The program also helps in providing information about various vacancies in specific departments. The Employment Exchanged focused on all unemployed youth, especially from lowly communities.

JIO Switch App 说:
2022年12月19日 20:02

Jio Switch is created. A simple interface and the absence of advertisements are two positive aspects of the Jio application for India. However, the product does not include a cloud-based or on-premises alternative. JIO Switch App JioSwitch, however, allows users to transfer or request large files with others and is quite simple to install and administer.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter