How to show a toast in Android Framework service.
在Framework里添加Toast就需要注意了,例如加在一个Service里,当希望应用调用 Service API 时提示个Toast:错误的方法:将Toast添加到API的实现里, 运行时不能显示此Toast。
正确的方法:必须将Toast添加到主线程里才能显示,例如在HandleMessage里增加这个Toast。
Message msg = mHandler.obtainMessage(EVENT_SHOW_A_TOAST);
msg.obj = toast;
mHandler.sendMessage(msg);
如果将Toast作为一个对象传送到HandleMessage里也会有问题:
08-05 10:52:42.326 E/JavaBinder( 1219): *** Uncaught remote exception! (Exceptions are not yet supported across processes.)
08-05 10:52:42.326 E/JavaBinder( 1219): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-05 10:52:42.326 E/JavaBinder( 1219): at android.os.Handler.<init>(Handler.java:121)
08-05 10:52:42.326 E/JavaBinder( 1219): at android.widget.Toast.<init>(Toast.java:68)
08-05 10:52:42.326 E/JavaBinder( 1219): at android.widget.Toast.makeText(Toast.java:231)
08-05 10:52:42.326 E/JavaBinder( 1219): at com.android.server.ConnectivityService.startUsingNetworkFeature(ConnectivityService.java:)
08-05 10:52:42.326 E/JavaBinder( 1219): at android.net.IConnectivityManager$Stub.onTransact(IConnectivityManager.java:)
08-05 10:52:42.326 E/JavaBinder( 1219): at android.os.Binder.execTransact(Binder.java:320)
08-05 10:52:42.326 E/JavaBinder( 1219): at dalvik.system.NativeStart.run(Native Method)
所以一定要添加到主线程里才可以正常显示:
case EVENT_SHOW_A_TOAST:
{
CharSequence strNew = (CharSequence)msg.obj;
CharSequence text = strNew;
int duration = Toast.LENGTH_SHORT;
//int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(mContext, text, duration);
toast.show();
break;
}
========================================================================
下面是Google对Toast使用说明:
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
Creating Toast Notifications
A toast notification is a message that pops up on the surface of the window.It only fills the amount of space required for the message and the user's currentactivity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events.
The screenshot below shows an example toast notification from the Alarm application.Once an alarm is turned on, a toast is displayed to assure you that the alarm was set.
A toast can be created and displayed from an Activity
orService
. If you create a toast notification from a Service, itappears in front of the Activity currently in focus.
If user response to the notification is required, consider using a Status Bar Notification.
The Basics
First, instantiate a Toast
object with one of themakeText()
methods.This method takes three parameters: the applicationContext
,the text message, and the duration for the toast. It returns a properly initialized Toastobject. You can display the toast notification withshow()
,as shown in the following example:
Context context = getApplicationContext(); CharSequence text = "Hello toast!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show();
This example demonstrates everything you need for most toast notifications.You should rarely need anything else. You may, however, want to position the toast differently or even use your own layout instead of a simple text message. The following sections describe how you can do these things.
You can also chain your methods and avoid holding on to the Toast object, like this:
Toast.makeText(context, text, duration).show();
Positioning your Toast
A standard toast notification appears near the bottom of the screen, centered horizontally.You can change this position with thesetGravity(int, int, int)
method. This accepts three parameters: aGravity
constant, an x-position offset, and a y-position offset.
For example, if you decide that the toast should appear in the top-left corner, you can set thegravity like this:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter.
Creating a Custom Toast View
If a simple text message isn't enough, you can create a customized layout for yourtoast notification. To create a custom layout, define a View layout,in XML or in your application code, and pass the rootView
objectto thesetView(View)
method.
For example, you can create the layout for the toast visible in the screenshot to the rightwith the following XML (saved astoast_layout.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:background="#DAAA" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /> </LinearLayout>
Notice that the ID of the LinearLayout element is "toast_layout". You must use thisID to inflate the layout from the XML, as shown here:
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello! This is a custom toast!"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();