编写绑定的 Service服务
来源:互联网 发布:射频信号测试软件 编辑:程序博客网 时间:2024/05/18 01:11
编写绑定的 Service服务
MainActivity
package annoy.service_test;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 MainActivity extends AppCompatActivity { Button button1, button2, button3; BindService.MyBinder binder; // 定义一个连接ServiceConnection对象 private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { System.out.println("--Service Conneciton"); // 获取Service的onBinder方法所返回的MyBinder对象 binder = (BindService.MyBinder) iBinder; } @Override public void onServiceDisconnected(ComponentName componentName) { System.out.println("--Service Disconnected--"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取界面中的3个按钮 button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); // 创建启动Service的Intent final Intent intent = new Intent(this, BindService.class); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 绑定指定Service bindService(intent, conn, Service.BIND_AUTO_CREATE); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 解除绑定 unbindService(conn); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 获取并显示Service的Count值 Toast.makeText(MainActivity.this, "Service的count值为:" + binder.getCount(), Toast.LENGTH_SHORT).show(); } }); }}
Service编写
BindService
package annoy.service_test;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class BindService extends Service { private int count = 0; private boolean quite = false; // 定义onBinder方法返回的对象 private MyBinder binder = new MyBinder(); // 继承Binder来实现IBunder public class MyBinder extends Binder { public int getCount() { return count; } } public BindService() { } @Override public IBinder onBind(Intent intent) { // throw new UnsupportedOperationException("Not yet implemented"); System.out.println("Service is Binded"); return binder; } @Override public void onCreate() { super.onCreate(); System.out.println("Service is Create"); // 启动一条线程 new Thread() { @Override public void run() { while (!quite) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } count++; } } }.start(); } @Override public boolean onUnbind(Intent intent) { System.out.println("Service is Unbind"); return true; } @Override public void onDestroy() { super.onDestroy(); this.quite = true; System.out.println("Service is Destroyed"); }}
0 0
- 编写绑定的 Service服务
- Android---服务(Service)的绑定服务
- 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景
- Service的使用(三)绑定服务
- Service服务的开启与绑定
- service(服务绑定)
- service(服务绑定)
- Service绑定本地服务
- Service绑定服务
- Service 02(绑定服务)
- Service中绑定服务和启动服务的区别
- Service-boundService绑定服务(3)
- Android中的Service服务绑定
- Android service(二)绑定服务
- Android -- service之绑定服务
- Service学习之--------绑定服务
- Service中的绑定服务总结
- Service服务(非绑定与绑定)
- 【设计和开发一套简单自动化UI框架】
- 3D反射理论概述
- jfreechart 折线图
- 快速修改navigationitem颜色方法
- 性能调优工具nmon
- 编写绑定的 Service服务
- MySql sum(A+B)和sum(A)+sum(B)的区别
- get方式传参中文乱码
- LIGHTING IN VERTEX AND FRAGMENT SHADER
- centos7 关闭firewall防火墙指令以及更换安装iptables并配置
- PHP->iNews新闻发布系统目录结构2
- Shader Forge on the Asset Store
- TinyReplaySystem回放系统设计和开发
- Error:Unable to tunnel through proxy. Proxyreturns "HTTP/1.1 400 Bad Request"