学习目标
service的配置,service服务生命周期,IntentService功能和用法
一
Service和activity比较相似,比如他们也都是context派生出来的,可以调用context中定义的getResources(),getContentResolver()等方法。当然,service也有自己的生命周期,方法如下:
IBinder onBind(Intent intent):service必须重写的方法,该方法返回一个IBinder对象,应用程序可通过该对象与service组件通信。
void onCreate():当该Service第一次被创建后将立即回调该方法
void onDestroy():当service被关闭之前立即回调该方法
void onStartCommand(Intent intent,int flag,int startId):之前是onStart方法,现已经过期不可用,当客户端调用startService(Intent)启动该service时,都会回调该方法。
boolean onUnbind(Intent intent):当该service上绑定的所有客户端都断开连接时将回调该方法
demo代码如下:
新建项目:
1,layout布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_bt1"
android:text="startService方式启动" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_bt2"
android:text="stopService方式关闭" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_bt3"
android:text="bindService方式关闭" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_bt4"
android:text="unbindService方式关闭" />
</LinearLayout>
2,activity活动java类代码
package com.example.android_lession8_1;
import com.example.service.MyService;
import com.example.service.MyService.Mybind;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
// private String app_name=MainActivity.this.getResources().getString(R.string.app_name);
// private String sd_path=Environment.getExternalStorageDirectory().getPath();
private Button bt1,bt2,bt3,bt4;
private ServiceConnection connection;
private int a=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1=(Button) findViewById(R.id.main_bt1);
bt2=(Button) findViewById(R.id.main_bt2);
bt3=(Button) findViewById(R.id.main_bt3);
bt4=(Button) findViewById(R.id.main_bt4);
connection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.d("zyy", "与服务断开连接");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("zyy", "与服务产生连接");
Mybind my=(Mybind) service;
int a=my.a;
Log.d("zyy", "service处理完后的a的值是"+a+" "+name.toString());
}
};
//启动service 采用startservice方式
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//指明启动的服务
Intent intent=new Intent();
intent.setAction("com.zyy.myservice");
//启动服务
MainActivity.this.startService(intent);
}
});
//关闭service
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//指明关闭的服务
Intent intent=new Intent();
intent.setAction("com.zyy.myservice");
//关闭服务
MainActivity.this.stopService(intent);
}
});
//bind方式启动服务
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//指明打开的服务
Intent intent=new Intent();
intent.setAction("com.zyy.myservice");
intent.putExtra("myparam", a);
//启动服务
MainActivity.this.bindService(intent, connection,Service.BIND_AUTO_CREATE);
}
});
//unbind关闭service
bt4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//指明关闭的服务
Intent intent=new Intent();
intent.setAction("com.zyy.myservice");
//关闭服务
MainActivity.this.unbindService(connection);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3,Service服务代码
package com.example.service;
import com.example.android_lession8_1.R;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service{
//context派生而出,故而res资源和sd卡都可以访问
public class Mybind extends Binder{
public int a;
public Mybind(int a){
this.a=a;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.d("zyy", "调用了服务的onBind 方法"+getResources().getString(R.string.app_name));
int a=intent.getIntExtra("myparam", 0);
if(a!=0){
a++;
}
return new Mybind(a);
}
@Override
public void onCreate() {
super.onCreate();
Log.d("zyy", "调用了服务的oncreate 方法");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d("zyy", "调用了服务的onStartCommand 方法");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.d("zyy", "调用了服务的onUnbind 方法");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("zyy", "调用了服务的onDestroy 方法");
}
@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
super.onRebind(intent);
Log.d("zyy", "调用了服务的onRebind 方法");
}
}
4,清单文件配置代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_lession8_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android_lession8_1.MainActivity"
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.service.MyService"
>
<intent-filter >
<action android:name="com.zyy.myservice"/>
</intent-filter>
</service>
</application>
</manifest>