android 程序和服务,Android服务和如何使用服务

Android服务的生命周期

Android中活动和碎片有生命周期,类似地,服务也有自己的生命周期,我们使用到的 onCreate()、onStartCommand()、onBind()和 onDestroy()等方法都是在服务的生命周期内可能回调的方法。

一旦在项目的任何位置调用了 Context 的 startService()方法,相应的服务就会启动起来,并回调 onStartCommand()方法。如果这个服务之前还没有创建过,onCreate()方法会先于onStartCommand()方法执行。服务启动了之后会一直保持运行状态,直到 stopService()或stopSelf()方法被调用。注意虽然每调用一次 startService()方法,nStartCommand()就会执行一次,但实际上每个服务都只会存在一个实例。所以不管你调用了多少次 startService()方法,只需调用一次 stopService()或 stopSelf()方法,服务就会停止下来了。

另外,还可以调用 Context 的 bindService()来获取一个服务的持久连接,这时就会回调服务中的 onBind()方法。类似地,如果这个服务之前还没有创建过,onCreate()方法会先于onBind()方法执行。之后,调用方可以获取到 onBind()方法里返回的 IBinder 对象的实例,这样就能自由地和服务进行通信了。只要调用方和服务之间的连接没有断开,服务就会一直保持运行状态。

当调用了 startService()方法后,又去调用 stopService()方法,这时服务中的 onDestroy()方法就会执行,表示服务已经销毁了。类似地,当调用了 bindService()方法后,又去调用unbindService()方法,onDestroy()方法也会执行,这两种情况都很好理解。但是需要注意,我们是完全有可能对一个服务既调用了 startService()方法,又调用了 bindService()方法的,这种情况下该如何才能让服务销毁掉呢?根据 Android 系统的机制,一个服务只要被启动或者被绑定了之后,就会一直处于运行状态,必须要让以上两种条件同时不满足,服务才能被销毁。所以,这种情况下要同时调用 stopService()和 unbindService()方法,onDestroy()方法才会执行。

服务的基本用法

程序中如何使用服务呢,可以定义一个MyServer类让其继承Service。如果我们希望服务一旦启动就立刻去执行某个动作,就可以将逻辑写在onStartCommand()方法里。而当服务销毁时,我们又应该在 onDestroy()方法中去回收那些不再使用的资源。以下是MyService类实现源码:

package com.example.luoxn28.activity;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

/**

* Created by luoxn28 on 2015/11/28.

*/

public class MyService extends Service {

private static final String TAG = "MyService";

private DownLoadBinder mBinder = new DownLoadBinder();

class DownLoadBinder extends Binder {

public void startDownLoad() {

Log.d(TAG, "startDownLoad");

}

public int getProgress() {

Log.d(TAG, "getProgress");

return 0;

}

}

@Override

public IBinder onBind(Intent intent) {

return mBinder;

}

@Override

public void onCreate() {

super.onCreate();

Log.d(TAG, "onCreate");

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.d(TAG, "onStartCommand");

return super.onStartCommand(intent, flags, startId);

}

@Override

public void onDestroy() {

super.onDestroy();

Log.d(TAG, "onDestroy");

}

}

另外需要注意,每一个服务都需要在 AndroidManifest.xml文件中进行注册才能生效,不知道你有没有发现,这是 Android四大组件共有的特点。于是我们还应该修改AndroidManifest.xml文件,代码如下所示:

package="com.example.luoxn28.activity" >

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name" >

android:name=".MainActivity"

android:launchMode="singleTask"

android:label="@string/app_name" >

布局文件

activity_main.xml源码:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/start_service"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Start Service" />

android:id="@+id/stop_service"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Stop Service" />

android:id="@+id/bind_service"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Bind Service" />

android:id="@+id/unbind_service"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Unbind Service" />

MainActivity.java源码

package com.example.luoxn28.activity;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.view.View;

import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {

private static final String TAG = "hdu";

private Button start_service;

private Button stop_service;

private Button bind_service;

private Button unbind_service;

private MyService.DownLoadBinder downLoadBinder;

private ServiceConnection connection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name){

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

downLoadBinder = (MyService.DownLoadBinder) service;

downLoadBinder.startDownLoad();

downLoadBinder.getProgress();

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);

start_service = (Button) findViewById(R.id.start_service);

stop_service = (Button) findViewById(R.id.stop_service);

bind_service = (Button) findViewById(R.id.bind_service);

unbind_service = (Button) findViewById(R.id.unbind_service);

start_service.setOnClickListener(this);

stop_service.setOnClickListener(this);

bind_service.setOnClickListener(this);

unbind_service.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.start_service:

Intent startIntent = new Intent(this, MyService.class);

startService(startIntent); // 启动服务

break;

case R.id.stop_service:

Intent stopIntent = new Intent(this, MyService.class);

stopService(stopIntent); // 停止服务

break;

case R.id.bind_service:

Intent bindIntent = new Intent(this, MyService.class);

bindService(bindIntent, connection, BIND_AUTO_CREATE); // 绑定服务

break;

case R.id.unbind_service:

unbindService(connection); // 解绑服务

break;

default:

break;

}

}

@Override

protected void onDestroy() {

super.onDestroy();

}

}

最后来个界面

0818b9ca8b590ca3270a3433284dd417.png

点击相应的按钮,就可以在logcat中看到相应的信息了。

参考资料:

1、《第一行代码》 服务相关章节

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值