Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Update ProgressBar from service, is it possible?

I'm trying to update a progressbar from a service. I don't know if this is the correct way or not, but I have to do that. In the service, I'm trying to upload images and it will return a progress for the notification bar. Now, I want to add a progress bar as the indicator and remove the notification at the notif bar.

These are the codes that I use for calling the ProgressBar

LayoutInflater inflater = (LayoutInflater) getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v = inflater.inflate(R.layout.fragment_doc, null);

    progressBar = (ProgressBar) v.findViewById(R.id.imgProgressBar);

It doesn't return any error, but when I tried to update the progressBar with

progressBar.setProgress(progress)

Nothing's happened.. What should I do to, is there anything wrong with my code?

Will appreciate any input. Thanks

like image 708
Webster Avatar asked Dec 13 '22 23:12

Webster


1 Answers

it is not correct way to update you Progress bar ... you should use Broadcast receiver to update your Activity or Fragment as per Service

From Service you should sendBroadCast

 Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(ACTION_NAME);       
        sendBroadcast(broadcastIntent);

For Activity or Framgent

private MyBroadRequestReceiver receiver;

on onCreate

IntentFilter filter = new IntentFilter(ACTION_NAME);
 receiver = new MyBroadRequestReceiver();
registerReceiver( receiver, filter);

and

 @Override
    public void onDestroy() {
        this.unregisterReceiver(receiver);
        super.onDestroy();
    }

And in your Activity or Fragment in Which you Want to update Progress bar

public class MyBroadRequestReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {

         //update your progressbar here

        }


    }
like image 114
9spl Avatar answered Dec 21 '22 10:12

9spl