44.android服务service-启动和关闭

本文详细介绍了Android中的服务组件,包括其概念、不同类型的进程优先级、服务的生命周期以及如何通过startService和bindService来启动和关闭服务。同时,还提供了具体的代码示例。
Service
运行于后台的一个组件,用来运行适合运行在后台的代码,可以视为没有界面的activity
进程的优先级:

1.Foreground process:前台进程,

*拥有一个正在与用户交互的activity的进程onResume方法被调用。

*拥有一个与正在和用户交互的activity绑定的服务的进程

*拥有一个正在“运行于前台”的服务-服务的startforeground()被调用。

*拥有执行以下三个周期方法中任意一个的服务(onCreate(),onStart(),onDestroy()

*拥有一个正在执行onReceive方法的广播接收者的进程

2.visible process:可见进程,

*拥有可见没有焦点的activy,onPause方法被调用。

*拥有一个与可见(或者前台)activity绑定的服务进程

3.Service process:服务进程,通过startService启动的服务
4.background process:后台进程,拥有一个不可见的activity(onStop方法被调用)的进程

5.Empty process:空进程,没有任何活动的应用组件的进程(activity已经退出了)

服务的生命周期

1.通过Context.startService()启动服务
onCreate()-->onStart()-->onDestroy()
onCreate()在创建服务时调用,如果调用多次startService(),onCreate()方法仍然只被调用一次;
onStart()在开始startService()调用时被调用,多次startService(),onStart()方法会被调用多次;
onDestroy()在终止服务时调用;
2.通过Context.bindService()启动服务
onCreate()-->onBind()-->onUnbind()-->onDestroy()
onBind()在绑定服务时调用,如果调用多次bindService(),则onBind()方法只被调用一次;
onUnbind()在解除绑定(unBindService)时调用;
	//服务创建的额时候被调用
	@Override
	public void onCreate(){
		super.onCreate();
		System.out.println("onCreate方法被调用");
	}
	
	//可见没有焦点的时候调用,因为服务本身没有焦点,这个方法过时了,现在都用下面的onStartCommand方法
	@Override
	public void onStart(Intent intent, int startId){
		super.onStart(intent, startId);
		System.out.println("onStart方法被调用");
	}
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId){
		System.out.println("onStartCommand方法被调用");
		return super.onStartCommand(intent, flags, startId);
	}
	
	//服务销毁的时候调用
	@Override
	public void onDestroy(){
		super.onDestroy();
		System.out.println("onDestroy方法被调用");
	}
开启一个服务和关闭一个服务
配置文件:
<service android:name="com.ldw.startService.MyService"></service>
activity_main.xml
<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=".MainActivity"
    android:orientation="vertical"
    >


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启服务" 
        android:onClick="click"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关闭服务" 
        android:onClick="click2"
        />


</LinearLayout>
MyService.java
package com.ldw.startService;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;


public class MyService extends Service {


	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}


}

MainActivity.java

package com.ldw.startService;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void click(View v){
    	//显示启动服务
    	Intent intent = new Intent(this, MyService.class);
    	startService(intent);
    }
    
    public void click2(View v){
    	//关闭服务
    	Intent intent = new Intent(this, MyService.class);
    	stopService(intent);
    }
    
}






Line 7695: 03-29 20:49:34.614 2083 3479 W WifiService: Attempt to retrieve OsuProviders with invalid scanResult List Line 7799: 03-29 20:49:34.701 2083 3479 I WifiService: releaseMulticastLock uid=10118 tag=MdnsServiceBrowserImpl Line 8621: 03-29 20:49:35.635 2083 2889 D WifiClientModeManager[101700966:wlan0]: currentstate: ScanOnlyModeState Line 8622: 03-29 20:49:35.635 2083 2889 D WifiClientModeManager[101700966:wlan0]: Do not defer stop for non-internet providing CMMs Line 8623: 03-29 20:49:35.635 2083 2889 D WifiClientModeManager[101700966:wlan0]: The target role change info null Line 8624: 03-29 20:49:35.635 2083 2889 D WifiClientModeManager[101700966:wlan0]: Continue to stop wifi Line 8658: 03-29 20:49:35.652 27281 27297 I android.hardware.wifi-service-lazy: Adding interface handle for wlan0 Line 8659: 03-29 20:49:35.652 27281 27297 I android.hardware.wifi-service-lazy: Adding interface handle for p2p1 Line 8660: 03-29 20:49:35.652 27281 27297 I android.hardware.wifi-service-lazy: Adding interface handle for p2p0 Line 8662: 03-29 20:49:35.652 27281 27297 W android.hardware.wifi-service-lazy: No active wlan interfaces in use! Using default Line 8697: 03-29 20:49:35.668 27281 27297 D android.hardware.wifi-service-lazy: Stopping legacy HAL Line 8700: 03-29 20:49:35.668 27281 27445 I android.hardware.wifi-service-lazy: Legacy HAL stop complete callback received Line 8702: 03-29 20:49:35.668 27281 27445 D android.hardware.wifi-service-lazy: Legacy HAL event loop terminated Line 8703: 03-29 20:49:35.668 27281 27297 D android.hardware.wifi-service-lazy: Legacy HAL stop complete Line 8891: 03-29 20:49:35.816 27281 27297 I android.hardware.wifi-service-lazy: Wifi HAL stopped Line 8893: 03-29 20:49:35.817 2083 2889 I WifiVendorHal: Vendor Hal stopped Line 8894: 03-29 20:49:35.817 2083 2889 I WifiNative: Successfully torn down Iface:{Name=wlan0,Id=7,Type=STA_SCAN} Line 8896: 03-29 20:49:35.817 2083 2889 I WifiNative: Successfully initiated teardown for iface=wlan0 Line 8897: 03-29 20:49:35.817 2083 2889 I WifiClientModeManager[101700966:unknown]: StartedState.exit(), setting mRole = null Line 8980: 03-29 20:49:35.856 2083 2889 D WifiClientModeManager[101700966:unknown]: IdleState.exit() Line 9023: 03-29 20:49:35.884 2083 2889 I WifiService: Receive onDriverCountryCodeChanged to null, update available channel list Line 10345: 03-29 20:49:36.748 2083 2985 E OplusWifiHalServiceAidlImpl: IOplusWifiService.getFwStatus failed: java.lang.UnsupportedOperationException: 安卓手机WiFi为什么关闭后就开不了了
最新发布
04-03
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值