Service

本文介绍了一个简单的Android Service绑定示例,通过一个可绑定的服务组件BindService,演示了如何实现服务与活动之间的交互,包括绑定服务、获取服务数据及解除绑定的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Service的第一个例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/bindbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="绑定Service" />

    <Button
        android:id="@+id/unbindbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="解除绑定" />

    <Button
        android:id="@+id/getcount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取数据" />

</LinearLayout>
 <service   android:name=".BindService">
           <intent-filter >
               <action android:name="com.example.android_service01.BINDSERVICE"/>
           </intent-filter> 
        </service>


package com.example.android_service01;

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

public class BindService extends Service {

	private int count;
	private boolean quit;
	private MyBinder myBinder = new MyBinder();

	public class MyBinder extends Binder {
		public int getCount() {
			return count;
		}
	}

	public BindService() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		System.out.println("--Service is binded!");
		return myBinder;
	}

	public void onCreate() {
		super.onCreate();
		System.out.println("--Service is created");
		new Thread() {
			public void run() {
				while (!quit) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					count++;
				}
			}
		}.start();
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("--Service is unBinded!");
		return super.onUnbind(intent);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		System.out.println("--Service is onDestroy!");
	}
}

package com.example.android_service01;


import com.example.android_service01.BindService.MyBinder;


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


public class MainActivity extends Activity {


	private Button button1;
	private Button button2;
	private Button button3;
	private BindService.MyBinder myBinder;
	
	private ServiceConnection conn=new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			System.out.println("--onServiceDisconnected");
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			System.out.println("--onServiceConnected");
			myBinder=(MyBinder) service;
		}
	};
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) findViewById(R.id.bindbutton);
		button2 = (Button) findViewById(R.id.unbindbutton);
		button3 = (Button) findViewById(R.id.getcount);
		
		final Intent intent=new Intent();
		intent.setAction("com.example.android_service01.BINDSERVICE");
		button1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				bindService(intent, conn, Service.BIND_AUTO_CREATE);
			}
		});
		button2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				unbindService(conn);
			}
		});
		button3.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "获取count值:"+myBinder.getCount(), Toast.LENGTH_SHORT).show();
			}
		});


	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值