Android开发之Service
1.创建、配置Service
package com.wwj;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class FirstService extends Service{
//必须实现的方法
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
// Service被创建时回调该方法
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Service is Created");
}
// Service被启动时回调该方法
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
System.out.println("Service is started");
}
// Service被关闭之前回调
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("Service is Destroyed");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wwj"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".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组件 -->
<service android:name=".FirstService">
<intent-filter >
<!-- 为该Service组件的intent-filter配置action -->
<action android:name="com.wwj.FIRST_SERVICE"/>
</intent-filter>
</service>
</application>
2.启动和停止Service
package com.wwj;
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 {
/** Called when the activity is first created. */
private Button start,stop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取程序界面中的start、stop两个按钮
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
//创建启动Service的Intent
final Intent intent = new Intent();
//为Intent设置Action属性
intent.setAction("com.wwj.FIRST_SERVICE");
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//启动指定Service
startService(intent);
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//停止指定Service
stopService(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动Service"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭Service"
/>
</LinearLayout>
以上为项目:Service的代码
项目运行效果如下:
DDMS的Logcat面板会显示如下信息:
3.绑定本地Service并与之通信
创建项目:BindService
代码如下:
==>BindService.java
package com.wwj;
import android.app.Service;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Binder;
import android.os.IBinder;
public class BindService extends Service{
private int count;
private boolean quit;
//定义onBinder方法所返回的对象
private MyBinder binder = new MyBinder();
//通过继承Binder来实现IBinder类
public class MyBinder extends Binder{
public int getCount(){
//获取Service的运行状态:count
return count;
}
}
//必须实现的方法
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("Service is Binded ");
//返回IBinder对象
return binder;
}
//Service被创建时回调该方法
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Service is Created");
//启动一条线程、动态地修改count状态值
new Thread(){
public void run() {
while(!quit){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
count++;
}
};
}.start();
}
//Service被断开连接时回调该方法
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("Service is Unbinded");
return true;
}
//Service被关闭之前回调
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
this.quit = true;
System.out.println("Service is Destroyed");
}
}
==>BindServiceActivity.java
package com.wwj;
import android.app.Activity;
import android.app.Service;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class BindServiceActivity extends Activity {
/** Called when the activity is first created. */
private Button bind, unbind, getServiceStatus;
// 保持所启动的Service的IBinder对象
BindService.MyBinder binder;
// 定义一个ServiceConnection对象
private ServiceConnection conn = new ServiceConnection() {
// 当该Activity与Service断开连接时回调该方法
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
System.out.println("--Service Disconnected--");
//获取Service的onBind方法返回的MyBinder对象
}
// 当该Activity与Service连接成功后回调该方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println("--Service Connected--");
binder = (BindService.MyBinder) service;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面中的bind,unbind,getServiceStatus按钮
bind = (Button)findViewById(R.id.bind);
unbind = (Button)findViewById(R.id.unbind);
getServiceStatus = (Button) findViewById(R.id.getServiceStatus);
//创建启动Service的Intent
final Intent intent = new Intent();
//为Intent设置Action
intent.setAction("com.wwj.BIND_SERVICE");
bind.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//绑定指定Service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
unbind.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//解除绑定Service
unbindService(conn);
}
});
getServiceStatus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//获取并显示Service的count值
Toast.makeText(BindServiceActivity.this, "Service的count值为: " + binder.getCount(),
4000).show();
}
});
}
}
布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定Service"
/>
<Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解除绑定"
/>
<Button
android:id="@+id/getServiceStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取Service状态"
/>
</LinearLayout>
注册文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wwj"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".BindServiceActivity"
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=".BindService">
<intent-filter >
<action android:name="com.wwj.BIND_SERVICE"/>
</intent-filter>
</service>
</application>
</manifest>
项目运行效果: