在Service里面装数据,从Activity里面用serviceConnection取数据
xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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.chenshuai.myapplication.ActivityService" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="测试普通服务" android:textSize="30sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="一般启动服务" android:onClick="yibanqdonclick"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="一般停止服务" android:onClick="yibantzonclick" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="测试绑定服务" android:textSize="30sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="绑定启动服务" android:onClick="bangdingqdonclick"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="读取服务" android:onClick="duqusjonclick"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="绑定停止服务" android:onClick="bangdingtzonclick" /> </LinearLayout> </LinearLayout>
Service
package com.example.chenshuai.myapplication; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class MyService_1 extends Service { public MyService_1() { //构造方法里测试 Log.e("TAG","构造Service"); } int i = 0; Boolean aBoolean = true; //自定义类,继承Binder,装数据 public class Mybinder extends Binder { public int getText() { return i; } } //绑定的回调方法 //必须要重写 @Override public IBinder onBind(Intent intent) { //绑定方法 // TODO: Return the communication channel to the service. // throw new UnsupportedOperationException("Not yet implemented"); Log.e("TAG","绑定并调用"); //return new Binder(); //启动子线程,改变i的值 new Thread() { public void run() { while (aBoolean) { try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } i++; } } }.start(); return new Mybinder(); } @Override public void onCreate() { //创建方法 Log.e("TAG","创建Service"); super.onCreate(); } @Override public void onDestroy() { //销毁方法 Log.e("TAG","销毁Service"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // Log.e("TAG","启动命令Command"); return super.onStartCommand(intent, flags, startId); } //解绑时候调用 @Override public boolean onUnbind(Intent intent) { Log.e("TAG","解除绑定"); //停止循环 aBoolean = false; return super.onUnbind(intent); } }
java
package com.example.chenshuai.myapplication; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Toast; public class ActivityService extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity_service); } //普通方式启动Service //参考Activity的启动方式 public void yibanqdonclick(View view) { //通过意图 //显示意图 Intent intent = new Intent(this,MyService_1.class); startService(intent); Toast.makeText(ActivityService.this, "启动Service", Toast.LENGTH_SHORT).show(); } public void yibantzonclick(View view) { //通过意图 //显示意图 Intent intent = new Intent(this,MyService_1.class); stopService(intent); Toast.makeText(ActivityService.this, "启动Service", Toast.LENGTH_SHORT).show(); } ServiceConnection serviceConnection; MyService_1.Mybinder mybinder; public void bangdingqdonclick(View view) { Intent intent = new Intent(this,MyService_1.class); //实现ServiceConnection接口 if (serviceConnection == null) { serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("TAG", "连接服务"); //接收数据 Mybinder mybinder = (MyService_1.Mybinder)service; } //异常断开才会调用 @Override public void onServiceDisconnected(ComponentName name) { Log.e("TAG", "断开连接"); } }; } //参数:意图,连接,连接方式 BIND_AUTO_CREATE自动创建 bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE); Toast.makeText(ActivityService.this, "连接服务成功", Toast.LENGTH_SHORT).show(); } public void bangdingtzonclick(View view) { //判断是否已经绑定,连接是否为空 if (serviceConnection != null) { //解除绑定 unbindService(serviceConnection); serviceConnection = null; Toast.makeText(ActivityService.this, "解除绑定", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(ActivityService.this, "尚未绑定", Toast.LENGTH_SHORT).show(); } } //读取服务数据 public void duqusjonclick(View view) { //调用服务对象的方法 if (mybinder != null) { Toast.makeText(ActivityService.this, "读取的信息是:"+mybinder.getText(), Toast.LENGTH_SHORT).show(); } } //重写回调方法,防止强制退出时,绑定服务还在运行 @Override protected void onDestroy() { if (serviceConnection != null) { //解除绑定 unbindService(serviceConnection); serviceConnection = null; } super.onDestroy(); } }
时间: 2024-10-21 14:15:01