Android 服务绑定与数据同步
2016-06-14 23:57
441 查看
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/8d4a7742796751c2b8df75b6d7944a75.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/536e518f3a360eb951ceee329335cc14.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/30/4331ec0959bea3b02ccccd940b025689.png)
package com.example.metrox.l15; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.provider.Settings; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Switch; public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection { Intent intent; EditText et; MyService.Binder binder = null; private boolean isBind = false; private boolean isRuning = false; @Override public void onClick(View view) { switch(view.getId()){ case R.id.btnStartService: intent = new Intent(MainActivity.this,MyService.class); et = (EditText) findViewById(R.id.editText); intent.putExtra("data",et.getText().toString()); startService(intent); isRuning = true; break; case R.id.btnStopService: if(isRuning){ stopService(intent); } break; case R.id.btnBindService: bindService(intent,this,BIND_AUTO_CREATE); isBind = true; break; case R.id.btnUnBindService: if(isBind){ unbindService(this); isBind = !isBind; } break; case R.id.btnSyncData: if(binder != null){ binder.setData(et.getText().toString()); } break; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnStartService).setOnClickListener(this); findViewById(R.id.btnStopService).setOnClickListener(this); findViewById(R.id.btnBindService).setOnClickListener(this); findViewById(R.id.btnUnBindService).setOnClickListener(this); findViewById(R.id.btnSyncData).setOnClickListener(this); } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { System.out.println("服务已连接..."); binder = (MyService.Binder) iBinder; } @Override public void onServiceDisconnected(ComponentName componentName) { System.out.println("服务已断开..."); } }
package com.example.metrox.l15; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class MyService extends Service { private boolean isRun = false; private String data = "default"; public MyService() { } @Override public IBinder onBind(Intent intent) { return new Binder(); } public class Binder extends android.os.Binder{ public void setData(String data){ MyService.this.data = data; } } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("服务已启动..."); data = intent.getStringExtra("data"); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); System.out.println("服务已创建..."); isRun = true; new Thread(){ @Override public void run() { super.run(); while (isRun){ System.out.println(data); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } @Override public void onDestroy() { super.onDestroy(); isRun = false; System.out.println("服务已消毁..."); } }
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.metrox.l15.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止服务" android:id="@+id/btnStopService" android:layout_marginTop="48dp" android:longClickable="false" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动服务" android:id="@+id/btnStartService" android:layout_alignTop="@+id/btnStopService" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解绑服务" android:id="@+id/btnUnBindService" android:longClickable="false" android:layout_alignTop="@+id/btnBindService" android:layout_alignLeft="@+id/btnStopService" android:layout_alignStart="@+id/btnStopService" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定服务" android:id="@+id/btnBindService" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_above="@+id/btnStartService" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:text="默认数据" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="同步数据" android:id="@+id/btnSyncData" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginBottom="74dp" /> </RelativeLayout>
相关文章推荐
- Fragment Home键返回桌面再打开程序,无法开启
- android发送邮件
- Android Intent意图的常用使用方法
- android控件按下时出现黄色背景
- Android ORMLite 框架的入门用法
- 一个老鸟发的公司内部整理的 Android 学习路线图 Markdown 版本
- Android的接口回调
- android资料网址多媒体播放,编解码。
- Android性能优化之布局优化
- TextView的一些应用(Textview 自定义字体、显示多种颜色、添加阴影)
- android——数据存储SQLite
- Android数据库之ormlite简单使用
- 避免Android中Context引起的内存泄露
- 【转】Android - Button(按钮)的响应点击事件的4种写法
- Android中ShapeDrawable资源的用法
- Android 从具体实例分析Bitmap使用时候内存注意点
- 【转】Android开发20——单个监听器监听多个按钮点击事件
- 跨进程数据共享问题及解决方案
- ADB not responding. If you'd like to retry, then please manually kill "adb.e处理 Android studio2.1
- (OK)(OK) running two Android-x86 in VirtualBox, they connect to NS3(MANETs) via "ethernet bridge"