您的位置:首页 > 移动开发 > Android开发

Android 学习笔记 Service (一) Local Service

2012-11-14 16:53 417 查看

LocalService

本地service,指这个service只提供给自己的程序访问,其他程序不能访问。

源码结构:



MainActivity.java

package com.example.siqi;

import com.example.siqi.service.TestLocalService;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

/**
* 参考1:如何创建一个服务
* http://android.blog.51cto.com/268543/527314 * 参考2:如何创建服务,并与activity交互
* ApiDemos\src\com\example\android\apis\app\ LocalServiceBinding.java
* 和LocalService.java
* 参考3:如何让自己的后台服务在app退出后依然运行
* http://android.tgbus.com/Android/androidnews/200902/178999.shtml * 参考4:理解服务(推荐)
* http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html * @author siqi
*
*/
public class MainActivity extends Activity {

/**
* 自定义一个ServiceConnection类,用于处理连接事件的响应
*/
private ServiceConnection mServiceConnection =  new ServiceConnection(){

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mBoundService = ((TestLocalService.LocalBinder)service).getService();
Log.d(TAG, TAG + " onServiceConnected");
Log.d(TAG, TAG + " Service: " + mBoundService);
}

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mBoundService = null;
Log.d(TAG, TAG + " onServiceDisconnected");
}
};

/**
* 绑定的service
*/
private TestLocalService mBoundService = null;

/**
* true 表示connection没有与一个service绑定,不需要unbound;
* false表示connection已经与一个service绑定,需要unbound<p>
* 用于记录mServiceConnection是否已经unbound了,一个已经绑定的connection
* 在程序退出前必须要unbound,不然会有泄漏。并且,unbound只能有一次,
* 多次unbound会报错。
*/
boolean isConnectionUnbound = true;

/**
* 用于日志记录
*/
private static final String TAG = "MainActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Log.d(TAG, TAG + " onCreate()");
/**
* 启动service,用这句是因为这样可以在app退出后,
* 服务仍然不会退出。
*/
startService(new Intent("com.example.siqi.localservice"));
/**
* 第二次运行这个sample的时候去掉注释,看一下多次startService
* 的结果。
*/
//startService(new Intent("com.example.siqi.localservice"));

/**
* 绑定到service,如果service没有启动,那么绑定的时候会自动创建
*/
bindService(new Intent("com.example.siqi.localservice"),
mServiceConnection, BIND_AUTO_CREATE);
/**
* 第二次运行这个sample的时候去掉注释,看一下多次bindService
* 的结果。
*/
//bindService(new Intent("com.example.siqi.localservice"),
//		mServiceConnection, BIND_AUTO_CREATE);
isConnectionUnbound = true;

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
/**
* 如果connection是绑定的状态,那么unbound
*/
if(isConnectionUnbound) {
this.unbindService(mServiceConnection);
isConnectionUnbound = false;
Log.d(TAG, TAG + " unbindService()");
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onDestroy()");

/**
* 在activity结束前判断,如果connection还连接
* 到service,那么unbound。
*/
if(isConnectionUnbound) {
this.unbindService(mServiceConnection);
isConnectionUnbound = false;
Log.d(TAG, TAG + " unbindService()");
}

super.onDestroy();
}

}


TestLocalService.java

package com.example.siqi.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class TestLocalService extends Service{
private static final String TAG = "TestLocalService";
private IBinder mBinder = new LocalBinder();

/**
* 实现一个Binder类
*/
public class LocalBinder extends Binder {
public TestLocalService getService() {

return TestLocalService.this;
}
}

/**
* 这个函数的返回值会当作参数传到
* public void onServiceConnected(ComponentName name, IBinder service)
* IBinder I是指interface,一般我们会在service类实现一个Binder,这样
* 通过这个Binder,我们可以与service交互。
*/
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onBind()");
return mBinder;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onCreate()");
super.onCreate();
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onDestroy()");
super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Log.d(TAG, TAG + " onStart()");
super.onStart(intent, startId);
}

}
AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.siqi"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.TestLocalService" android:exported="false">
<intent-filter>
<action android:name="com.example.siqi.localservice" />
</intent-filter>
</service>
</application>

</manifest>


1)

程序运行结果:

11-14 16:35:30.133: D/MainActivity(3393): MainActivity onCreate()
11-14 16:35:30.203: D/TestLocalService(3393): TestLocalService onCreate()
11-14 16:35:30.213: D/TestLocalService(3393): TestLocalService onStart()
11-14 16:35:30.213: D/TestLocalService(3393): TestLocalService onBind()
11-14 16:35:30.264: D/MainActivity(3393): MainActivity onServiceConnected
11-14 16:35:30.264: D/MainActivity(3393): MainActivity Service: com.example.siqi.service.TestLocalService@44e8e5c0
按返回键退出程序:

11-14 16:35:41.223: D/MainActivity(3393): MainActivity unbindService()
11-14 16:35:46.234: D/MainActivity(3393): MainActivity onDestroy()
可以看到,因为我们是先startService然后bindService,所以在程序退出的时候TestLocalService并没有被结束掉,仍然在后台运行。

2)这次我们把

startService(new Intent("com.example.siqi.localservice"));
注释掉,这样就只有bindService了。
程序运行结果:

11-14 16:46:52.393: D/MainActivity(5040): MainActivity onCreate()
11-14 16:46:52.463: D/TestLocalService(5040): TestLocalService onCreate()
11-14 16:46:52.463: D/TestLocalService(5040): TestLocalService onBind()
11-14 16:46:52.543: D/MainActivity(5040): MainActivity onServiceConnected
11-14 16:46:52.553: D/MainActivity(5040): MainActivity Service: com.example.siqi.service.TestLocalService@44e8e2b8
按返回键退出程序:

11-14 16:47:03.003: D/MainActivity(5040): MainActivity onDestroy()
11-14 16:47:03.014: D/MainActivity(5040): MainActivity unbindService()
11-14 16:47:03.073: D/TestLocalService(5040): TestLocalService onDestroy()
可以看到service在程序结束的时候也结束掉了。

3)不注释startService和bindService,这样我们就有两个startService和bindService

程序运行结果:

11-14 16:47:03.014: D/MainActivity(5040): MainActivity unbindService()
11-14 16:47:03.073: D/TestLocalService(5040): TestLocalService onDestroy()
11-14 16:51:12.123: D/MainActivity(5682): MainActivity onCreate()
11-14 16:51:12.183: D/TestLocalService(5682): TestLocalService onCreate()
11-14 16:51:12.194: D/TestLocalService(5682): TestLocalService onStart()
11-14 16:51:12.203: D/TestLocalService(5682): TestLocalService onStart()
11-14 16:51:12.203: D/TestLocalService(5682): TestLocalService onBind()
11-14 16:51:12.283: D/MainActivity(5682): MainActivity onServiceConnected
11-14 16:51:12.283: D/MainActivity(5682): MainActivity Service: com.example.siqi.service.TestLocalService@44e8e8a8
可以看出,每次startService都会执行OnStart(),而bindService只会执行一次onBind()。

按返回键退出程序:

11-14 16:56:52.973: D/MainActivity(5682): MainActivity onDestroy()
11-14 16:56:52.983: D/MainActivity(5682): MainActivity unbindService()


分析什么的就不多说了,强烈推荐大家看一下这篇,说的很详细:
http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: