您的位置:首页 > 移动开发 > Android开发

android,bindService实例

2016-03-29 00:51 417 查看
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.activity_main);
flag = false;
startBtn = (Button)this.findViewById(R.id.startBtn);
stopBtn = (Button)this.findViewById(R.id.stopBtn);
startBtn.setOnClickListener(listener);
stopBtn.setOnClickListener(listener);
}////onCreate

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);
//  -----  boolean bindService(Intent service, ServiceConnection conn, int flags)
//         Connect to an application service, creating it if needed.
//   ---- conn - Receives information as the service is started and stopped.
//        flags - Operation options for the binding. May be 0 or Context.BIND_AUTO_CREATE.
//   ----- 第3个参数Context.BIND_AUTO_CREATE表明只要绑定存在,就自动建立
//        Service;同时也告知Android系统,这个Service的重要程度与调用者相同,
//        除非考虑终止调用者,否则不要关闭这个Service
Log.i(TAG, " bindService111111111------bindService(intent, conn, Context.BIND_AUTO_CREATE);");
}

private void unBind(){
if(flag == true){
Log.i(TAG, "unbindService111111--------BindService-->unBind()");
unbindService(conn);
flag = false;
}
}

private ServiceConnection conn = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.i(TAG, "unbindService(conn)--onServiceDisconnected--");/////单击   stop按钮未执行
//        	需注意的是,unbindService()方法成功后,系统并不会调用
//        	onServiceDisconnected(),因为onServiceDisconnected()仅在意外断开
//        	绑定时才被调用
}
// log.i(TAG, " bindService-----IBinder service");
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.i(TAG, " bindService33333333-----bindService--onServiceConnected");
MyBinder binder = (MyBinder)service;
BindService bindService = binder.getService();
bindService.MyMethod();
Log.i(TAG, " bindService66666-----bindService--onServiceConnected");
flag = true;
}
};

}

/*
*
* 没创建时绑定  执行BindService 1 2 3 4 5 6
* 已创建时绑定  执行BindService  1
*
* 绑定后解绑定 执行 unBindService 1
*
*
*
*
*/
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 < 10; i++) {
Log.i(TAG, " bindService5555555-----BindService-->MyMethod()");
}
}//MyMethod

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub

Log.i(TAG, " bindService222222----public IBinder onBind(Intent arg0)");

return myBinder;
}///onBind

public class MyBinder extends Binder {

public BindService getService() {
Log.i(TAG, " bindService444444----public BindService getService()");
return BindService.this;
}
}///MyBinder

private MyBinder myBinder = new MyBinder();

}// class BindService
<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"
tools:context="${relativePackage}.${activityClass}" >

<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Start" />

<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/startBtn"
android:layout_below="@+id/startBtn"
android:layout_marginTop="46dp"
android:text="Stop" />

</RelativeLayout>
<?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="15"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android bindService