我正在使用,AsyncQueryHandler并onQueryComplete在查询完成后调用它.
我的问题:onQueryComplete在UI线程上调用?
我知道它在后台进行查询.
在哪里AsyncQueryHandler实例化是否重要?(如果在UI线程中实例化将意味着onQueryComplete将在UI线程上调用).
multithreading android android-loadermanager android-handler
我用swipeview构建了一个音乐播放器项目(2个片段).一个片段有开始按钮,当我点击开始按钮时,另一个片段显示歌曲的元数据信息以及搜索栏.我的问题是在播放时通过一首歌获得当前时间.
片段的代码看起来像这样:
public static class SongParameterFragment extends Fragment {
public SeekBar seekBar;
static int fragmentNR;
public ImageView cover;
public TextView songParameter, songDuration;
runnableTimeline timelineMonitor;
public SongParameterFragment(int position) {
this.fragmentNR = position;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(com.example.swipeview3.R.layout.song_parameters, container, false);
cover = (ImageView)v.findViewById(com.example.swipeview3.R.id.imageView1);
songParameter = (TextView)v.findViewById(com.example.swipeview3.R.id.textView_SongParameter);
songDuration = (TextView)v.findViewById(com.example.swipeview3.R.id.textView_SongDuration);
seekBar = (SeekBar)v.findViewById(com.example.swipeview3.R.id.seekBar);
seekBar.setOnSeekBarChangeListener(seekBarChangeListener);
timelineMonitor = new runnableTimeline();
Thread thread = new Thread(timelineMonitor);
thread.start();
return v;
}
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new OnSeekBarChangeListener() { …Run Code Online (Sandbox Code Playgroud) android android-music-player android-resources android-handler
我在我的活动中修改以下代码:
new Handler().postDelayed(new Runnable() {
public void run() {
txtStatus.setText("hello");
}
}, 1000);
Run Code Online (Sandbox Code Playgroud)
至:
static Runnable myRunnable = new Runnable() {
public void run() {
txtStatus.setText("hello");
};
new Handler().postDelayed(myRunnable, 1000);
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,因为我们引用了一个非静态变量.
这也不起作用:
public void setText() {
txtStatus.setText("hello");
}
static Runnable myRunnable = new Runnable() {
public void run() {
setText(); // doesn't work
MyActivity.this.setText(); // still doesn't work
};
new Handler().postDelayed(myRunnable, 1000);
Run Code Online (Sandbox Code Playgroud)
那么我的初始示例如何被重写为使用静态类而不是匿名内部类(以避免内存泄漏的可能性)?
在我的Activity中我使用了一个处理程序
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
//do GUI stuff, edit views, etc..
}
};
Run Code Online (Sandbox Code Playgroud)
我也有一个Runnable,发布到PHP并等待json响应
Runnable runnable3 = new Runnable() {
public void run() {
JSONParserArray jsonParserRun = new JSONParserArray();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("p1", "myParam1"));
try {
String link = "http://mylink.com/someFileOnServer.php";
JSONArray jArray = jsonParserRun.makeHttpRequest(link , "POST",
params);
if (jArray != null && jArray.length() != 0) {
} else {
//either null (because of throwing exception) or empty …Run Code Online (Sandbox Code Playgroud) 处理程序是一个线程与否?如果是,我们如何从此Handler(线程)更新UI?
如果我们使用Looper概念,那么它是可能的.在这种情况下,它适用于任何线程吗?我对这些Threads,Handlers和Loopers非常困惑.有人可以用一个例子解释一下吗?处理程序是一个线程与否?如果是,我们如何从此Handler(线程)更新UI.
如果我们使用Looper概念,那么它可能适用于任何线程吗?我对这个Thread,Handler和Looper非常困惑.有人可以用一个例子解释一下吗?
问题" 线程和处理程序之间的区别是什么 "只是关于处理程序和线程,但不解释Loopers及其行为.并且接受的答案是"另一方面处理程序是允许您与UI线程通信的后台线程(更新UI)",但根据下面的"ben75"的答案,处理程序不是线程.因此,我不认为这是该问题的重复.
我用runnable旋转图像.我想旋转图像例如第4次然后暂停/停止rotate.i写了一些函数
public void rotateImage(final View myView, final int size) {
runnable = new Runnable() {
@Override
public void run() {
count++;
myView.setRotation(myView.getRotation() + size);
if (count ==3) {
myHandler.removeCallbacks(runnable);
}
myHandler.postDelayed(this, 100);
// 1000 means 1 second duration
}
};
myHandler.postDelayed(runnable, 100);
}
Run Code Online (Sandbox Code Playgroud)
我可以旋转图像,但我不能停止/暂停旋转.removeCallbacks目前不工作我的代码有什么问题,如果有人知道解决方案,请帮助我
在处理一个更大的项目时,我想显示一个带有时间延迟的 String 数组的所有元素(以便用户可读)。
在我的示例中,我有一个StringBuilder text在选项卡上拆分并存储在String[] words.
我最初的想法是使用类似的东西:
String[] words = text.toString().split("\t");
for (String word : words) {
textView.setText(word);
SystemClock.sleep(2000);
}
Run Code Online (Sandbox Code Playgroud)
但是,这会阻塞 UI 线程,因此只显示最后一个值。在阅读更多之后,我想出了这个解决方案(有效):
final String[] words = text.toString().split("\t");
final Handler handler = new Handler();
handler.post(new Runnable(){
@Override
public void run() {
textView.setText(words[i]);
i++;
if(i < words.length) {
handler.postDelayed(this, TIME_DELAY_MS);
}
}
});
Run Code Online (Sandbox Code Playgroud)
但是,这要求我将其i作为类的私有变量(在与处理程序相同的范围内声明它会强制它是最终的,为了遍历数组而不能这样做)。
为了这个目的而拥有一个类的私有变量似乎非常不雅 - 有没有更好的方法来做到这一点,我错过了?
我想创建一个新的线程onCreate并使用posta 来与UI线程进行通信View.但是,post似乎永远不会运行ed语句.这里有一个小例子:
import android.app.Activity
import android.os.Bundle
import android.widget.TextView
import kotlin.concurrent.*
import org.jetbrains.anko.*
class MainActivity: Activity(), AnkoLogger {
protected override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val view = TextView(this)
setContentView(view)
thread() {
info("before post")
view.post({ info("inside post") })
info("after post")
}
}
}
Run Code Online (Sandbox Code Playgroud)
查看日志,我只能看到before post和after post,但从来没有inside post.
我究竟做错了什么?
我们可以执行吗
handler.postDelayed(runnable,400)
Run Code Online (Sandbox Code Playgroud)
来自非Activity阶级?
我有一个控制器类,假设它是一个适配器。我们可以在那里使用吗Handler?
我尝试使用断点调试我的应用程序,但控制未到达
handler.postDelayed(runnable,400)
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决这个问题吗?
其实我用的是OCR。如果进行了某些匹配,我想自动返回到我的主要活动。我想它是一个循环器。我还需要拍摄它的照片。为此,我需要使用处理程序。
我在 android 项目中执行了这段代码Kotlin,它将记录两条消息。如果我将其更改token为Char,否则String它将仅打印一条消息,这是所需的行为。android 中的 java 项目中的相同用例可以正常工作。
val handler = Handler()
//val token1: Long = 1001L
//val token2: Int = 121
val token1: Long = 1001L
val token2: Int = 1002
handler.postAtTime(
{
Log.e("postAtTime 1", " printed 1 ")
handler.removeCallbacksAndMessages(token2)
},
token1,
SystemClock.uptimeMillis() + 2000
)
handler.postAtTime(
{
Log.e("postAtTime 2", " printed 2 ")
},
token2,
SystemClock.uptimeMillis() + 4000
)
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么在 Kotlin 中,对于 类型的标记Int,Long处理程序不会删除回调?
编辑 如果我尝试使用注释值,它会起作用
android ×10
android-handler ×10
java ×3
kotlin ×2
ui-thread ×2
arrays ×1
memory-leaks ×1
runnable ×1
timer ×1