我正在编写一个J2ME应用程序.其中一个部分定期轮询目录的内容,如果有任何新内容,则在屏幕上绘制它们.我已经通过让UI表单启动一个带有指针返回自身的轮询线程来完成此操作,并且当轮询线程找到它回调到表单并调用syncrhonized方法来更新它的显示时.这似乎工作正常.
我的问题是这个.在C#/ .NET中我知道让非UI线程更新UI并不好,处理它的正确方法是将其委托给UI线程.
例如以下内容:
public void DoSomeUIThing()
{
if (this.uiComponent.InvokeRequired)
{
this.uiComponent.Invoke(someDelegateThatCallsBackToThis);
}
else
{
this.uiComponent.Text = "This is the update I want to happen";
}
}
Run Code Online (Sandbox Code Playgroud)
有没有J2ME等价于如何管理这个过程?Java怎么样?或者Java/J2ME对此有何好处?如果没有,这是怎么做到的?
[编辑]看来Swing支持我通过SwingUtilities.invokeLater()和invokeAndWait()方法询问的内容.是否有相同的J2ME框架?
我正在使用Google Guice作为IOC容器处理Java Swing应用程序.事情进展顺利.有一些UI问题.当标准L&F被Pushing像素替换时,由于在UI线程之外创建Guice的Swing组件,物质L&F应用程序未运行.
有没有办法告诉Guice在UI线程中创建Swing组件?
也许我应该创建自定义提供程序,它们将在SwingUtilities.invokeAndWait(Runnable)创建后返回Swing组件.
我不喜欢在UI线程中运行整个应用程序的想法,但也许它只是一个完美的解决方案.
这是我的拳头stackoverflow帖子所以请对我温柔!我确信我正在尝试做的事情是可能的,这是我做过的事情(或者没做过的事情?)导致问题......我只是不确定那是什么东西.
我正在尝试做的事:
在我的应用程序同步并处理下载的数据时显示ProgressDialog.
问题:
ProgressDialog显示但不旋转(这使它看起来像已冻结),同步和处理发生,ProgressDialog关闭,应用程序继续正常.
我目前如何做到这一点:
创建ProgressDialog - 在我的活动中
执行同步 - 在我的服务
处理数据 - 在我的服务
Dismis中ProgressDialog - 在我的活动中
我尝试过的事情:
使用线程(下面的代码)使用AsynTask(如果需要可以提供代码)使用Handler(如果需要可以提供代码)
在花了很多时间寻找我的问题的答案后,似乎其他人有相同或类似的问题,并设法使用上述一个想法解决它.但是,我无法实现任何这些想法来解决我的特定问题.这让我确信这是我做错的事情......我只是不确定是什么.
我编写的原始代码并用作我所有尝试修复的基础:
首先
public class myActivity extends ListActivity {
private ProgressDialog dialog;
private boolean mIsBound;
private myService mBoundService;
private ServiceConnection mConnection;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*if we need to login start login activity for result*/
...
}
...
/*other methods*/
...
}
Run Code Online (Sandbox Code Playgroud)
然后
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch …Run Code Online (Sandbox Code Playgroud) 我有一个游戏,其中有广告.我最终发现滞后的来源基本上是因为adRequest过程需要很长时间.
super.onCreate(savedInstanceState){
mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
adView = new AdView(this, AdSize.BANNER, "MY_ID");
adView.setVisibility(AdView.VISIBLE);
mainLayout.addView(adView);
adView.loadAd(new AdRequest());
//more codes below
}
Run Code Online (Sandbox Code Playgroud)
我尝试做一些像创建一个线程,当有adRequest时会做一些loadAd.但导致广告不会显示.所以我认为loadAd请求必须在UI Thread中完成.这有什么解决方法吗?我仍然不了解UI Thread如何工作
通常在我想到的另一个线程的UI线程上做某事时出错,但我不知道我做错了什么.该错误似乎只出现在手机行驶时,因此GPS位置不断变化.
我希望存储最新的位置,因此UI上没有任何内容.我从主要活动调用了以下方法:
public void getFreshDeviceLocation(long interval, final long maxtime) {
if (gps_recorder_running){return;}
gpsTimer = new Timer();
//starts location
startMillis=System.currentTimeMillis();
// receive updates
for (String s : locationManager.getAllProviders()) {
LocationListener listener=new LocationListener() {
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
// if this is a gps location, we can use it
if (location.getProvider().equals(
LocationManager.GPS_PROVIDER)) {
doLocationUpdate(location, true); //stores the location in the prefs
stopGPS();
}
}
@Override
public …Run Code Online (Sandbox Code Playgroud) 使用handler想要定期运行计数为0,如果countis为1,否则请修复此代码.
mRunnable = new Runnable(){
@Override
public void run() {
if (count == 0) {
setImage();
count = 1;
} else {
weather = mContentResolver.getType(mUri);
setWeather(weather);
count = 0;
}
}
};
mHandler = new Handler();
mHandler.postDelayed(mRunnable, 3000);
Run Code Online (Sandbox Code Playgroud) 考虑这个例子:
async Task Foo()
{
button.Text = "This is the UI context!";
await BarA();
button.Text = "This is still the UI context!";
await BarB();
button.Text = "Oh no!"; // exception (?)
}
async Task BarA()
{
await Calculations();
}
async Task BarB()
{
await Calculations().ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)
如何await BarB()在不读取async Task BarB()函数体的情况下更改上下文?我是否真的需要确切知道异步函数是否ConfigureAwait(false)在任何时候调用?或者这个例子可能是错误的并且没有例外?
我弄清楚了.
出于某种原因,这个线程的代码实际上是在UI线程上运行的.如果我突破它,UI就会停止.或者睡觉吧,UI停了.因此,在"ui"线程中不允许网络活动.
我没有使用过异步任务,因为我不知道循环它的正确方法.(调用它的新实例onPostExecute似乎是不好的做法,好像异步是一个关闭的任务.
我扩展了Thread.
public class SyncManager extends Thread {
public SyncManager(Context context){
sdb = new SyncManagerDBHelper(context);
mContext = context;
}
@Override
public void run() {
while(State == RUNNING) {
try{
SyncRecords(); // Break point here = UI freeze.
} catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(10000); // So also causes UI freeze.
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void startThread() {
Log.i("SyncManager", "start called");
if((State == PAUSED || State == STOPPED) …Run Code Online (Sandbox Code Playgroud) 我正在尝试延迟我尝试过的Kotlin中的代码
Thread.sleep(1000)
Run Code Online (Sandbox Code Playgroud)
但是它冻结了UI。
有人知道为什么会这样吗,以及如何在不冻结UI的情况下进行延迟?