Android Service 组件

Service是Androd系统提供的四种组件之一,它的地位和Activity是并列的,只不过没有Activity的使用频率高。顾名思义Service就是运行在后台的一种服务程序,一般很少和用户交互,因此没有可视化界面。


下面我们演示一下如何创建一个Service:
1:我们通过布局文件layout/main.xml创建一个启动、停止、及绑定一个Service
<?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/startButton01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="启动Service"
  />
  <Button
    android:id="@+id/stopButton02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="停止Service"
  />
  <Button
    android:id="@+id/bindButton03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="绑定Service"
  />
  <Button
    android:id="@+id/unbindButton04"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="解除绑定"
  />
</LinearLayout>
2:我们创建一个MainActivity.java来实现Service
package com.android.test;

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;
import android.widget.Toast;

public class MainActivity extends Activity {
    
   private Button startBtn, stopBtn, bindBtn, unbindBtn;
  
         public void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                 //实例化Button
                startBtn = (Button)findViewById(R.id.startButton01);
                stopBtn = (Button)findViewById(R.id.stopButton02);
                bindBtn = (Button)findViewById(R.id.bindButton03);
                unbindBtn = (Button)findViewById(R.id.unbindButton04);
                 //添加监听器
                startBtn.setOnClickListener(startListener);
                stopBtn.setOnClickListener(stopListener);
                bindBtn.setOnClickListener(bindListener);
                unbindBtn.setOnClickListener(unBindListener);
        }
         //启动Service监听器
         private OnClickListener startListener = new OnClickListener() {

     public void onClick(View v) {
      Intent intent = new Intent();
       //设置Action属性
      intent.setAction( "com.android.test.action.MY_SERVICE");
      startService(intent);
    }
          
        };
         //停止Service监听器
         private OnClickListener stopListener = new OnClickListener() {

     public void onClick(View v) {
      Intent intent = new Intent();
      intent.setAction( "com.android.test.action.MY_SERVICE");
      stopService(intent);
    }
        };
         //绑定Service监听器
         private OnClickListener bindListener = new OnClickListener() {

     public void onClick(View v) {
      Intent intent = new Intent();
      intent.setAction( "com.android.test.action.MY_SERVICE");
       //绑定Service
      bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }
        };
         //解除Service监听器
         private OnClickListener unBindListener = new OnClickListener() {

     public void onClick(View v) {
      Intent intent = new Intent();
      intent.setAction( "com.android.test.action.MY_SERVICE");
       //解除绑定
      unbindService(conn);
    }
        };
        
         private ServiceConnection conn = new ServiceConnection() {

     public void onServiceConnected(ComponentName name, IBinder service) {
      Log.i( "SERVICE", "连接成功!");
      Toast.makeText(MainActivity. this, "连接成功!", Toast.LENGTH_LONG).show();
    }

     public void onServiceDisconnected(ComponentName name) {
      Log.i( "SERVICE", "断开连接!");
      Toast.makeText(MainActivity. this, "断开连接!", Toast.LENGTH_LONG).show();
    }      
        };
3:创建一个MyService.java继承Service,覆盖其声明周期中的方法,并在各个方法中显示信息
package com.android.test;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
   //可以返回null,通常返回一个aidl定义的接口
   public IBinder onBind(Intent intent) {
    Log.i( "SERVICE", "onBind......");
    Toast.makeText(MyService. this, "onBind......", Toast.LENGTH_LONG).show();
     return null;
  }
   //Service创建时调用
   public void onCreate() {
    Log.i( "SERVICE", "onCreate......");
  }
   //当客户端调用startService()方法启动Service时,该方法被调用
   public void onStart(Intent intent, int startId) {
    Log.i( "SERVICE", "onStart......");
    Toast.makeText(MyService. this, "onStart......", Toast.LENGTH_LONG).show();
  }
   //当Service不再使用时调用
   public void onDestroy() {
    Log.i( "SERVICE", "onDestroy......");
    Toast.makeText(MyService. this, "onDestroy......", Toast.LENGTH_LONG).show();
  }
}
4:在AndroidManifest.xml配置文件中声明Activity和Service
<?xml version= "1.0" encoding= "utf-8"?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
            package="com.android.test"
            android:versionCode="1"
            android:versionName="1.0">
        <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="MyService">
      <intent-filter>
        <action android:name="com.android.test.action.MY_SERVICE"/>
      </intent-filter>
    </service>
        </application>
        <uses-sdk android:minSdkVersion="4" />

</manifest>
运行如图:
Android Service 组件_第1张图片 Android Service 组件_第2张图片 Android Service 组件_第3张图片 Android Service 组件_第4张图片 Android Service 组件_第5张图片

本文出自 “Art_Hero” 博客,请务必保留此出处http://curran.blog.51cto.com/2788306/527777

你可能感兴趣的:(android)