Android:Service的绑定和解绑定,Service与Activity通信
来源:互联网 发布:学好python要多久 编辑:程序博客网 时间:2024/05/01 19:21
先看一张原理图:
1.先从Activity绑定Service,如果Service还没被创建,则Service会调用onCreate()方法,然后再调用onBind()的的方法;
2.然后onBind()的返回值通过实现了IBinder接口的对象传回给实现了ServiceConnection接口的对象的onServiceConnected()方法;
3.onServiceConnected()方法中同样实例化实现了IBinder接口的对象,并将Service传来的对象接收;
4.尽情的在Activity中 获取消息吧;
接下来,在看一下运行截图:
点击绑定后,在点击显示图片,就会将从Service那边发回来的id值赋给ImageView对象;有时候会发现,点击显示图片后还是同一张,应该是刚好id值相同;
接下来看一下代码:
注释我都写在代码里了~
MainActivity.java:
package com.myproject.wyc.bindservice;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Binder;import android.os.IBinder;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.ImageView;import com.wyc.bean.MyBinder;import com.wyc.service.MyService;public class MainActivity extends ActionBarActivity { private String MyTag = "MyTag"; private Boolean bindTag = false;//设置一个标记,用于标记绑定与否; private ImageView imgObj; private Button btnObj,btn02Obj,btn03Obj; //这两者,用于绑定Service后,接收从Service传回来的对象,并能通过该对象获取需要的值; private MyBinder myBinder; private MyServiceConnection myServiceConnection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myServiceConnection = new MyServiceConnection(); imgObj = (ImageView)findViewById(R.id.imgid); btnObj = (Button)findViewById(R.id.btnid); btn02Obj = (Button)findViewById(R.id.btn02id); btn03Obj = (Button)findViewById(R.id.btn03id); btnObj.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imgObj.setImageResource(myBinder.getRes()); } }); btn02Obj.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, MyService.class); Log.i(MyTag,"intent"); //绑定Service; bindService(intent,myServiceConnection, Service.BIND_AUTO_CREATE); Log.i(MyTag,"bindService"); //设置标记,true代表绑定; bindTag = true; } }); btn03Obj.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //解绑定; unbindService(myServiceConnection); Log.i(MyTag,"unbindService"); //设置标记,false代表解绑定; bindTag = false; } }); } public class MyServiceConnection implements ServiceConnection{ /** * 当Service绑定时候触发onBind()后,放回值会被该方法接收即 实现 IBinder接口类型的类的对象; * Binder实现了IBinder接口; * 因此创建MyBind继承Binder,并添加所需的方法等,达到需求; */ @Override public void onServiceConnected(ComponentName name, IBinder service) { //接收从Service传回的对象,强制转换类型 myBinder = (MyBinder)service; Log.i(MyTag,"mainactivity myBinder.getRes()"+myBinder.getRes()); } @Override public void onServiceDisconnected(ComponentName name) { } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onDestroy() { super.onDestroy(); /** * 由于程序可能在没有解绑定的情况下被退出,而会报错; * 所以我们要做个判断:当退出程序时,时候已经解绑定; * 如果bindTag为true则解绑定,为false则不用; */ if (bindTag) { unbindService(myServiceConnection); } }}
MyBinder.java:
package com.wyc.bean;import android.os.Binder;/** * Created by Administrator on 15-3-12. */public class MyBinder extends Binder { private int res; public int getRes() { return res; } public void setRes(int res) { this.res = res; }}
MyService.java:
package com.wyc.service;import android.app.Service;import android.content.Intent;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.os.SystemClock;import android.util.Log;import com.myproject.wyc.bindservice.R;import com.wyc.bean.MyBinder;/** * Created by Administrator on 15-3-12. */public class MyService extends Service { private String MyTag = "MyTag"; private Handler handler; private Boolean myBoolean = false; //在写了这个数组后,其实我是后悔的,因为我其实这个数组完全可以写在Activity那边, // 然后Service只需要发送索引值过去就好; private int[] draws = new int[]{R.drawable.item8,R.drawable.item9,R.drawable.item10}; @Override public void onCreate() { super.onCreate(); Log.i(MyTag,"onCreate"); //java的线程是无法停止的,所以想要让其处理事务或者停止处理, // 可以通过标志来设定,一般用true和false来标志; myBoolean = true; //Service和Activity一样也不能做耗时处理,写个子线程处理, // 并发送消息给主线程,主线程再将值赋给MyBinder对象, //而MyBinder对象的属性的值又能被Activity获取到; new Thread(new Runnable() { @Override public void run() { int index = 0; Log.i(MyTag,"Thread"); while(myBoolean){ Message msg =Message.obtain(); msg.arg1 = draws[index]; Log.i(MyTag,"arg1 = "+msg.arg1); handler.sendMessage(msg); Log.i(MyTag,"draws[index]="+draws[index]); index = ++index%3;//++i与i++的问题:++i自加再赋值;i++赋值后再自加…… Log.i(MyTag,"draws[index]="+draws[index]); SystemClock.sleep(300); } } }).start(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(MyTag,"onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.i(MyTag,"onDestroy"); myBoolean = false; } @Override public boolean onUnbind(Intent intent) { Log.i(MyTag,"onUnbind"); return super.onUnbind(intent); } @Override public IBinder onBind(Intent intent) { final MyBinder myBinder = new MyBinder(); handler = new Handler(){ //获取消息,并赋值; @Override public void handleMessage(Message msg) { super.handleMessage(msg); int res = msg.arg1; myBinder.setRes(res); } }; return myBinder; }}
activity_main.xml:
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity"> <ImageView android:id="@+id/imgid" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btnid" android:text="显示图片" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" /> <Button android:id="@+id/btn02id" android:text="绑定" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" /> <Button android:id="@+id/btn03id" android:text="解绑定" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" /></LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myproject.wyc.bindservice" > <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.wyc.service.MyService" android:enabled="true" /> </application></manifest>
组后附上资源:
item8.jpg:
item9.jpg:
item10.jpg:
我一直不知道这个博客如何添加附件,否则源码直接发压缩包更好,然后只挑重点的现实在页面。
下面是源码的压缩包,我发在百度网盘;
我使用的是Android studio写的程序;
http://pan.baidu.com/s/1sj9Iy3V
0 0
- Android:Service的绑定和解绑定,Service与Activity通信
- android service 与activity通信--绑定service
- android开发之Activity与Service绑定
- 【Android】Activity与服务Service绑定
- Android Service绑定与跨进程通信
- Activity与Service的启动和绑定
- Service基础Activity与Service绑定
- Android笔记(一):Activity绑定Service,并访问通信
- Android本地Service的启动与绑定
- 绑定Activity和Service
- Activity和Service绑定
- Activity 和Service绑定
- Service 和 Activity 绑定
- Activity 和Service绑定
- Activity 和Service绑定
- 绑定Activity和Service
- Activity绑定service
- Activity 和Service绑定
- python2.7中没有zlib库的解决方案(zipimport.ZipImportError: can't decompress data; zlib not available)
- VS2010 LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 的解决方法[笔记]
- win7 myeclipse2003 删默认密码不成功问题
- Raspberry Pi-2 内存占用过高现象分析
- GridFS实现原理
- Android:Service的绑定和解绑定,Service与Activity通信
- iOS 自动布局 Auto Layout 入门 06 详情页面 (b) 按钮的布局
- 门面模式
- 神经网络学习(一)
- 飞机大战-1
- nginx-gridfs模块的安装使用
- input button jump is not setup解决办法
- django 实例
- 设计是怎样炼成的