更新原始活动中的集成进度条
我一直在尝试让我的进度条视图在我的文件扫描器应用程序中工作,但我对活动、服务、线程和处理程序的正确组合彻底感到困惑。
结构如下:我的活动包含一个水平样式的进度条。 单击菜单项时,我生成一个服务,即 onCreate(),我希望能够在其中更新进度栏。
我的问题:我错过了什么?
- 活动“a”(带有进度条)
2. "a".onOptionsItemSelected(): 生成服务 "b"
3. "b".onCreate(): 控制 "a" 中的进度条 // 这是我遇到麻烦的地方
进度条布局 (1):
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
android:secondaryProgress="0"
android:layout_width="300px"
android:layout_marginLeft="10px"
android:id="@+id/progress_horizontal"
/>
"a".onOptionsItemSelected (2):
public boolean onOptionsItemSelected(MenuItem item)
{
if (svc == null)
{
android.util.Log.v("@@@@@@@@@@@@@@@@@@@@@", "starting");
svc = new Intent(this, DoScan.class);
// done in "a".onCreate()
// hmap = new HashMap();
// hmap.put("tv", tv);
svc.putExtra("hmap", hmap);
startService(svc);
}
break;
}
"b".onCreate() (3 ):
@Override
public void onCreate() {
super.onCreate();
//startThread();
TextView tv = (TextView) Peekaboo.hmap.get("tv");
tv.append("cocktail");
}
I've been trying to get my progress bar view to work in my file scanner application, and I'm thoroughly stumped by the proper combination of Activities, Services, Threads, and Handlers.
Here's the structure: My Activity contains a Horizontal-styled ProgressBar. On menu item click, I spawn a Service which, onCreate(), which is where I want to be able to update the progress bar.
My question: what am I missing?
- Activity "a" (with ProgressBar)
2. "a".onOptionsItemSelected(): Spawn Service "b"
3. "b".onCreate(): Control the ProgressBar in "a" // here is where I have my trouble
Layout for Progress Bar (1):
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
android:secondaryProgress="0"
android:layout_width="300px"
android:layout_marginLeft="10px"
android:id="@+id/progress_horizontal"
/>
"a".onOptionsItemSelected (2):
public boolean onOptionsItemSelected(MenuItem item)
{
if (svc == null)
{
android.util.Log.v("@@@@@@@@@@@@@@@@@@@@@", "starting");
svc = new Intent(this, DoScan.class);
// done in "a".onCreate()
// hmap = new HashMap();
// hmap.put("tv", tv);
svc.putExtra("hmap", hmap);
startService(svc);
}
break;
}
"b".onCreate() (3):
@Override
public void onCreate() {
super.onCreate();
//startThread();
TextView tv = (TextView) Peekaboo.hmap.get("tv");
tv.append("cocktail");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
坦率地说,您的服务的
onCreate()
是......有问题的:对于#1 和#2,问问自己,当用户旋转屏幕(例如,滑出 G1 的键盘)并且服务所持有的小部件变得无效时会发生什么。
就 #3 而言,启动本地服务不会自动创建后台线程。 该服务将在与该活动和所有其他活动相同的线程上运行。 如果您希望在后台线程上完成工作,请使用
AsyncTask
或创建一个线程并使用 Handler 或post()
或postDelayed()
> 或runOnUiThread()
让后台线程安排 UI 更新在 UI 线程上发生。To be blunt, your service's
onCreate()
is...problematic:In terms of #1 and #2, ask yourself what will happen when the user rotates the screen (e.g., slides out the G1's keyboard), and the widgets the service is holding become invalid.
In terms of #3, starting a local service does not automatically create a background thread. The service will run on the same thread as the activity and all other activities. If you want work to be done on a background thread, use
AsyncTask
or create a thread and use a Handler orpost()
orpostDelayed()
orrunOnUiThread()
to have the background thread arrange for UI updates to occur on the UI thread.无需做任何特殊的事情,您的程序就在一个线程中运行,即 UI 线程,操作系统的 UI 线程。 程序中完成的任何操作都在 UI 线程中运行,除非您创建另一个线程来运行它。该线程需要将任务进度传达回 UI 线程(使用 Runnable、Handler 和 Thread 类)。 然后主 UI 线程更新用户在 ProgressBar 上看到的内容。
最好的办法是花一个小时完成 SDK 线程示例。
好的起点是此处和此处。
Without doing anything special your program runs in one thread, the UI thread, the OS's UI thread. Anything done in your program is running in the UI thread unless you create another thread for it to run in. That thread needs to communicate the task's progress back to the UI thread (use Runnable, Handler, and Thread classes). Then the main UI thread updates what the user sees on the ProgressBar.
This best thing to do is take an hour and work through the SDK thread examples.
Good places to start are here and here.