Update/access 服务中的进度条、TextView
Update/access Progressbar, TextView from a Service
我有一个 activity A,它有一个进度条和一个文本视图。
如果用户单击一个正在启动服务 (ServiceB) 的按钮,我正在尝试找到一种方法来更新 Activity A 中的进度条并同时设置(进度) Activity A.
中 Textview 中的文本
我查看了 Google 和 Whosebug,我想我找到了一种方法来实现 here
但是我很难实现这个,非常感谢任何帮助。
PS:不要投反对票,我知道 UI 不应该直接从服务访问,所以我正在寻找一种正确的方法。
部分相关代码:
Activity答:
@EActivity(R.layout.downloads_activity)
public class DownloadsActivity extends BaseActivity {
@ViewById(R.id.progress_text)
TextView progresstxt;
@ViewById(R.id.progressdownload)
ProgressBar downloadprogress;
// Update Progressbar and set Text sent from ServiceB
}
服务B:
public class ServiceB extends IntentService {
...
@Override
public void onProgress(DownloadRequest request, long totalBytes, long downloadedBytes, int progress) {
int id = request.getDownloadId();
if (!isActive) {
downloadManager.cancel(downloadId1);
deleteCancelledFile.deleteOnExit();
} else if (id == downloadId1) {
// How to update progressbar and textview of Activity A?
progresstxt.setText("Downloading: " + progress + "%" + " " + getBytesDownloaded(progress, totalBytes));
downloadprogress.setProgress(progress);
}
}
...
}
您需要使用LocalBroadcastManager
以下是需要注意的步骤
在 activity 中创建一个 LocalBroadcastManager。
private BroadcastReceiver mLocalBroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// take values from intent which contains in intent if you putted their
// here update the progress bar and textview
String message = intent.getStringExtra("message");
int progress = Integer.parseInt(intent.getStringExtra("progress"));
}
};
在activity的onCreate()上注册它
LocalBroadcastManager.getInstance(this).registerReceiver(mLocalBroadcast ,
new IntentFilter("myBroadcast"));
在activity的onDestroy()
中注销
// 注销,因为 activity 即将关闭。
LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalBroadcast);
从服务发送更新到activity以更新UI
从 IntentService 通过 intent 发送进度和 textView 更新
Intent intent = new Intent("myBroadcast");
// You can also include some extra data.
intent.putExtra("message", "This is my message!"); // msg for textview if needed
intent.putExtra("progress", progressValue); // progress update
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
它将这些数据发送到我们在activity
中注册的mLocalBroadcast
希望这些对你有帮助。
我有一个 activity A,它有一个进度条和一个文本视图。
如果用户单击一个正在启动服务 (ServiceB) 的按钮,我正在尝试找到一种方法来更新 Activity A 中的进度条并同时设置(进度) Activity A.
中 Textview 中的文本我查看了 Google 和 Whosebug,我想我找到了一种方法来实现 here
但是我很难实现这个,非常感谢任何帮助。
PS:不要投反对票,我知道 UI 不应该直接从服务访问,所以我正在寻找一种正确的方法。
部分相关代码:
Activity答:
@EActivity(R.layout.downloads_activity)
public class DownloadsActivity extends BaseActivity {
@ViewById(R.id.progress_text)
TextView progresstxt;
@ViewById(R.id.progressdownload)
ProgressBar downloadprogress;
// Update Progressbar and set Text sent from ServiceB
}
服务B:
public class ServiceB extends IntentService {
...
@Override
public void onProgress(DownloadRequest request, long totalBytes, long downloadedBytes, int progress) {
int id = request.getDownloadId();
if (!isActive) {
downloadManager.cancel(downloadId1);
deleteCancelledFile.deleteOnExit();
} else if (id == downloadId1) {
// How to update progressbar and textview of Activity A?
progresstxt.setText("Downloading: " + progress + "%" + " " + getBytesDownloaded(progress, totalBytes));
downloadprogress.setProgress(progress);
}
}
...
}
您需要使用LocalBroadcastManager 以下是需要注意的步骤
在 activity 中创建一个 LocalBroadcastManager。
private BroadcastReceiver mLocalBroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// take values from intent which contains in intent if you putted their
// here update the progress bar and textview
String message = intent.getStringExtra("message");
int progress = Integer.parseInt(intent.getStringExtra("progress"));
}
};
在activity的onCreate()上注册它
LocalBroadcastManager.getInstance(this).registerReceiver(mLocalBroadcast ,
new IntentFilter("myBroadcast"));
在activity的onDestroy()
中注销// 注销,因为 activity 即将关闭。 LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalBroadcast);
从服务发送更新到activity以更新UI
从 IntentService 通过 intent 发送进度和 textView 更新
Intent intent = new Intent("myBroadcast");
// You can also include some extra data.
intent.putExtra("message", "This is my message!"); // msg for textview if needed
intent.putExtra("progress", progressValue); // progress update
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
它将这些数据发送到我们在activity
中注册的mLocalBroadcast希望这些对你有帮助。