安卓绑定本地服务Service并与之通信
2017-02-18 11:30
447 查看
效果
代码
1.主Activity
package com.example.test2.myapplication.service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.example.test2.myapplication.R; /** * Created by Derek on 2017/2/18. * 绑定本地服务Service并与之通信 */ public class BindServiceTest extends AppCompatActivity implements View.OnClickListener { private Button bind; private Button unbind; private Button getservicestatus; BindService.MyBinder binder; ServiceConnection serviceConnection = new ServiceConnection() { //当Activity和service连接成功时,回调该方法 @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.d("GsonUtils", "onServiceConnected"); binder = (BindService.MyBinder) iBinder; } @Override public void onServiceDisconnected(ComponentName componentName) { Log.d("GsonUtils", "onServiceDisconnected"); } }; Intent intent; private TextView textView8; private TextView count; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bindservicetest); initView(); // intent = new Intent(); intent = new Intent(this, BindService.class); intent.setAction("com.example.test2.myapplication.service.Bind_Service"); } private void initView() { bind = (Button) findViewById(R.id.bind); unbind = (Button) findViewById(R.id.unbind); getservicestatus = (Button) findViewById(R.id.getservicestatus); bind.setOnClickListener(this); unbind.setOnClickListener(this); getservicestatus.setOnClickListener(this); textView8 = (TextView) findViewById(R.id.textView8); textView8.setOnClickListener(this); count = (TextView) findViewById(R.id.count); count.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bind: //绑定Service bindService(intent, serviceConnection, BIND_AUTO_CREATE); break; case R.id.unbind: //解除绑定Service unbindService(serviceConnection); break; case R.id.getservicestatus: //不能直接设置int的值 //count.setText(binder.getCount()); //获取并显示Service的count值 count.setText("Service的count值为:" +binder.getCount()); Toast.makeText(this, "Service的count值为:" + binder.getCount(), Toast.LENGTH_SHORT).show(); break; } } }
2.服务
package com.example.test2.myapplication.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; /** * Created by Derek on 2017/2/18. */ public class BindService extends Service { int count; boolean quit; //MyBinder通过继承Binder来实现IBinder类 public class MyBinder extends Binder { public int getCount() { //获取service的运行状态值:count return count; } } //定义onBind方法所返回的对象 private MyBinder binder = new MyBinder(); @Nullable @Override public IBinder onBind(Intent intent) { Log.d("GsonUtils", "onBind"); //返回IBinder对象 return binder; } @Override public void onCreate() { super.onCreate(); //启动一条线程,动态的修改count的状态值 new Thread() { @Override public void run() { super.run(); while (!quit) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } count++; } } }.start(); } @Override public boolean onUnbind(Intent intent) { Log.d("GsonUtils", "onUnbind"); return true; } //service被关闭时 @Override public void onDestroy() { super.onDestroy(); //停止线程任务的执行 this.quit = true; Log.d("GsonUtils", "onDestroy"); } }
布局xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/bind" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="bind"/> <Button android:id="@+id/unbind" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="unbind"/> <Button android:id="@+id/getservicestatus" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="getservicestatus"/> <TextView android:id="@+id/textView8" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Service的count值动态值" android:textSize="26dp"/> <TextView android:textColor="#f00" android:id="@+id/count" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="count" android:textSize="26dp"/> </LinearLayout>
AndroidManifest.xml
<service android:name="com.example.test2.myapplication.service.BindService"> <intent-filter> <action android:name="com.example.test2.myapplication.service.Bind_Service"></action> </intent-filter> </service>
。。。
相关文章推荐
- Android菜鸟的成长笔记(18)——绑定本地Service并与之通信
- Android学习笔记_22_服务Service应用之—与Activity进行相互通信的本地服务
- Android:Service:与Activity进行相互通信的本地服务
- Service学习之路(03)之绑定本地Service并与之通信
- 绑定本地service并与之通信
- Android基础——Service的使用(绑定本地Service并与之通信)
- android后台服务service全解析(上)--service的使用与本地通信
- 绑定本地Service并与之通信
- android菜鸟学习笔记28----Android中的Service生命周期及本地和远程服务绑定的实现
- Android IPC机制绑定Service实现本地通信
- Android菜鸟的成长笔记(18)——绑定本地Service并与之通信
- Android笔记二十八.Service组件入门(二).绑定本地Service并与之通信
- Android菜鸟的成长笔记(18)——绑定本地Service并与之通信
- Service之基础(开启服务、绑定本地服务)
- Android Service之绑定本地服务
- android学习笔记---40_建立能与访问者进行相互通信的本地服务,后台运行的service
- android学习笔记---40_建立能与访问者进行相互通信的本地服务,后台运行的service
- 绑定本地Service并与之通信
- 绑定本地Service并与之通信
- adnroid四大组件之Service(5) 绑定服务,数据通信-IBinder