android开发中关于service的介绍
很简单一个例子:
android开发中,当需要创建在后台运行的程序的时候,就要使用到service。service 可以分为有无限生命和有限生命两种。特别需要注意的是service跟activities是不同的(简单来说可以理解为后台与前台的区别),例如,如果需要使用service的话,需要调用startservice(),从而利用startservice()去调用service中的oncreate()和onstart()方法来启动一个后台的service。
启动一个service的过程如下:context.startservice() ->oncreate()- >onstart()->service running其中oncreate()可以进行一些服务的初始化工作,onstart()则启动服务。
停止一个service的过程如下:context.stopservice() | ->ondestroy() ->service stop
接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。
servicesdemo.java(是一个activity)
package com.android.myservice;
import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
public class servicedemo extends activity implements onclicklistener {
private static final string tag = "servicedemo";
button buttonstart, buttonstop;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
buttonstart = (button) findviewbyid(r.id.buttonstart);
buttonstop = (button) findviewbyid(r.id.buttonstop);
buttonstart.setonclicklistener(this);
buttonstop.setonclicklistener(this);
}
public void onclick(view src) {
switch (src.getid()) {
case r.id.buttonstart:
log.i(tag, "onclick: starting service");
startservice(new intent(this, myservice.class));
break;
case r.id.buttonstop:
log.i(tag, "onclick: stopping service");
stopservice(new intent(this, myservice.class));
break;
}
}
}
除此之外还要在manifest里面声明服务:(androidmanifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.android.myservice">
<application android:label="@string/app_name">
<activity android:name=".servicedemo" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.main"/>
<category android:name="android.intent.category.launcher"/>
</intent-filter>
</activity>
<service android:enabled="true" android:name=".myservice"/>
</application>
</manifest>
定义service(myservice.java)
package com.android.myservice;
import android.app.service;
import android.content.intent;
import android.media.mediaplayer;
import android.os.ibinder;
import android.util.log;
import android.widget.toast;
public class myservice extends service {
private static final string tag = "myservice";
mediaplayer player;
@override
public ibinder onbind(intent intent) {
return null;
}
@override
public void oncreate() {
toast.maketext(this, "my service created", toast.length_long).show();
log.i(tag, "oncreate");
player = mediaplayer.create(this, r.raw.braincandy);
player.setlooping(false);
}
@override
public void ondestroy() {
toast.maketext(this, "my service stoped", toast.length_long).show();
log.i(tag, "ondestroy");
player.stop();
}
@override
public void onstart(intent intent, int startid) {
toast.maketext(this, "my service start", toast.length_long).show();
log.i(tag, "onstart");
player.start();
}
}
layout文件夹中是main.xml
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<textview
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/services_demo" android:gravity="center" android:textsize="20sp" android:padding="20dp"/>
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonstart" android:text="@string/start"></button>
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" android:id="@+id/buttonstop"></button>
</linearlayout>
values 文件夹中是strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="start">start</string>
<string name="stop">stop</string>
<string name="services_demo">service demo</string>
</resources>
上一篇: rocketMQ基本架构简介
下一篇: HTTP请求方法对照表