Android 服务绑定与数据同步
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 服务绑定与数据同步相关的知识,希望对你有一定的参考价值。
1 package com.example.metrox.l15; 2 3 import android.content.ComponentName; 4 import android.content.Intent; 5 import android.content.ServiceConnection; 6 import android.os.IBinder; 7 import android.provider.Settings; 8 import android.support.v7.app.AppCompatActivity; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.widget.EditText; 12 import android.widget.Switch; 13 14 public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection { 15 Intent intent; 16 EditText et; 17 MyService.Binder binder = null; 18 private boolean isBind = false; 19 private boolean isRuning = false; 20 @Override 21 public void onClick(View view) { 22 switch(view.getId()){ 23 case R.id.btnStartService: 24 intent = new Intent(MainActivity.this,MyService.class); 25 et = (EditText) findViewById(R.id.editText); 26 intent.putExtra("data",et.getText().toString()); 27 startService(intent); 28 isRuning = true; 29 break; 30 case R.id.btnStopService: 31 if(isRuning){ 32 stopService(intent); 33 } 34 break; 35 case R.id.btnBindService: 36 bindService(intent,this,BIND_AUTO_CREATE); 37 isBind = true; 38 break; 39 case R.id.btnUnBindService: 40 if(isBind){ 41 unbindService(this); 42 isBind = !isBind; 43 } 44 break; 45 case R.id.btnSyncData: 46 if(binder != null){ 47 binder.setData(et.getText().toString()); 48 } 49 break; 50 } 51 } 52 53 @Override 54 protected void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 setContentView(R.layout.activity_main); 57 58 findViewById(R.id.btnStartService).setOnClickListener(this); 59 findViewById(R.id.btnStopService).setOnClickListener(this); 60 findViewById(R.id.btnBindService).setOnClickListener(this); 61 findViewById(R.id.btnUnBindService).setOnClickListener(this); 62 findViewById(R.id.btnSyncData).setOnClickListener(this); 63 } 64 65 @Override 66 public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 67 System.out.println("服务已连接..."); 68 binder = (MyService.Binder) iBinder; 69 } 70 71 @Override 72 public void onServiceDisconnected(ComponentName componentName) { 73 System.out.println("服务已断开..."); 74 } 75 }
1 package com.example.metrox.l15; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 8 public class MyService extends Service { 9 private boolean isRun = false; 10 private String data = "default"; 11 public MyService() { 12 } 13 14 @Override 15 public IBinder onBind(Intent intent) { 16 return new Binder(); 17 } 18 19 public class Binder extends android.os.Binder{ 20 public void setData(String data){ 21 MyService.this.data = data; 22 } 23 } 24 25 @Override 26 public int onStartCommand(Intent intent, int flags, int startId) { 27 System.out.println("服务已启动..."); 28 data = intent.getStringExtra("data"); 29 30 return super.onStartCommand(intent, flags, startId); 31 } 32 33 @Override 34 public void onCreate() { 35 super.onCreate(); 36 System.out.println("服务已创建..."); 37 isRun = true; 38 new Thread(){ 39 @Override 40 public void run() { 41 super.run(); 42 while (isRun){ 43 System.out.println(data); 44 try { 45 sleep(1000); 46 } catch (InterruptedException e) { 47 e.printStackTrace(); 48 } 49 } 50 } 51 }.start(); 52 } 53 54 @Override 55 public void onDestroy() { 56 super.onDestroy(); 57 isRun = false; 58 System.out.println("服务已消毁..."); 59 } 60 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.metrox.l15.MainActivity"> 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="Hello World!" 16 android:id="@+id/textView" /> 17 18 <Button 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="停止服务" 22 android:id="@+id/btnStopService" 23 android:layout_marginTop="48dp" 24 android:longClickable="false" 25 android:layout_below="@+id/textView" 26 android:layout_centerHorizontal="true" /> 27 28 <Button 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="启动服务" 32 android:id="@+id/btnStartService" 33 android:layout_alignTop="@+id/btnStopService" 34 android:layout_alignParentLeft="true" 35 android:layout_alignParentStart="true" /> 36 <Button 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:text="解绑服务" 40 android:id="@+id/btnUnBindService" 41 android:longClickable="false" 42 android:layout_alignTop="@+id/btnBindService" 43 android:layout_alignLeft="@+id/btnStopService" 44 android:layout_alignStart="@+id/btnStopService" /> 45 46 <Button 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:text="绑定服务" 50 android:id="@+id/btnBindService" 51 android:layout_centerVertical="true" 52 android:layout_alignParentLeft="true" 53 android:layout_alignParentStart="true" /> 54 55 <EditText 56 android:layout_width="match_parent" 57 android:layout_height="wrap_content" 58 android:id="@+id/editText" 59 android:layout_above="@+id/btnStartService" 60 android:layout_alignParentLeft="true" 61 android:layout_alignParentStart="true" 62 android:text="默认数据" /> 63 64 <Button 65 android:layout_width="wrap_content" 66 android:layout_height="wrap_content" 67 android:text="同步数据" 68 android:id="@+id/btnSyncData" 69 android:layout_alignParentBottom="true" 70 android:layout_alignParentLeft="true" 71 android:layout_alignParentStart="true" 72 android:layout_marginBottom="74dp" /> 73 </RelativeLayout>
以上是关于Android 服务绑定与数据同步的主要内容,如果未能解决你的问题,请参考以下文章
如何用 Android 数据绑定替换 androidx.fragment.app.FragmentContainerView 中的片段