android本地後台服務樣本

來源:互聯網
上載者:User

在android編寫Service入門中介紹了android的兩種後台服務,本地和遠端。這裡用本地服務做了一個類比定時後台發簡訊的技術原型。

啟動該樣本應用的樣子:

 

選擇啟動:

啟動後,可看到ddms logcat日誌:

這是操作:啟動、停止和退出應用,顯示的調試日誌資訊。類比後台服務每5秒鐘發送一次簡訊資訊。

主要代碼,後台服務SmsService:

package com.easymorse;

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

public class SmsService extends Service {

    private boolean started;

    private boolean threadDisable;

    private ServiceBinder serviceBinder = new ServiceBinder();

    public class ServiceBinder extends Binder implements ISmsService {

        @Override
        public boolean isStarted() {
            return started;
        }

        @Override
        public void start() {
            started=true;
            Log.d(“sms.service”, “sms service started.”);
        }

        @Override
        public void stop() {
            started=false;
            Log.d(“sms.service”, “sms service stopped.”);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return serviceBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Thread thread = new Thread() {
            @Override
            public void run() {
                while (!threadDisable) {
                    try {
                        if (started) {
                            Log.d(“sms.service”, “send a sms message.”);
                        }
                        Thread.sleep(1000 * 5);
                    } catch (InterruptedException e) {
                    }
                }
            }
        };

        thread.start();

        Log.d(“sms.service”, “sms service created.”);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        threadDisable = true;
        Log.d(“sms.service”, “sms service shutdown.”);
    }
}

 

前台的Actvity代碼,SmsServiceOptions:

package com.easymorse;

import android.app.TabActivity;
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.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class SmsServiceOptions extends TabActivity {

    private RadioGroup radioGroup;

    private ISmsService smsService;

    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            smsService = (ISmsService) service;

            if (smsService.isStarted()) {
                radioGroup.check(R.id.radioButtonStart);
            } else {
                radioGroup.check(R.id.radioButtonStop);
            }

            radioGroup
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(RadioGroup group,
                                int checkedId) {
                            if (checkedId == R.id.radioButtonStart) {
                                Log.d(“sms.service”, “starting service…”);
                                smsService.start();
                            } else {
                                Log.d(“sms.service”, “stopping service…”);
                                smsService.stop();
                            }
                        }
                    });
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            smsService = null;
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.setTitle(“簡訊服務器”);
        this.bindService(new Intent(“com.easymorse.SmsService”),
                this.serviceConnection, BIND_AUTO_CREATE);

        TabHost tabHost = this.getTabHost();
        tabHost.setup();

        TabHost.TabSpec spec = tabHost.newTabSpec(“服務選項”);
        spec.setContent(R.id.Option01);
        spec.setIndicator(“服務選項”);
        tabHost.addTab(spec);

        spec = tabHost.newTabSpec(“服務狀態”);
        spec.setContent(R.id.Option02);
        spec.setIndicator(“服務狀態”);
        tabHost.addTab(spec);

        radioGroup = (RadioGroup) this.findViewById(R.id.radioGroup01);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unbindService(serviceConnection);
    }
}

 

另外,不要忘記在設定檔中增加對service的聲明,見AndroidManafest.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
    package=”com.easymorse” android:versionCode=”1″ android:versionName=”1.0″>
    <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
        <activity android:name=”.SmsServiceOptions” 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=”.SmsService”>
            <intent-filter>
                <action android:name=”com.easymorse.SmsService”></action>
            </intent-filter>
        </service>
    </application>
    <uses-sdk android:minSdkVersion=”3″ />
</manifest>

 

原始碼見:

http://easymorse.googlecode.com/svn/tags/android.local.service.1.demo/

這個樣本的局限性在於,如果Activity退出,則後台服務也一起退出。本地服務是否可以在Activity退出後依然在後台執行,還需要進一步瞭解android api。我猜測應該是不可以的,本地服務和Activity可能是同一個進程的不同線程關係。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.