Started Service实例
再一次的重申:一个started service 是另一个组件靠调用startService()并传递一个Intent来启动的,结果调用service的onStartCommend()并接受来自startService()传递过的Intent
- 有两个你可以继承的类去开启一个started service
- Service类(加个类吧,免得引起误会)
这个类对于所有的service是一个基本类,当你继承这个类的时候,很有必要开启一个新的线程去完成所有的service工作,依然是那个原因,这个service会使用你的主线程
- IntentService
这是service类的子类,继承了这个类,那么这个service将使用一个辅助线程去处理所有的开始请求,如果你不要求你的service同时去处理多种请求,那继承这样的服务将是一个好的选择,你只需实现onHandleIntent()方法,这个方法接收每一个请求的intent
2.下面是started service的两种方式的实例
继承IntentService类
这个intentService要做以下几件事:
创建一个默认的辅助线程执行将所有的intent发送给onStartCommand()
创建一个工作队列每次只将一个intent发送给onHandleIntent(),所以你不必担心多线程的问题
当所有的请求被处理完后,这个service将会停止,因此你也不必调用stopSelf()
提供一个默认的onBind(),返回的null值
提供一个默认的onStartCommand(),将发送intent给工作队列,然后调用onHandleIntent()
不说了,下面是实例
这是MainActivity类
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 sendMessage (View V)
{
System.out.println("你点击了按钮");
Intent intent = new Intent();
intent.setClass(MainActivity.this, HelloIntentService.class);
startService(intent);
}
}
这是mainAcivity类的xml配置文件
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="76dp"
android:layout_marginTop="115dp"
android:onClick="sendMessage"
android:text="开始" />
这是intentServiceexample类
public class HelloIntentService extends IntentService{
// 这个构造方法是必需的,调用父类的构造方法
public HelloIntentService() {
super("HelloIntentService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("onStartCommand-------被调用");
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
// 我们可以在这里处理一些工作,比如下载等等,这里我们为了看清楚他们的调用方法,将输出提示信息
System.out.println("onHandleIntent --------被调用");
}
}
别忘了加
<service android:name="HelloIntentService"></service>
输出结果可以用Logcat查看
如果你也一定要重写其他的回调方法,想onCreate(),onStartComman()或onDestrory(),时一定要调用父类的实现
继承Service类
如果你要求你的service执行一些多线程(而不是通过工作队列处理开始请求),那么你可以继承Service类
为了进行比较下面的例子,这个将和上面那个例子实现一样的工作
这是MainActivity类
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 sendMessage(View v)
{
System.out.println("你点击了按钮");
Intent intent = new Intent();
intent.setClass(MainActivity.this, HelloService.class);
startService(intent);
}
}
这MainActivity类的配置文件
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="76dp"
android:layout_marginTop="93dp"
android:text="点击"
android:onClick="sendMessage"/>
这是HelloService类
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.widget.Toast;
public class HelloService extends Service{
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
//接收来自线程的Message
private final class ServiceHandler extends Handler {
// ServiceHandler的构造方法
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// 我们要做的工作可以在这里写
// 这里我们只在logCat输出信息
System.out.println("handleMessage------被调用");
// 使用 startId停止服务
// the service in the middle of handling another job
stopSelf(msg.arg1);
}
}
@Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
System.out.println("onCreate------被调用");
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand------被调用");
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
// If we get killed, after returning from here, restart
return START_STICKY;
}
@Override
public void onDestroy() {
System.out.println("onDestory------被调用");
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
记得加
<service android:name="HelloService"></service>
结果是