Android的很多功能都使用到了后台服务,就像Windows应用程序大量依赖后台服务一样。服务的作用无需赘言,Android应用程序是如何实现后台服务的呢。今天写了一个创建和停止后台服务的简单例子,在此提供给大家做参考。当然Android后台服务的实际应用肯定比这个复杂,更为复杂的情况我们今后再做探讨。
ServiceActivity
<span style="font-family:Microsoft YaHei;">package com.example.androidexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ServiceActivity extends Activity {
private Button btnStartService;
private Button btnStopService;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_service);
intent = new Intent();
intent.setAction("My_Action");
btnStartService = (Button)super.findViewById(R.id.btnStartService);
btnStopService = (Button)super.findViewById(R.id.btnStopService);
btnStartService.setOnClickListener(startServiceListener);
btnStopService.setOnClickListener(stopServiceListener);
}
OnClickListener startServiceListener = new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startService(intent);
}
};
OnClickListener stopServiceListener = new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
stopService(intent);
}
};
}</span>
从这个Activity里我们注意到Activity和Service的关联是通过Intent实现的。通过设置Intent的Action来指定动作的执行者。这里还需要说的一点是关于Service的Manifest.xml配置文件。这里面需要指定Action和Service的类名。
<span style="font-family:Microsoft YaHei;"><application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidexample.ServiceActivity"
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:name="com.example.business.FirstService">
<intent-filter>
<action android:name="My_Action"></action>
</intent-filter>
</service>
</application></span>
这里我们能够更加清楚地看到Activity是如何跟Service连接起来的。
我们使用的布局很简单,就是两个按钮。
<span style="font-family:Microsoft YaHei;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnStartService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/StartService"/>
<Button
android:id="@+id/btnStopService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/StopService"/>
</LinearLayout></span>
这里还有一个重要的角色类不可忽视,那就是Service类。
<span style="font-family:Microsoft YaHei;">package com.example.business;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import com.example.androidexample.*;
public class FirstService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Service is created.");
//Toast.makeText(new ServiceActivity(), "Service is created", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("Service is destoryed.");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service is started.");
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}</span>
这个类很好理解,是做Service的启动和停止的。注意这里的System.out.println()不是往console里面输出,而是输出在LogCat的消息里面。
点击StartService 和 StopService后我再LogCat上能看到如下消息:
下一个章节我们会讨论