Service组件
后台运行的程序
要在AndroidManifest.xml里面配置
<service android:name=".EchoService"></service>
MainActivity添加代码
Intent serviceIntent = new Intent(this,EchoService.class);
startService(serviceIntent);
----------------------
onBind 返回IBinder对象,不为null才能connect成功
同时执行Unbind ,Stop 才能关闭service
Bind Service后退,执行onDestroy
Start Service后退,不执行onDestroy
Service之间通信要先绑定
后台运行的程序
要在AndroidManifest.xml里面配置
<service android:name=".EchoService"></service>
MainActivity添加代码
Intent serviceIntent = new Intent(this,EchoService.class);
startService(serviceIntent);
----------------------
onBind 返回IBinder对象,不为null才能connect成功
同时执行Unbind ,Stop 才能关闭service
Bind Service后退,执行onDestroy
Start Service后退,不执行onDestroy
Service之间通信要先绑定
service 是系统组建,由操作系统控制,只能通过bindService请求操作系统,自己不能create
程序实现了启动服务,每隔一秒输出一个数字。可以start,stop,bind,Unbind,通过logcat查看
MainActivity
package com.sammer.l003usingservice; import android.content.ComponentName; import android.content.Context; 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; public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection { private Button btnStartService,btnStopService,btnBindService,btnUnbindService,btnGetCurrentNum; private Intent serviceIntent; private EchoService echoService = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent = new Intent(this,EchoService.class); btnStartService = (Button) findViewById(R.id.btnStartService); btnStopService = (Button) findViewById(R.id.btnStopService); btnBindService = (Button) findViewById(R.id.btnBindService); btnUnbindService = (Button) findViewById(R.id.btnUnbindService); btnGetCurrentNum = (Button) findViewById(R.id.btnGetCurrentNum); btnStartService.setOnClickListener(this); btnStopService.setOnClickListener(this); btnBindService.setOnClickListener(this); btnUnbindService.setOnClickListener(this); btnGetCurrentNum.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btnStartService: startService(serviceIntent); break; case R.id.btnStopService: stopService(serviceIntent); break; case R.id.btnBindService: bindService(serviceIntent,this, Context.BIND_AUTO_CREATE); break; case R.id.btnUnbindService: unbindService(this); echoService = null; break; case R.id.btnGetCurrentNum: if(echoService != null) { System.out.println("当前服务中数字时:"+echoService.getCurrentNum()); } break; } } @Override public void onServiceConnected(ComponentName componentName, IBinder binder) { System.out.println("onServiceConnected"); //获取当前服务实例 echoService = ((EchoService.EchoServiceBinder) binder).getService(); } @Override public void onServiceDisconnected(ComponentName componentName) { } }
EchoService.java
package com.sammer.l003usingservice; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import java.util.Timer; import java.util.TimerTask; /** * Created by Administrator on 2017/8/9. * Weidong Wang 2017 */ public class EchoService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { System.out.println("onBind"); return echoServiceBinder; } //自定义一个内部类EchoServiceBinder继承Binder,必须有返回值才能connect private final EchoServiceBinder echoServiceBinder = new EchoServiceBinder(); public class EchoServiceBinder extends Binder { public EchoService getService(){ return EchoService.this; } } public int getCurrentNum(){ return i; } @Override public void onCreate() { System.out.println("onCreate"); startTimer(); super.onCreate(); } @Override public void onDestroy() { System.out.println("onDestroy"); stopTimer(); super.onDestroy(); } private int i = 0; public void startTimer(){ if(timer == null){ timer = new Timer(); task = new TimerTask(){ @Override public void run() { i++; System.out.println(i); } }; timer.schedule(task,1000,1000); } } public void stopTimer(){ if(timer != null){ task.cancel(); timer.cancel(); task = null; timer = null; } } private Timer timer; private TimerTask task; }AndroidManifest.xml添加
activity_main</activity> <service android:name=".EchoService"></service> </application>
<Button android:id="@+id/btnStartService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Service" /> <Button android:id="@+id/btnStopService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop Service" /> <Button android:id="@+id/btnBindService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bind Service" /> <Button android:id="@+id/btnUnbindService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Unbind Service" /> <Button android:id="@+id/btnGetCurrentNum" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Get Current Number" />