天天看点

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中。

继续阅读