Android开发之Service初步

来源:互联网 发布:城市电话区号查询软件 编辑:程序博客网 时间:2024/04/30 10:08
一、什么是Service
Service是Android系统提供的一个应用程序组件。Service与Activity组件类似,不过Service没有图形用户界面。Service通常用来处理一些耗时比较长的操作。Service在后台一直运行,可以使用Service更新ContentProvider,发送Intent以及启动系统的通知等等。
需要注意的是Service不是一个单独的进程也不是一个线程,而是主线程的一部分。
二、Service的使用
    自定义一个类继承自android.app.Service包中的Service类。复写Service类中的以下几个方法
1.public IBinder onBind(Intent arg0) 将一个Service和一个Activity绑定
2.public void onCreate() 创建一个Service对象时调用
3.public int onStartCommand(Intent intent, int flags, int startId) 当启动或者重新启动对象时调用
4.public void onDestroy() 销毁一个Service对象时调用
    Service需要在AndroidManifest.xml注册文件中注册
        <service android:name=".FirstService"></service>
示例:新建一个Android应用层,在main.xml中添加两个按钮:buttonStart和buttonStop,点击之后分别可以启动和停止Service。
    源代码:
    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"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /><Button android:text="startService" android:id="@+id/buttonStart" android:layout_width="200dp" android:layout_height="wrap_content"></Button><Button android:text="stopService" android:id="@+id/buttonStop" android:layout_width="200dp" android:layout_height="wrap_content"></Button></LinearLayout>


    Android_Service.java
    
package idea.org;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 Android_Service extends Activity {private Button buttonStart=null;private Button buttonStop=null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        buttonStart=(Button)findViewById(R.id.buttonStart);        buttonStop=(Button)findViewById(R.id.buttonStop);        buttonStart.setOnClickListener(new StartClickListener());        buttonStop.setOnClickListener(new StopClickListener());    }    class StartClickListener implements OnClickListener    {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent=new Intent();intent.setClass(Android_Service.this,FirstService.class);startService(intent);}        }    class StopClickListener implements OnClickListener    {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent=new Intent();intent.setClass(Android_Service.this,FirstService.class);stopService(intent);}        }}


    FirstService.java
    
package idea.org;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class FirstService extends Service{@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubSystem.out.println("Service onBind");return null;}/* (non-Javadoc) * @see android.app.Service#onCreate() */@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("Service onCreate");}/* (non-Javadoc) * @see android.app.Service#onStartCommand(android.content.Intent, int, int) */@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem.out.println("flags---->"+flags);System.out.println("startId---->"+startId);System.out.println("Service onStartCommand");return START_NOT_STICKY;}/* (non-Javadoc) * @see android.app.Service#onDestroy() */@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();System.out.println("Service onDestroy");}}


    AndroidManifest.xml
    
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="idea.org"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="11" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".Android_Service"                  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=".FirstService"></service>    </application></manifest>


    界面:
    
    运行效果:
    点击startService按钮启动Service,先调用onCreate()方法载调用onStartCommand()方法。
    
    启动Service之后就在后台运行,所以在此点击startService按钮,只调用onStartCommand()方法,不再调用onCreate()方法。
    
    点击stopService按钮之后调用onDestroy()方法停止Service服务。
    
    
原创粉丝点击