天天看點

Android Service——活動和服務進行通信

服務是在活動裡啟動的,但是啟動了服務之後,活動與服務基本就沒什麼關系了,為了能讓服務和活動的關系緊密一點,在這裡介紹一種利用廣播接收器的方式來使得服務和活動聯系起來。

在這裡模拟下載下傳的情況,利用一個進度條來使得服務和活動聯系起來,然後布局一個按鈕和一個進度條

利用廣播接收器來實作活動和服務的聯系

<Button
        android:id="@+id/button_startdown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="開始下載下傳"/>

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_gravity="center_horizontal" />
           

然後在主活動中通過點選事件來啟動服務

Intent intent=new Intent(getApplicationContext(),MyFirstService.class);
        startService(intent);
           

在Service類中的onStartCommand()方法中定義一個count計數模拟每秒下載下傳百分之一,然後發送廣播

package com.example.administrator.myserviceapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by Administrator on 2015/9/8.
 */
public class MyFirstService extends Service {
    private int count=;
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("00000000000000000000000", "onCreate ");
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("00000000000000000000000", "onStartCommand ");
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    count++;
                    if (count>){
                        count=;
                    }
                    try {
                        Thread.sleep();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Intent intent=new Intent();
                    intent.setAction("com.downlowd.test");
                    intent.putExtra("count",count);
                    sendBroadcast(intent);
                }
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("00000000000000000000000", "onDestroy ");
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
           

最後在主活動中進行廣播接收器的注冊和處理

package com.example.administrator.myserviceapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import static com.example.administrator.myserviceapplication.R.id.button_startdown;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button mButtonStartDown;
    private ProgressBar mProgressBar;
    private MyDownLoadReceiver mMyDownReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButtonStartDown= (Button) findViewById(button_startdown);
        mButtonStartDown.setOnClickListener(this);
        mProgressBar= (ProgressBar) findViewById(R.id.progressBar);
        //廣播接收器的注冊   動态注冊
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("com.downlowd.test");
        mMyDownReceiver=new MyDownLoadReceiver();
        registerReceiver(mMyDownReceiver,intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //廣播接收器取消注冊

        unregisterReceiver(mMyDownReceiver);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.button_startdown:
               start();
                break;
            default:
                break;
        }
    }

    private void start() {
        Intent intent=new Intent(getApplicationContext(),MyFirstService.class);

        startService(intent);
    }

    private void stop() {
        Intent intent=new Intent(getApplicationContext(),MyFirstService.class);
        stopService(intent);
    }
    class MyDownLoadReceiver extends BroadcastReceiver{
//廣播接收器接收廣播并進行處理
        @Override
        public void onReceive(Context context, Intent intent) {
            int count=intent.getIntExtra("count",);
            mProgressBar.setProgress(count);
        }
    }
}
           

廣播接收器通過接收到的由服務發送的廣播後将設定進度條。

使用IntentService

由于Service不是一個單獨的程序 ,也不是一個線程,它和應用程式在同一個程序中,是以我們應該避免在Service裡面進行耗時的操作。是以如果有耗時操作在Service裡,就必須開啟一個單獨的線程來處理!

其他使用跟Service相同,隻是服務這一塊略有不同。

這裡寫一個IntentService類來具體說明一下

package com.example.administrator.myserviceapplication;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Administrator on 2015/9/9.
 */
public class MyIntentService extends IntentService {
    private int count=;
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public MyIntentService(String name) {
        super(name);
    }
    public MyIntentService(){
        this("");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("00000000000000000000000", "啟動了MyIntentService");
        while (true){
            count++;
            if (count>){
                count=;
            }
            try {
                Thread.sleep();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Intent intent1=new Intent();
            intent1.setAction("com.downlowd.test");
            intent1.putExtra("count",count);
            sendBroadcast(intent1);
        }
    }
}
           

同樣不要忘了在Manifest裡進行注冊。在這裡可以看到,Service中的那段代碼是寫在一個線程中,而IntentService本身就是一個線程是以不需要寫在Thread中。

繼續閱讀