Tag: android handler

从PauseHandler实现调用SupportFragmentManager

我正在尝试实现此处描述的PauseHandler: https://stackoverflow.com/a/8122789/1977132 我的活动是ActionBarActivity,processMessage方法中的代码调出一个对话框,给出错误Cannot resolve method ‘getSupportFragmentManager’ 遵循此处的建议: https : //stackoverflow.com/a/27425568/1977132 我试图将它更改为getFragmentManager(),但后来我得到错误Cannot resolve method ‘show(android.app.FragmentManager(), java.lang.String’ 我在这里粘贴了我认为相关的代码: import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBarActivity; public class PlaySession extends ActionBarActivity implements View.OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { final Fragment state = new State(); final […]

带处理程序的顺序countdowntimer不会正确更新textView

我试图建立某种顺序倒计时。 意思是,我建立了一个“练习”队列,每个练习都包含一个特定的持续时间,即倒计时时间。 在自定义的Countdown类中,我将这些练习从队列中弹出并将持续时间用作倒计时。 我希望这些倒计时一个接一个地运行。 为此,我根据抽象类CountDownTimer的代码基础构建了一个Countdown类。 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Locale; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.widget.Button; import android.widget.TextView; public class ExerciseMeCountDownTimer { private static final int MSG_COUNTDOWN = 100; private static final int MSG_FINISH = 99; private ArrayDeque eq; private long mMillisInFuture; private int mCountdownInterval; private String name; private long […]

Android线程/处理程序错误IllegalStateException:尚未发布指定的消息队列同步障碍标记

我想做什么 使用处理程序和后台线程每3秒更新一次UI线程上的TextView x 10次。 UI上的输出应该是“重复:1”开始,每3秒开始,它应该是++的数字。 例如,“重复:1”在3秒后更新为“重复:2”,然后在3秒后更新为“重复:3”。 我是如何尝试这样做的 我测试的第一个方法是使用带有Thread.sleep()的for循环来为每个循环引起第二个延迟。 在每个循环中,我调用sendMessage(message)方法,我的理论是每次都会调用UI Thread上的handleMessage()方法。 这是代码: public class MainActivity extends AppCompatActivity { private static final String TAG = “MainActivity”; private TextView repeat; private Handler mHandler; private Thread backgroundThread; private String uiString; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, “onCreate(Bundle) called by Gil “); repeat = (TextView)findViewById(R.id.view_repeat); runInBackground(); backgroundThread.start(); […]

Handler.handleMessage未在测试中调用,但在应用程序中调用

我有一个在单独进程中运行的服务。 该服务在onCreate()方法中生成一个新线程。 该线程将消息发送回服务。 如果我手动启动应用程序一切正常 – Handler在我的服务中收到消息。 但在我的测试中, handleMessage()方法永远不会被调用。 如何修复我的测试以使handleMessage()方法有效? 谢谢! 服务: public class MyService extends Service { private ServiceHandler mHandler; private final int MSG_HELLO_WORLD = 1; private final String TAG = getClass().getSimpleName(); @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. return null; } @Override public void onCreate() { […]

这个线程一旦完成就会在最后运行时会发生什么?

我有这个线程从服务器下载一些图像。 因此,一旦下载了图像,我就会调用处理程序并进行UI更新。 因此,由于线程的stop()已弃用,我无法使用它。 我这里有两个问题。 这个线程最终会发生什么?(在我调用处理程序方法之后意味着它发生了什么)。 或者如何在不使用stop()的情况下停止此线程? 这是我的代码。 handler=new Handler() { public void handleMessage(Message msg) { if(msg.what==0) { //UI Updation takes place. } } }; final Thread t = new Thread(new Runnable() { public void run() { Log.i(“Inside Thread”, “Downloading Images…”); myDownlaodMethod(); handler.sendEmptyMessage(0); } }); t.start();