简单的Android广播不显示敬酒 [英] Simple Android Broadcast does not show toast
问题描述
我只是想创造机器人的活动简单的广播,但它不工作。
I'm just trying to create a simple broadcast in Androids activity but its not working.
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent("android.intent.action.UMS_CONNECTED");
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.UMS_CONNECTED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_LONG).show();
}
};
registerReceiver(receiver, filter);
}
}
中的XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="usb.usbd"
android:versionCode="1"
android:versionName="1.0">
<uses-feature android:name="android.hardware.usb.host" />
<uses-sdk android:minSdkVersion="12" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".UsbddActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.UMS_CONNECTED" />
<action android:name="android.intent.action.UMS_DISCONNECTED" />
</intent-filter>
</activity>
</application>
</manifest>
的屏幕显示我的Xoom的,但敬酒不显示。 Xoom的是通过USB连接的,所以应该显示敬酒/
The screen shows on my Xoom but the toast does not display. The Xoom is connected through the USB so it should display the toast/
推荐答案
您无法启动对话框(且不应推出吐司)从在 广播接收器
。相反,有广播接收器
启动该做了另一个组件(活动/服务等)。要显示吐司
只是让另一个活动
并调用它,或作出方法上的电流活动
显示它并调用。接收器的整个想法是,你做的尽可能少的内部的onReceive
和摆脱,它的接线,它不是你做的工作。
You can't launch a dialog (and shouldn't launch a toast) from within a BroadcastReceiver
. Instead have the BroadcastReceiver
launch another component (Activity/Service, etc) that does something. TO show a Toast
just make another Activity
and invoke it, or make a method on your current Activity
that shows it and call that. The entire idea of a receiver is that you do as little as possible inside onReceive
and get out, it's wiring, it's not where you do work.
<一个href=\"http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29\"相对=nofollow>广播接收器的JavaDoc
的当广播接收器接收一个Intent广播时调用此方法。在此期间,您可以使用广播接收器的其他方法来查看/修改当前结果值。该功能通常被称为它的进程的主线程中,所以你永远不应该在它执行长时间运行的操作(有10秒的超时,系统考虑要阻挡的接收器和一个候选人被杀害之前允许的话) 。你不能在你的实现的onReceive()的启动弹出式对话框。的
您不能在执行的onReceive的推出弹出式对话框()
这篇关于简单的Android广播不显示敬酒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!