[英]How to display toast inside timer?
我想在計時器中顯示toast消息,我使用了以下代碼:
timer.scheduleAtFixedRate( new TimerTask()
{
public void run()
{
try {
fun1();
} catch (Exception e) {e.printStackTrace(); }
}
}, 0,60000);
public void fun1()
{
//want to display toast
}
我收到以下錯誤:
WARN / System.err(593):java.lang.RuntimeException:無法在未調用Looper.prepare()的線程內創建處理程序
WARN / System.err(593):在android.os.Handler。(Handler.java:121)
WARN / System.err(593):在android.widget.Toast。(Toast.java:68)
WARN / System.err(593):在android.widget.Toast.makeText(Toast.java:231)
謝謝。
您不能在單獨的Thread內部進行UI更新,例如Timer。 您應該使用Handler對象進行UI更新:
timer.scheduleAtFixedRate( new TimerTask() {
private Handler updateUI = new Handler(){
@Override
public void dispatchMessage(Message msg) {
super.dispatchMessage(msg);
fun1();
}
};
public void run() {
try {
updateUI.sendEmptyMessage(0);
} catch (Exception e) {e.printStackTrace(); }
}
}, 0,60000);
最簡單的方法(IMO)是:
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
final String message = "Hi";
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MyActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
});
關鍵是MyActivity.this.runOnUiThread(Runnable)。
我想制作一個可以在計時器中顯示Toast的簡單項目。 Timer將使用服務啟動。 然后,Timer在服務啟動時啟動,在服務停止時停止。
1級
package com.example.connect;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button button1,button2;
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
}
public void Start(View v)
{
startService(new Intent(MainActivity.this , Connect_service.class));
}
public void Stop(View v)
{
stopService(new Intent(MainActivity.this , Connect_service.class));
}
}
2級
package com.example.connect;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class Connect_service extends Service{
Timer timer = new Timer();
TimerTask updateProfile = new CustomTimerTask(Connect_service.this);
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
timer.scheduleAtFixedRate(updateProfile, 0, 5000);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
timer.cancel();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
3級
package com.example.connect;
import java.util.TimerTask;
import android.content.Context;
import android.os.Handler;
import android.widget.Toast;
public class CustomTimerTask extends TimerTask {
private Context context;
private Handler mHandler = new Handler();
public CustomTimerTask(Context con) {
this.context = con;
}
@Override
public void run() {
new Thread(new Runnable() {
public void run() {
mHandler.post(new Runnable() {
public void run() {
Toast.makeText(context, "In Timer", Toast.LENGTH_SHORT).show();
}
});
}
}).start();
}
}
你必須打電話給UIThread來展示Toast。 不是來自計時器線程。
else從該計時器線程調用UI線程。
這個鏈接可以幫到你,
http://developer.android.com/resources/articles/timed-ui-updates.html
還有這個
http://developer.android.com/guide/appendix/faq/commontasks.html#threading
謝謝。
創建一個Handler
並在其中顯示吐司
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// Toast here
}
};
您需要訪問應用程序的上下文才能執行此操作。 嘗試創建自己的類,將上下文作為輸入參數:
private class MyTimerTask extends TimerTask {
private Context context;
public MyTimerTask(Context context) {
this.context = context;
}
@Override
public void run() {
Toast.makeText(context, "Toast text", Toast.LENGTH_SHORT).show();
}
}
然后在你的計時器:
timer.scheduleAtFixedRate( new MyTimerTask(this), 0,60000);
我正試圖用自己的觀點做自己的祝酒。
我成功地結合了你的方法。 下面的代碼允許我顯示toasts並更改/刪除視圖而不會崩潰,只需將MyTimerTask
構造函數的參數更改為您需要處理的任何內容。
public void yourFunction(){
Timer timer = new Timer();
MyTimerTask mtc = new MyTimerTask(this.getContext(), tvNotice);
timer.schedule(mtc, 1000);
}
private class MyTimerTask extends TimerTask {
private TextView tv;
private Context context;
public MyTimerTask(Context pContext, TextView pTv) {
this.tv = pTv;
this.context = pContext;
}
@Override
public void run() {
updateUI.sendEmptyMessage(0);
}
private Handler updateUI = new Handler(){
@Override
public void dispatchMessage(Message msg) {
super.dispatchMessage(msg);
tv.setText("TextView Message");
Toast.makeText(context, "Toast Message", 0).show();
}
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.