Android - 从服务中更新ProgressBar,是否可能?

4
我尝试从一个服务中更新进度条,我不知道这是否是正确的方法,但我必须这样做。在服务中,我正在尝试上传图像,并且它将返回通知栏的进度。现在,我希望添加一个进度条作为指示器,并删除通知栏上的通知。
这些是我用于调用进度条的代码。
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);

没有任何错误返回,但是当我尝试使用progressBar.setProgress(progress)更新进度条时,什么也没有发生。我的代码有什么问题?请问我该怎么做?

非常感谢您的任何意见和建议。谢谢。


更好的方法是使用异步任务,它可以让你更新进度条。在服务中,有绑定的服务,可以让你与用户界面进行通信。 - geniushkg
嗨,我已经尝试在onCreate()上使用setProgress(),但仍然不起作用。因此,我的第一个目标是先更新进度条,然后再尝试使用Async。 - Webster
1
http://stackoverflow.com/questions/15057565/update-progressbar-from-a-service-started-by-a-broadcastreceiver - Karthik
1
不要担心,有时候只需要额外的努力才能让事情正常运行,查看此链接并仔细阅读 - http://guides.codepath.com/android/handling-progressbars - geniushkg
@geniushkg,感谢您的建议,我会尝试去查看它。 - Webster
3个回答

9

更新进度条的方法不正确……应该使用广播接收器根据服务更新您的 Activity 或 Fragment。

从服务中发送广播。

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

针对Activity或Fragment

private MyBroadRequestReceiver receiver;

on onCreate

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

并且

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

在您希望更新进度条的Activity或Fragment中

public class MyBroadRequestReceiver extends BroadcastReceiver{

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

         //update your progressbar here

        }


    }

嗨,是的,但我正在尝试从服务中更新进度条,而不是从活动和片段中更新 :( - Webster
1
服务没有任何视图,因此您应该在Activity或Fragment上更新进度条。如果您发现它有用,可以将其标记为True,谢谢。 - 9spl

2

服务没有用户界面
因此,如果您想在服务和UI(活动)之间进行通信

您应该使用广播

在您的服务中,您可以添加以下内容

Intent intent = new Intent("Actionname");
            intent.putExtra("....",.......);
            intent.putExtra(".",...);
            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

在“您的活动”中
private class BroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
             if(intent.getAction().equalsIgnoreCase("reciverlocation"))
            {  // what you wane to do

}}

不要忘记在你的清单文件中添加接收器。

1

If your activity is running, you may use an interface for the same -

Step 1: Define interface in your service -

public interface OnProgressUpdateListener {

    void onProgressUpdate(int progress);
}

Step 2: Create setter for listener -

private static OnProgressUpdateListener progressListener;
public static void setOnProgressChangedListener(OnProgressUpdateListener _listener) {
        progressListener = _listener;
}

Step 3: implement listener into your `activity

MyService.setOnProgressChangedListener(this);
@Override
public void onProgressUpdate(int progress) {
    // Do update your progress...
}

PS, Don't forget to set it to null.. since it is static

@Override
public void onDestroy() {
    super.onDestroy();
    MyService.setOnProgressChangedListener(null);
}


嗨,谢谢您的回答,我会得到结果后再跟您联系。 - Webster

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接