bindservice使用例子

MainActivity.java如下

package com.example.bindservice;

import com.example.bindservice.BindService.MyBinder;

import android.os.Bundle;

import android.os.IBinder;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.ServiceConnection;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity {

private Button startBtn;

private Button stopBtn;

private boolean flag;

private static final String TAG = "BindService";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

flag = false;

startBtn = (Button)this.findViewById(R.id.startBtn);

stopBtn = (Button)this.findViewById(R.id.stopBtn);

startBtn.setOnClickListener(listener);

stopBtn.setOnClickListener(listener);

}

private OnClickListener listener = new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()) {

case R.id.startBtn:

bindService();

break;

case R.id.stopBtn:

unBind();

break;

default:

break;

}

}

};

private void bindService(){

Intent intent = new Intent(MainActivity.this,BindService.class);

bindService(intent, conn, Context.BIND_AUTO_CREATE);

}

private void unBind(){

if(flag == true){

Log.i(TAG, "BindService-->unBind()");

unbindService(conn);

flag = false;

}

}

private ServiceConnection conn = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

// TODO Auto-generated method stub

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

// TODO Auto-generated method stub

MyBinder binder = (MyBinder)service;

BindService bindService = binder.getService();

bindService.MyMethod();

flag = true;

}

};

@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;

}

}

BindService.java如下

package com.example.bindservice;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

public class BindService extends Service {

private static final String TAG = "BindService";

public void MyMethod(){

for(int i = 0; i < 100; i++)

{

Log.i(TAG, "BindService-->MyMethod()");

}

}

@Override

public IBinder onBind(Intent arg0) {

// TODO Auto-generated method stub

return myBinder;

}

public class MyBinder extends Binder{

public BindService getService(){

return BindService.this;

}

}

private MyBinder myBinder = new MyBinder();

}

strings.xml如下

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">BindService</string>

<string name="action_settings">Settings</string>

<string name="hello_world">Hello world!</string>

<string name="stopBtn">stopBtn</string>

<string name="startBtn">startBtn</string>

</resources>

main.xml如下

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

<Button

android:id="@+id/startBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/textView1"

android:layout_below="@+id/textView1"

android:layout_marginTop="18dp"

android:text="@string/startBtn" />

<Button

android:id="@+id/stopBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignRight="@+id/startBtn"

android:layout_below="@+id/startBtn"

android:layout_marginTop="29dp"

android:text="@string/stopBtn" />

</RelativeLayout>

AndroidManifest.xml如下

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.bindservice"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="17" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.example.bindservice.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<service android:name="com.example.bindservice.BindService"></service>

</application>

</manifest>

时间: 2024-08-25 01:10:36

bindservice使用例子的相关文章

小猪的Android入门之路 Day 9 part 1

小猪的Android入门之路 Day 9 part 1 Android四大组件之--Service浅析 --转账请注明出处:coder-pig 本节引言: 在前面的学习中我们已经把安卓四个基本组件中的两个: Actvity(活动)和BroadCastReceiver过了一遍,而在Day 9中我们会对第三个组件Service进行 解析,两种类型的Service,Service的生命周期,如何去使用Service,声明Service,调用,停止Service; 跨进程调用AIDL,以及常用的系统服务

Android Service的两种启动方式

参考链接 1. 概念 开始,先稍稍讲一点android中Service的概念和用途吧~ Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外Local服务因为是在同一进程因此不需要IPC, 也不需要AIDL.相应bindService会方便很多.主进程被Kill后,服务便会终止. 2.远程服务为独立的进程,对应进程名格式为所在包名加上你指定的android:process字符

Android Service组件在新进程绑定(bindService)过程

1.首先看两个例子 (1)进程内 Client端 public class CounterService extends Service implements ICounterService { ...... public class CounterBinder extends Binder { public CounterService getService() { return CounterService.this; } } ...... } Server端 public class Ma

Android中Service的一个Demo例子

Android中Service的一个Demo例子  Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding.  本文,主要贴代码,不对Service做过多讲解.  代码是从网上找的一个例子,Copy下来发现代码不完全正确,稍微修改了下.  AndroidManifest.xml <application android:icon="@drawable/ic_launcher" android:label="@stri

Android Bound Service(一) ----- Extending Binder Service(进程内绑定Service的简单例子)

ref:http://developer.android.com/guide/components/bound-services.html? 前言 重新学习这一项技术,主要的原因,是因为以前没有好好的学,那时总觉得作品能动,能完成工作就好了,而这种得过且过的想法,大大地影响了我的技术程度,也因此,在这个这个博客里,有许多的复习心得.有幸得到一位前辈的指导,指出,若只是学习,而无实际应用,这样进步会较少,因此,最好要多看源码,并且自己多尝试多实践,这样学习一万小时,应该能有小进步,因此开始了 Bo

startService与bindService混合使用对Service生命周期的影响

FBI Warning:欢迎转载,但请标明出处:http://blog.csdn.net/codezjx/article/details/45314925,未经本人同意请勿用于商业用途,感谢支持! 项目开发中有遇到startService与bindService混合使用的情况,发现其对Service生命周期有很大影响,故与各位分享一下... 一.正常情况(应该大家都很熟了,简单介绍): (1)单独使用startService(): onCreate()->onStartCommand()->S

Android Studio 导入OpenCV 并调试运行face-detection例子

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); line-height: 120%; text-align: justify } p.western { font-family: "Calibri", serif; font-size: 10pt } p.cjk { font-family: "Droid Sans Fallback"; font-size: 10pt } p.ctl {

Android中bindService的使用及Service生命周期

Android中有两种主要方式使用Service,通过调用Context的startService方法或调用Context的bindService方法,本文只探讨纯bindService的使用,不涉及任何startService方法调用的情况.如果想了解startService相关的使用,请参见<Android中startService的使用及Service生命周期>. bindService启动服务的特点 相比于用startService启动的Service,bindService启动的服务

Android AIDL例子

为使应用程序之间能够彼此通信,Android提供了IPC (Inter Process Communication,进程间通信)的一种独特实现: AIDL (Android Interface Definition Language, Android接口定义语言). 建立两个Android项目,一个是client,一个是server(提供service). 这篇文章将通过一个项目来介绍AIDL用法,包含了service和client.可能简单了些,不过轻省许多. 本文提供了一个关于AIDL使用的