android-handler

Most concise way to run a simple background task?

偶尔善良 提交于 2019-12-10 16:06:24
问题 I've seen at least five patterns through which you can have some code to run in a worker thread. Most simple: new Thread(new Runnable() { public void run() { // } }).start(); We can extend AsyncTask ; we have AsyncTaskLoader and other Loader s for data, Service s and so on. I assume each one of those has some advantages which I'm not discussing here. What I'm wondering is: which is the most appropriate, concise way to run a simple action? Defining simple : Quite short action, <1s; No need to

Multiple AsyncTasks issue on Android Spinner

拈花ヽ惹草 提交于 2019-12-10 16:02:09
问题 I'm getting some error. I know what is the problem but I don't know how to fix it. I've 4 spinner. Every spinner related one asynctask. For short, When I select one item on spinner1 , execute second asynctask that populate spinner2 (selected item add to second asynctask as parameter) Continues in this way. But before end of first asynctask, spinner2 can't populate. In this case I'm getting error. How to fix it ? How to make when first task finish, second task work ? Here is the code; First

Handler.sendMessageDelayed(msg, delay) not working correctly

百般思念 提交于 2019-12-10 13:36:53
问题 I have defined a splashscreen to be shown during loading. But depending on the internet connection it can takes only 600ms to load or sometimes 5000ms. So i defined that the splashscreen is at least shown 3000ms so that the user is not irritated by flackering screen. I define the start of the splashscreen the following way: private void splashScreen() { setContentView(R.layout.splashscreen); splash = (ImageView) findViewById(R.id.splashscreenLayer); startSplashTime = new Date(); new

AudioManager sending message to a Handler on a dead thread?

风流意气都作罢 提交于 2019-12-08 19:41:41
问题 I am trying to programmatically raise the volume to the STREAM_MUSIC stream's maximum value, but I am having a "Sending message to a Handler on a dead thread" issue when I do. Also, it seems like it does not raise the volume 100% of the time, although when I get this error, it will raise it MOST of the time. The code is: System.out.println("Maximum volume for this stream is: "+maxstreamvol+" and it used to be set to: "+currentvol); final AudioManager am = (AudioManager)this.getSystemService

Android Thread/Handler Error IllegalStateException: The specified message queue synchronization barrier token has not been posted

a 夏天 提交于 2019-12-07 16:29:07
问题 What I am trying to do Update TextView on the UI Thread every 3 seconds x 10 times using a Handler and a background thread. The output on the UI should be "Repeat: 1" to begin with and after every 3 seconds, it should ++ the number. Eg "Repeat: 1" updates to "Repeat: 2" after 3 seconds and then to "Repeat: 3" after further 3 seconds. How I am trying to do that The first method that I am testing is to use a for loop with a Thread.sleep() to cause a n second delay for each loop. In each loop, I

How to set Delay for loop in android?

孤者浪人 提交于 2019-12-07 07:03:13
问题 I am trying to display a multiplication table with delay. My code is working fine but I am not able to implement the delay. Here is My code: tableButton1 = (Button) findViewById(R.id.Button1); tableButton1.setOnClickListener(new OnClickListener() { public void onClick(View v) { new Thread(new Runnable() { public void run() { str = tableButton1.getText().toString(); a = Integer.parseInt(str); StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 10; i++){ sb.append(a + " x " + i + " = "

Android : Custom adapter fails to reload list view on activity resume

倾然丶 夕夏残阳落幕 提交于 2019-12-06 14:33:51
问题 I am trying to display received messages using custom adapter on list view , until now everything is working fine , but when I press back and come back and launch this activity again , only message send works but received messages cannot be displayed on the list view. Steps I have done so far : Use debugger to find out if message is not empty, messages are received and not empty. Use debugger inside receives() function , it turns out on first launch of activity looper.loop() function gives

How to use Handler and handleMessage in Kotlin?

孤者浪人 提交于 2019-12-06 04:58:21
问题 Java code: private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // code here } }; How to convert this java code to Kotlin? I tried this: private val mHandler = object : Handler() { fun handleMessage(msg: Message) { // code here } } But this is seems to be incorrect and gives a compile time error on object 回答1: Problem: The syntax to override handleMessage() method of the Handler class is incorrect. Solution: Add override keyword before the

Android: How to use a Handler handleMessage and access the reference to the MainActivity

半世苍凉 提交于 2019-12-05 23:33:11
问题 I have a service which receives a command from the Internet and launches a background thread. This thread is passed a handler from the service (the service is bounded and passed the handler) and sends a message to the handler to take a picture. I'm stuck on the implementation of the handler. static Handler handler = new Handler() { @Override public void handleMessage(Message msg) { //TODO: Handle different types of messages mCamera.takePicture(null, null, MainActivity.this); } }; Questions:

Android Thread/Handler Error IllegalStateException: The specified message queue synchronization barrier token has not been posted

假装没事ソ 提交于 2019-12-05 20:29:05
What I am trying to do Update TextView on the UI Thread every 3 seconds x 10 times using a Handler and a background thread. The output on the UI should be "Repeat: 1" to begin with and after every 3 seconds, it should ++ the number. Eg "Repeat: 1" updates to "Repeat: 2" after 3 seconds and then to "Repeat: 3" after further 3 seconds. How I am trying to do that The first method that I am testing is to use a for loop with a Thread.sleep() to cause a n second delay for each loop. In each loop, I call the sendMessage(message) method and my theory is that the handleMessage() method on the UI Thread