开发者

Show value in progressbar from native method

开发者 https://www.devze.com 2023-03-13 06:23 出处:网络
I have some native code and want to update the progressbar from the native code. There isn\'t no ret开发者_JAVA百科urn value because it is a long duration task.

I have some native code and want to update the progressbar from the native code. There isn't no ret开发者_JAVA百科urn value because it is a long duration task.

I found a small example http://developer.android.com/reference/android/widget/ProgressBar.html but when I move the update part in a extra method I get a NullPointerException.

It seems that this part must be in the thread of the progressbar.

I tried another way by using the AsyncTask as nested class, but I haven't access on the method publishProgress from anywhere outside the class itself.

Is there any possibility to get it working?


If it is possible to break your long task into multiple incremental calls then I'd recommend doing that. Then you can make those calls from a loop inside AsyncTask.doInBackground(), just like in the SDK examples.

If that's not possible, you'll need a progress variable in your native code that can safely be accessed from multiple threads: Write to it from your worker code and read it from a new "getProgress()" JNI function, with the appropriate synchronisation done in native code. You would then be able to call your getProgress() function from AsyncTask.doInBackground(), or whatever UI scheme you choose to use.

0

精彩评论

暂无评论...
验证码 换一张
取 消