您好我想打吐司
可我没有就事论事什么,可从任何每当我想在我的应用程序线程。因此,要做到这一点,我延长了活动
类:
Hi I want to make Toast
available to me no-matter-what and available from any thread whenever I like within my application. So to do this I extended the Activity
class:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
public class MyActivity extends Activity{
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
mHandler = new Handler();
super.onCreate(savedInstanceState);
}
private class ToastRunnable implements Runnable {
String mText;
public ToastRunnable(String text) {
mText = text;
}
public void run(){
Toast.makeText(getApplicationContext(), mText, Toast.LENGTH_SHORT).show();
}
}
public void doToast(String msg) {
mHandler.post(new ToastRunnable(msg));
}
}
让所有 活动
班我的应用程序,现在只是
so that all Activity
classes in my app are now simply
public class AppMain extends MyActivity {
//blah
}
我希望能够做到(在辅助线程)是这样的:
what I expected to be able to do (in a worker thread) was this:
try{
MyActivity me = (MyActivity) Looper.getMainLooper().getThread();
me.doToast("Hello World");
}
catch (Exception ex){
Log.e("oh dear", ex.getMessage());
}
和,只要活动
是 MyActivity
它应该工作 - 但问题是 - - >在 Looper.getMainLooper()getThread();
不是返回 MyActivity
来我和它的制作。我哭了 - 我究竟做错了什么?
and so long as the Activity
was a "MyActivity
" it should work - but the problem is ---> the Looper.getMainLooper().getThread();
isn't returning the MyActivity
to me and it's making me cry - what am I doing wrong?
:编辑:
一些背景来解释为什么我坚持这种类型的implmentation的。
some background to explain "why" I am stuck with this type of implmentation.
我需要能够确认到,一个的HTTP POST事件已经是成功的用户。现在。如果用户点击确定就用户界面形式,它可能或不可能有互联网,在那个时候。如果它有互联网 - 一切都很好 - 这职位通过HTTP POST形式的一切都很好 ..但如果有否网络 Android的大多数(99.999%的应用服务跛脚/可怜/ mewling这个,基本上为用户提供无计划B假设在任何时候互联网是没有 - 当它不是)
I need to be able to confirm to the user that a "HTTP POST" event has been a success. Now. If the User clicks "OK" on the UI Form it MAY or MAY NOT have internet at that time.. If it has Internet - all well and good - it posts the form via HTTP POST all well and good.. but if there is NO Internet most (99.999% of Android apps lame /pathetic / mewling at this, and basically offer the user no plan "b" assuming that at all times the internet is there - when it is NOT)
我的应用程序不会跛脚走(我称之为)在后台无提示线索。我有足够的用户交互遍布应用程序,我不知道在哪里,用户将是,但最终在HTTP POST的队列/重/队列/重试回报!成功了! 我想吐司
,作为一个信息给用户(如:你的形式发送)
My App will not "go lame (as I call it)" - it does have a plan "b" instead it "Queues" the post event and retries every x minutes.. now this is a silent thread in the background.. I have plenty of user interaction all over the app I don't know where the user will "be" but eventually when the HTTP POST that queue/retries/queue/retries returns "! Success! " I want to Toast
that as a message to the user (EG: "your form was sent")
有什么毛病 runOnUiThread
?
http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Hello, world!", Toast.LENGTH_SHORT).show();
}
});