service:四大组件之一,和activity是同一级别的组件。但是和activity不一样的是,activity是可以展现给用户看的,而service呢,它主要是在后台处理一些耗时的逻辑,或者去执行某种长时间运行的任务,必要的时候我们甚至可以在程序退出的情况下,让service仍然可以在后台处于运行的状态。
我们新建一个Myservice,重写Service中onStartCommand,onCreate(),onDestroy()方法,如下列表所示:
public class MyService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Log.d("MyApp", "onCreate"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("MyApp", "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.d("MyApp", "onDestroy"); super.onDestroy(); } }service既然也是四大组件之一,要想使用就得在manifests中注册才能使用,注册代码如下:
<service android:name="com.czh.study.myhomeclass.MyServiceStudy.MyService" android:exported="false" />然后新建一个activity_main4.xml作为主程序的布局文件,代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main4" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="启动service" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/start_service" android:onClick="onClick" /> <Button android:text="关闭service" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/storp_service" android:onClick="onClick" /> </LinearLayout>这里在布局中加入了两个按钮,一个启动service,一个关闭service。 然后创建一个Main4Activity作为主布局,代码如下:
public class Main4Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); } public void onClick(View view){ switch (view.getId()){ case R.id.start_service: Intent startserviceIntent = new Intent(Main4Activity.this,MyService.class); startService(startserviceIntent); break; case R.id.storp_service: Intent storpserviceIntent = new Intent(Main4Activity.this,MyService.class); stopService(storpserviceIntent); break; } }这里我们构建了两个Intent对象,用startService来启动service,用stopService来停止service
先创建一个MyonBinder并重写service类的onStartCommand,onBind,onUnbind方法
public class MyonBinder extends Service { @Override public void onCreate() { Log.d("Myapp","onCreate"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Nullable @Override public IBinder onBind(Intent intent) { Log.d("Myapp","onBind"); return new MyBinder(); } @Override public boolean onUnbind(Intent intent) { Log.d("Myapp","onUnbind"); return super.onUnbind(intent); } @Override public void onDestroy() { Log.d("Myapp","onDestroy"); super.onDestroy(); } }还是需要注册才能使用:
<service android:name="com.czh.study.myhomeclass.MyServiceStudy.MyonBinder" android:exported="false" />然后在activity_main4.xml中的布局,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main4" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="绑定" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bind_service" android:onClick="onClick" /> <Button android:text="解绑定" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/unbindService" android:onClick="onClick" /> </LinearLayout>然后在主布局中创建两个按钮,用来启动和关闭service的:
public class Main4Activity extends AppCompatActivity { private ServiceConnection sc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); } public void onClick(View view){ switch (view.getId()){ case R.id.bind_service: sc = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { MyonBinder.MyBinder mb =(MyonBinder.MyBinder) service; mb.showToast(); mb.showList(); } @Override public void onServiceDisconnected(ComponentName name) { } }; bindService(new Intent(Main4Activity.this,MyonBinder.class),sc,BIND_AUTO_CREATE); break; case R.id.unbindService: unbindService(sc); break; } }