一、Service简单介绍
Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序。服务的开发比较简单,如下:
第一步:继承Service类
public class XXXService extends Service { }
第二步:在AndroidManifest.xml文件中的<application>节点里对服务进行配置:
<serviceandroid:name=".XXXService" />
服务不能自己运行,需要通过调用Context.startService()或Context.bindService()方法启动服务。这两个方法都可以启动Service,但是它们的使用场合有所不同。使用startService()方法启用服务,调用者与服务之间没有关连,即使调用者退出了,服务仍然运行。使用bindService()方法启用服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止,大有“不求同时生,必须同时死”的特点。
采用Context.startService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onStart()方法。如果调用startService()方法前服务已经被创建,多次调用startService()方法并不会导致多次创建服务,但会导致多次调用onStart()方法。采用startService()方法启动的服务,只能调用Context.stopService()方法结束服务,服务结束时会调用onDestroy()方法。
二、生命周期1:startService启动服务
上面做了简单介绍,那我们废话少说直接上代码,我们建立一个project
1、activity_main.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动service"
/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭service"
/>
</LinearLayout>
2、TestService类
package com.yyh.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class TestService extends Service{
public static String TAG = "TestService@@@";
/**
* 必须实现的方法。不重写会报错
*/
@Override
public IBinder onBind(Intent intent){
return null;
}
/**
* Service被创建时回调该方法
*/
@Override
public void onCreate(){
super.onCreate();
Log.v(TAG, "Created方法");
}
/**
* Service被启动时回调该方法
* onStart在API 5 被弃用,所以写onStartCommand
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Log.v(TAG, "onStartCommand方法");
return START_STICKY;
}
/**
* Service被关闭之前回调。
*/
@Override
public void onDestroy(){
super.onDestroy();
Log.v(TAG, "Destroyed方法");
}
}
3、MainActivity类
package com.yyh.main;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button start;
private Button stop;
private Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取程序界面中的start、stop两个按钮
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
//intent显式意图
//intent = new Intent(MainActivity.this, TestService.class);
//intent隐式意图
intent = new Intent();
intent.setPackage(getPackageName());//不加这句话的话 android 5.0以上会报:Service Intent must be explitict
intent.setAction("com.yyh.service.FIRST_SERVICE");// 设置Action属性,清单文件有对应
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.start:
startService(intent);// 启动指定Serivce
break;
case R.id.stop:
stopService(intent);// 停止指定Serivce
break;
default:
break;
}
}
}
需要注意的是,intent隐式启动Service,android5.0以上会报错误:java.lang.IllegalArgumentException: Service Intent must be explicit
4、AndroidManifest.xml
<!-- 配置一个Service组件 -->
<!-- 注册service,对应显示启动 -->
<!--<service android:name="com.yyh.service.TestService"/>-->
<!-- 注册service,对应隐式启动 -->
<service android:name="com.yyh.service.TestService">
<intent-filter>
<!-- 为该Service组件的intent-filter配置action -->
<action android:name="com.yyh.service.FIRST_SERVICE" />
</intent-filter>
</service>
更新中。。。
本文深入讲解Android中的服务机制,包括Service的基本概念、如何创建及配置服务、服务的启动方式及生命周期,通过实例代码演示如何使用startService启动服务,并探讨了服务的绑定与解绑过程。
5449

被折叠的 条评论
为什么被折叠?



