一.Service的簡介java
- 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顯示以下:android
當點擊 按鈕時,Service則執行了onDestroy()方法,LogCat顯示以下:app
當點擊或按鈕,進入Settings(設置)->Applications(應用)->Running Services(正在運行的服務)看一下咱們新啓動了的服務,效果圖以下:ide
下面是具體的例子:佈局
MainActivity.javathis
- 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.javaspa
- 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.xmlxml
- <?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>
效果圖:blog
當點擊按鈕時,前後執行了Service中onCreate()->onStart()這兩個方法,LogCat顯示以下:
當點擊按鈕,則Service執行了onUnbind()方法,LogCat顯示以下: