使用BroadcastReceiver更新ProgressBar

问题描述 投票:1回答:4

我试图从ProgressBar任务更新Service。我实现了一个BroadcastReceiver,以便我可以与UI线程进行交互。我更新了主要活动中的ProgressBar,并从MyService活动中接收数据。 MyService活动执行Async任务并更新应在OnProgressUpdate方法中发回的意图。

这是我的代码:

主要内容:

package com.example.services;

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

import static android.content.Intent.ACTION_ATTACH_DATA;

public class MainActivity extends AppCompatActivity {

    private MyBroadRequestReceiver receiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFilter filter = new IntentFilter(ACTION_ATTACH_DATA);
        receiver = new MyBroadRequestReceiver();
        registerReceiver( receiver, filter);
    }

    public void startService(View view) {
        startService(new Intent(getBaseContext(), MyService.class));
        //pb.setProgress();
    }
    public void stopService(View view) {
        stopService(new Intent(getBaseContext(), MyService.class));
    }
    public class MyBroadRequestReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar);
            int progress = intent.getFlags();
            pb.setProgress(progress);

        }

    }
}

为MyService:

package com.example.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.AsyncTask;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {

    int counter = 0;
    static final int UPDATE_INTERVAL = 1000;
    private Timer timer = new Timer();

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        doSomethingRepeatedly();

        try {
            new DoBackgroundTask().execute(
                    new URL("http://www.amazon.com/somefiles.pdf"),
                    new URL("http://www.wrox.com/somefiles.pdf"),
                    new URL("http://www.google.com/somefiles.pdf"),
                    new URL("http://www.learn2develop.net/somefiles.pdf"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null){
            timer.cancel();
        }
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }

    private void doSomethingRepeatedly() {
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                Log.d("MyService", String.valueOf(++counter));

            }
        }, 0, UPDATE_INTERVAL);
    }

    private class DoBackgroundTask extends AsyncTask<URL, Integer, Long> {
        protected Long doInBackground(URL... urls) {
            int count = urls.length;
            long totalBytesDownloaded = 0;
            for (int i = 0; i < count; i++) {
                totalBytesDownloaded += DownloadFile(urls[i]);
                //Intent broadcastIntent = new Intent();
                //broadcastIntent.setAction(Intent.ACTION_ATTACH_DATA);
                //sendBroadcast(broadcastIntent);
                publishProgress((int) (((i + 1) / (float) count) * 100));
            }
            return totalBytesDownloaded;
        }

        protected void onProgressUpdate(Integer... progress) {
            Log.d("Downloading files", String.valueOf(progress[0]) + "% downloaded");
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("com.example.services.MainActivity");
            //broadcastIntent.putExtra("progress",progress);
            broadcastIntent.setFlags(progress[0]);
            sendBroadcast(broadcastIntent);
            Toast.makeText(getBaseContext(),
                    String.valueOf(progress[0]) + "% downloaded-"+counter,
                    Toast.LENGTH_LONG).show();

        }

        protected void onPostExecute(Long result) {
            Toast.makeText(getBaseContext(), "Downloaded " + result + " bytes",
                    Toast.LENGTH_LONG).show();
            //stopSelf();
        }
    }

    private int DownloadFile(URL url) {
        try {
            //---simulate taking some time to download a file---
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //---return an arbitrary number representing
        // the size of the file downloaded---
        return 100;
    }
}

请看看我的qazxsw poi并告诉我我是否正确这样做。我的ProgressBar根本没有更新。

java android android-service android-progressbar
4个回答
0
投票

因为你没有startService ononCreate()方法。服务不会运行。


0
投票

首先,这不是你解决问题的好方法。请浏览Google Android docs qazxsw poi

我建议你切换到DownloadManager。


0
投票

您的代码存在一些问题。

首先,如果您需要从服务更新活动的进度条,那么有效的方法是使用onProgressUpdate而不是普通广播(称为全局广播)。

其次,因为在服务中,你使用Backgournd guide,然后在活动中,你必须使用相同的动作来接收广播。

LocalBroadcastManager

代替

broadcastIntent.setAction("com.example.services.MainActivity");

最后,因为您在服务中使用AsyncTask,为了避免服务的泄漏上下文,推荐的方法是将asynctask声明为静态类。

全部放在一起。

my service.Java

IntentFilter filter = new IntentFilter("com.example.services.MainActivity");

main activity.Java

IntentFilter filter = new IntentFilter(ACTION_ATTACH_DATA);

0
投票

您的意图过滤器定义为“ACTION_ATTACH_DATA”

public class MyService extends Service {

    int counter = 0;
    static final int UPDATE_INTERVAL = 1000;
    private Timer timer = new Timer();

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        doSomethingRepeatedly();

        try {
            new DoBackgroundTask(this).execute(
                    new URL("http://www.amazon.com/somefiles.pdf"),
                    new URL("http://www.wrox.com/somefiles.pdf"),
                    new URL("http://www.google.com/somefiles.pdf"),
                    new URL("http://www.learn2develop.net/somefiles.pdf"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }

    private void doSomethingRepeatedly() {
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                Log.d("MyService", String.valueOf(++counter));

            }
        }, 0, UPDATE_INTERVAL);
    }

    // Declare asynctask as static class.
    private static class DoBackgroundTask extends AsyncTask<URL, Integer, Long> {

        // Using WeakReference to keep the context of the service to avoid leaking.
        private WeakReference<Context> mContext;

        DoBackgroundTask(Context context) {
            mContext = new WeakReference<>(context);
        }

        protected Long doInBackground(URL... urls) {
            int count = urls.length;
            long totalBytesDownloaded = 0;
            for (int i = 0; i < count; i++) {
                totalBytesDownloaded += DownloadFile(urls[i]);
                //Intent broadcastIntent = new Intent();
                //broadcastIntent.setAction(Intent.ACTION_ATTACH_DATA);
                //sendBroadcast(broadcastIntent);
                publishProgress((int) (((i + 1) / (float) count) * 100));
            }
            return totalBytesDownloaded;
        }

        protected void onProgressUpdate(Integer... progress) {
            Log.d("Downloading files", String.valueOf(progress[0]) + "% downloaded");
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("com.example.services.MainActivity");
            //broadcastIntent.putExtra("progress",progress);
            broadcastIntent.setFlags(progress[0]);

            Context context = mContext.get();
            if (context != null) {
                LocalBroadcastManager.getInstance(context).sendBroadcast(broadcastIntent);

                int counter = ((MyService)context).counter;
                Toast.makeText(context,
                        String.valueOf(progress[0]) + "% downloaded-" + counter,
                        Toast.LENGTH_LONG).show();
            }
        }

        protected void onPostExecute(Long result) {
            Context context = mContext.get();
            if (context != null) {
                Toast.makeText(context, "Downloaded " + result + " bytes",
                        Toast.LENGTH_LONG).show();
                //stopSelf();
            }
        }

        private int DownloadFile(URL url) {
            try {
                //---simulate taking some time to download a file---
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //---return an arbitrary number representing
            // the size of the file downloaded---
            return 100;
        }
    }
}

所以,像这样发送你的广播:

public class MainActivity extends AppCompatActivity {

    private MyBroadRequestReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Register broadcast receiver.
        IntentFilter filter = new IntentFilter("com.example.services.MainActivity");
        receiver = new MyBroadRequestReceiver();
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
    }

    @Override
    protected void onStop() {
        // Unregister broadcast receiver.
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
        super.onStop();
    }

    public void startService(View view) {
        startService(new Intent(getBaseContext(), MyService.class));
    }

    public void stopService(View view) {
        stopService(new Intent(getBaseContext(), MyService.class));
    }

    public class MyBroadRequestReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar);
            int progress = intent.getFlags();
            pb.setProgress(progress);
        }
    }
}

另外,不要忘记在onDestroy上取消注册广播

© www.soinside.com 2019 - 2024. All rights reserved.