Android Service(绑定BindService)
来源:互联网 发布:ubuntu skype安装包 编辑:程序博客网 时间:2024/05/16 05:41
<service android:name=".BindService"/>
BindService.java
package shortcut.song.com.myapplication;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.support.annotation.Nullable;/** * Created by Administrator on 2017/8/14 0014. */public class BindService extends Service { private int count; private boolean quit; // 定义onBind方法返回的对象 private MyBinder myBinder = new MyBinder(); public class MyBinder extends Binder { public int getCount() { // 获取Service的运行状态:count return count; } } // 必须实现的方法,绑定Service时回调该方法 @Nullable @Override public IBinder onBind(Intent intent) { System.out.println("Service is Binded"); return myBinder; } // Service创建时回调该方法 @Override public void onCreate() { super.onCreate(); System.out.println("Service is onCreate"); new Thread(){ @Override public void run() { super.run(); while (!quit) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } count++; } } }.start(); } // Service 被断开连接时回调该方法 @Override public boolean onUnbind(Intent intent) { System.out.println("Service is onUnbind"); return true; } // Service 被关闭之前回调该方法 @Override public void onDestroy() { super.onDestroy(); this.quit = true; System.out.println("Service is onDestroy"); }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="shortcut.song.com.myapplication.BindServiceActivity"> <Button android:id="@+id/bind_service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="onBind"/> <Button android:id="@+id/unbind_service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="unBind"/> <Button android:id="@+id/get_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Status"/></LinearLayout>
package shortcut.song.com.myapplication;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class BindServiceActivity extends AppCompatActivity { Button bind,unbind,getStatus; // 保持所启动的Service的IBinder对象 BindService.MyBinder myBinder; // 定义一个ServiceConnection对象 private ServiceConnection conn = new ServiceConnection() { // 当该Activity与Service连接成功时回调该方法 @Override public void onServiceConnected(ComponentName name, IBinder service) { System.out.println("--Service connected--"); // 获取Service的onBind方法所返回的MyBinder对象; myBinder = (BindService.MyBinder)service; } // 当该Activity与Service断开连接时回调该方法 @Override public void onServiceDisconnected(ComponentName name) { System.out.println("--Service Disconnected--"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bind_service); bind = (Button)findViewById(R.id.bind_service); unbind = (Button)findViewById(R.id.unbind_service); getStatus = (Button)findViewById(R.id.get_status); // 创建启动Service的Intent final Intent intent = new Intent(this, BindService.class); bind.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 绑定指定Service bindService(intent, conn, Service.BIND_AUTO_CREATE); } }); unbind.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 解除绑定Service unbindService(conn); } }); getStatus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取并显示Service的count 值 Toast.makeText(BindServiceActivity.this, "Servicer 的值为:"+myBinder.getCount(), Toast.LENGTH_SHORT).show(); } }); }}
阅读全文
0 0
- Android Service(绑定BindService)
- android service中bindService是异步绑定
- 绑定Service bindService
- Android Service组件在进程内绑定(bindService)过程
- Android Service组件在新进程绑定(bindService)过程
- Android Service之bindService
- android 之 Service(Context BindService())
- Android SERVICE(详解二 Bindservice)
- Android中Service使用bindService
- android Service---bindService和remoteService
- Android Service:bindService与Remote Service
- Android Service的总结,service, bindservice,intentservice
- Android中bindService的细节之一:从进程的角度分析绑定Service的流程【Service所在进程首次启动】
- android开发教程:如何调用bindService绑定?
- Android Service中的startService和bindService
- Android Service之 startService与bindService
- android service bindService onServiceConnected没有调用
- Android组件之Service之startService、bindService
- SQLyog查询时中文显示为问号
- 最小费用最大流(Bellman-Ford找增广路)
- Eclipse插件springsource-tool-suite的下载和安装
- Robotframework之下拉列表select
- 不使用判断,找出两个数中的较大者
- Android Service(绑定BindService)
- 快速排序
- 多个版本的python同机安装模块
- eclipse 安装python后pydev不出现
- bzoj4596&&JZOJ5271&&5043保持平衡 贪心+堆(经典思想)
- maven 教程
- spring boot 异常处理全局拦截
- 机房收费系统的准备工作
- 排序算法学习:快速的桶排序