您的位置:首页 > 移动开发 > Android开发

Android中Service(服务)详解

2017-02-12 10:50 309 查看
Service是Android中四大组件之一,在Android开发中起到非常重要的作用,先来看一下官方对Service的定义:

Service
 是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件。服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。
此外,组件可以绑定到服务,以与之进行交互,甚至是执行进程间通信 (IPC)。 例如,服务可以处理网络事务、播放音乐,执行文件 I/O 或与内容提供程序交互,而所有这一切均可在后台进行。

服务基本上分为两种形式:

启动当应用组件(如 Activity)通过调用 
startService()
 启动服务时,服务即处于“启动”状态。一旦启动,服务即可在后台无限期运行,即使启动服务的组件已被销毁也不受影响。
已启动的服务通常是执行单一操作,而且不会将结果返回给调用方。例如,它可能通过网络下载或上传文件。 操作完成后,服务会自行停止运行。绑定当应用组件通过调用 
bindService()
 绑定到服务时,服务即处于“绑定”状态。绑定服务提供了一个客户端-服务器接口,允许组件与服务进行交互、发送请求、获取结果,甚至是利用进程间通信
(IPC) 跨进程执行这些操作。 仅当与另一个应用组件绑定时,绑定服务才会运行。 多个组件可以同时绑定到该服务,但全部取消绑定后,该服务即会被销毁。
以下是启动的小demo:XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
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"
android:orientation="vertical"
tools:context="com.dzz.android22_services.MainActivity">

<Buttonandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动服务"
android:onClick="start"
/>
<Buttonandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"
android:onClick="stop"
/>
</LinearLayout>


Java 代码:
package com.dzz.android22_services;

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,MyServices2.class);
}

public void start(View view){
intent.putExtra("data","下载的路径");
startService(intent);
}
public void stop(View view){
stopService(intent);
}

}

新建一个Acticity  MyServices
package com.dzz.android22_services;

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;

/**
* Created by 朝花偏不夕拾 on 2017/2/12.
*/

public class MyServices extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("test","onBind");
return null;
}
@Override
public void onCreate() {
Log.i("test","onCreate");
super.onCreate();
}
//ANR   application  not  responsing应用程序未响应
//why   耗时操作写到了主线程
//how   子线程

//why  服务和Thread的区别
//    服务死不掉
//    Thread  可以杀掉
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String data=intent.getStringExtra("data");
Log.i("test","onStartCommand,"+"startId");
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(300);
}
//停止服务
//            stopSelf();//当第一个线程执行完毕,则会停止服务
//            所有的线程都执行完毕,才停止服务
stopSelf(startId);
}
}
@Override
public void onDestroy() {
Log.i("test","onDestroy");
super.onDestroy();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息