今天来讲一下安卓的四大基本组件之一的服务·
先来一个简单的排版
-<LinearLayout tools:context="com" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"><Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="start" android:text="启动服务"/><Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="stop" android:text="停止服务"/></LinearLayout>也就是两个按钮以便于模拟服务开启和关闭与之相对应的MainActivity
import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity { PRivate Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent(this,MyServices1.class); } public void start(View view){ //开启服务 startService(intent); } public void stop(View view){ stopService(intent);//关闭服务 }}然后需要新建一个类来继承Service 并重新它的方法
import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.SystemClock;import android.support.annotation.Nullable;import android.util.Log;/** * @author Zking-Snail * @time 2017/2/10 19:09 * @Version ${REV} */public class MyServices extends Service { @Nullable @Override public IBinder onBind(Intent intent) { Log.i("test","onBind"); return null; } @Override public void onCreate() { super.onCreate(); Log.i("test","onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { String data=intent.getStringExtra("data"); Log.i("test","onStartCommand,"+data); new MyThread(startId).start(); return super.onStartCommand(intent, flags, startId); } class MyThread extends Thread{ private int startId; public MyThread(int startId) { this.startId = startId; } @Override public void run() { super.run(); for (int i = 0; i <10 ; i++) { Log.i("test","i="+i); SystemClock.sleep(1000); }然后我这里是模拟一下进行打印的操作可以看到我这里另开了一个线程来进行打印这类耗时的操作
个人的见解是:
一.在应用中,如果是长时间的在后台运行,而且不需要交互的情况下,使用服务。
同样是在后台运行,不需要交互的情况下,如果只是完成某个任务,之后就不需要运行,而且可能是多个任务,需需要长时间运行的情况下使用线程。
二.如果任务占用CPU时间多,资源大的情况下,要使用线程。
关于服务的停止方式
stopSelf();//当第一个线程执行完毕,则会停止服务//所有的线程都执行完毕,才停止服务 stopSelf(startId);使用起来会有明显的区别服务为了弥补也线程相比方面的不足还可以继承另一个类IntentService
import android.app.IntentService;import android.content.Intent;import android.os.SystemClock;import android.util.Log;/** * @author Zking-Snail * @time 2017/2/10 20:05 * @Version ${REV} */public class MyServices2 extends IntentService { //自定义一个无参的构造方法来实现有参的构造方法 public MyServices2() { super(""); } public MyServices2(String name) { super(name); } //类似Service中的:onStartCommand @Override protected void onHandleIntent(Intent intent) { for (int i = 0; i <10 ; i++) { Log.i("test","i="+i); SystemClock.sleep(200); } }}相对于继承Service 而言IntentService、并不需要另开线程来执行耗时的操作,而且在关闭服务的时候继承了IntentService的类
会依照线程开辟的顺序来依次关闭线程!
与广播差不多的是也要在清单文件中声明
<service android:name=".MyServices2" android:exported="true"//让服务可以被执行 ></service> </application>可能我说的不够好,不喜勿喷
新闻热点
疑难解答