一.Service的简介
- package com.android.service.activity;
- 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 MainActivity extends Activity
- {
- private Button startBtn;
- private Button stopBtn;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- startBtn = (Button) findViewById(R.id.startBtn);
- stopBtn = (Button) findViewById(R.id.stopBtn);
- //添加监听器
- startBtn.setOnClickListener(listener);
- stopBtn.setOnClickListener(listener);
- }
- //启动监听器
- private OnClickListener listener=new OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- Intent intent=new Intent(MainActivity.this, ServiceDemo.class);
- switch (v.getId())
- {
- case R.id.startBtn:
- startService(intent);
- break;
- case R.id.stopBtn:
- stopService(intent);
- break;
- default:
- break;
- }
- }
- };
- }
- package com.android.service.activity;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- public class ServiceDemo extends Service
- {
- private static final String TAG="Test";
- @Override
- //Service时被调用
- public void onCreate()
- {
- Log.i(TAG, "Service onCreate--->");
- super.onCreate();
- }
- @Override
- //当调用者使用startService()方法启动Service时,该方法被调用
- public void onStart(Intent intent, int startId)
- {
- Log.i(TAG, "Service onStart--->");
- super.onStart(intent, startId);
- }
- @Override
- //当Service不在使用时调用
- public void onDestroy()
- {
- Log.i(TAG, "Service onDestroy--->");
- super.onDestroy();
- }
- @Override
- //当使用startService()方法启动Service时,方法体内只需写return null
- public IBinder onBind(Intent intent)
- {
- return null;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/startBtn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="启动 Service"
- />
- <Button
- android:id="@+id/stopBtn"
- android:layout_width="match_parent"
- 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.android.service.activity"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="10" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".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=".ServiceDemo" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </service>
- </application>
- </manifest>
当点击按钮时,先后执行了Service中onCreate()->onStart()这两个方法,LogCat显示如下:
当点击 按钮时,Service则执行了onDestroy()方法,LogCat显示如下:
当点击或按钮,进入Settings(设置)->Applications(应用)->Running Services(正在运行的服务)看一下我们新启动了的服务,效果图如下:
下面是具体的例子:
MainActivity.java
- package com.android.bindservice.activity;
- 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.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- // 声明Button
- private Button startBtn,stopBtn,bindBtn,unbindBtn;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 设置当前布局视图
- setContentView(R.layout.main);
- // 实例化Button
- startBtn = (Button)findViewById(R.id.startBtn1);
- stopBtn = (Button)findViewById(R.id.stopBtn2);
- bindBtn = (Button)findViewById(R.id.bindBtn3);
- unbindBtn = (Button)findViewById(R.id.unbindBtn4);
- // 添加监听器
- startBtn.setOnClickListener(startListener);
- stopBtn.setOnClickListener(stopListener);
- bindBtn.setOnClickListener(bindListener);
- unbindBtn.setOnClickListener(unBindListener);
- }
- // 启动Service监听器
- private OnClickListener startListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 创建Intent
- Intent intent = new Intent();
- // 设置Class属性
- intent.setClass(MainActivity.this, BindService.class);
- // 启动该Service
- startService(intent);
- }
- };
- // 停止Service监听器
- private OnClickListener stopListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 创建Intent
- Intent intent = new Intent();
- // 设置Class属性
- intent.setClass(MainActivity.this, BindService.class);
- // 启动该Service
- stopService(intent);
- }
- };
- // 连接对象
- private ServiceConnection conn = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- Log.i("Service", "连接成功!");
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- Log.i("Service", "断开连接!");
- }
- };
- // �定Service监听器
- private OnClickListener bindListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 创建Intent
- Intent intent = new Intent();
- // 设置Class属性
- intent.setClass(MainActivity.this, BindService.class);
- // 绑定Service
- bindService(intent, conn, Service.BIND_AUTO_CREATE);
- }
- };
- // 解除绑定Service监听器
- private OnClickListener unBindListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 创建Intent
- Intent intent = new Intent();
- // 设置Class属性
- intent.setClass(MainActivity.this, BindService.class);
- // 解除绑定Service
- unbindService(conn);
- }
- };
- }
BindService.java
- package com.android.bindservice.activity;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- public class BindService extends Service{
- private static final String TAG="Test";
- //返回null
- public IBinder onBind(Intent intent) {
- Log.i(TAG, "Service onBind--->");
- return null;
- }
- // Service创建时调用
- public void onCreate() {
- Log.i(TAG, "Service onCreate--->");
- }
- // 当客户端调用startService()方法启动Service时,该方法被调用
- public void onStart(Intent intent, int startId) {
- Log.i(TAG, "Service onStart--->");
- }
- // 当Service不再使用时调用
- public void onDestroy() {
- Log.i(TAG, "Service onDestroy--->");
- }
- // 当解除绑定时调用
- public boolean onUnbind(Intent intent) {
- Log.i(TAG, "Service onUnbind--->");
- return super.onUnbind(intent);
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:text="启动Service"
- android:id="@+id/startBtn1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:text="停止Service"
- android:id="@+id/stopBtn2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:text="绑定Service"
- android:id="@+id/bindBtn3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:text="解除绑定"
- android:id="@+id/unbindBtn4"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
在AndroidManifest.xml文件中添加16~21行的声明
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.bindservice.activity"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="10" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".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=".BindService" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </service>
- </application>
- </manifest>
效果图:
当点击按钮时,先后执行了Service中onCreate()->onStart()这两个方法,LogCat显示如下:
当点击按钮,则Service执行了onUnbind()方法,LogCat显示如下:
当点击按钮,则Service执行了onUnbind()方法,LogCat显示如下: