AlertDialog从内部BroadcastReceiver的?能不能做到?我工作的一个应用程序,会弹出一个对话框,如果我收到短信。我想code这是一个BroadcaseReceiver之内。但我不能使用此行code AlertDialog.Builder建设者=新AlertDialog.Builder(本);
。是否有人可以帮我一个提示!
公共类SMSPopU preceiver延伸的BroadcastReceiver {
私有静态最后弦乐LOG_TAG =SMSReceiver;
公共静态最终诠释NOTIFICATION_ID_RECEIVED = 0x1221;
静态最后弦乐ACTION =android.provider.Telephony.SMS_RECEIVED;
公共无效的onReceive(上下文的背景下,意图意图){
Log.i(LOG_TAG的onReceive);
如果(intent.getAction()。等于(SMSPopU preceiver.ACTION)){
StringBuilder的SB =新的StringBuilder();
捆绑包= intent.getExtras();
如果(捆绑!= NULL){
[对象]的PDU =(对象[])bundle.get(的PDU);
对于(对象PDU:PDU)的{
SmsMessage消息=
SmsMessage.createFromPdu((byte []的)PDU);
sb.append(接收SMS \ BackSlasHn发件人:);
sb.append(messages.getDisplayOriginatingAddress());
sb.append(\ñ----留言---- \ N);
sb.append(messages.getDisplayMessageBody());
}
}
Log.i(SMSPopU preceiver.LOG_TAG,
[SMSApp] onReceiveIntent:+ SB);
Toast.makeText
(上下文,sb.toString(),Toast.LENGTH_LONG).show();
}
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setMessage(你确定要退出吗?)
.setCancelable(假)
.setPositiveButton(是,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
dialog.cancel();
}
})
.setNegativeButton(否,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
dialog.cancel();
}
});
AlertDialog警报= builder.create();
}
}
解决方案
主要问题:尽量避免将耗时的功能组合成的BroadcastReceiver。它应该只是在绑定活动/服务接收并开始进一步处理。
更新:
请检查下面可能有帮助的来源:
在StackOverflow的类似问题:
How从BroadcastReceiver的android系统中发送数据到一个活动?
的Android手机短信接收器不工作
Android SDK中演示的例子:
android-sdk-windows\samples\android-8\ApiDemos\src\com\example\android\apis\os\SmsMessagingDemo.java
和课程标准的Android API文档:http://developer.android.com/reference/android/content/BroadcastReceiver.html
UPDATE2:
添加应用程序框架,它应该是。请注意,没有内容视图定义。这是因为你的应用程序将有透明屏幕。为了实现这一目标
@android:款式/ Theme.Translucent
根据主题标签进入在AndroidManifest.xml中这一活动。
公共类NotifySMSReceived扩展活动
{
私有静态最后弦乐LOG_TAG =SMSReceiver;
公共静态最终诠释NOTIFICATION_ID_RECEIVED = 0x1221;
静态最后弦乐ACTION =android.provider.Telephony.SMS_RECEIVED;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
IntentFilter的过滤器=新的IntentFilter(ACTION);
this.registerReceiver(mReceivedSMSReceiver,过滤器);
}
私人无效displayAlert()
{
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setMessage(你确定要退出吗?)。setCancelable(
假).setPositiveButton(是,
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
dialog.cancel();
}
})。setNegativeButton(否,
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
dialog.cancel();
}
});
AlertDialog警报= builder.create();
alert.show();
}
私人最终的BroadcastReceiver mReceivedSMSReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
串动= intent.getAction();
如果(ACTION.equals(动作))
{
//您的短信处理code
displayAlert();
}
}
};
}
AlertDialog from within BroadcastReceiver? Can it be done? I am working on a app that will pop up a Dialog box if I get SMS message. I am trying to code this within a BroadcaseReceiver. But I cant use this line of code AlertDialog.Builder builder = new AlertDialog.Builder(this);
. Can someone please help me with a hint!
public class SMSPopUpReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "SMSReceiver";
public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "onReceive");
if (intent.getAction().equals(SMSPopUpReceiver.ACTION)) {
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (Object pdu : pdus){
SmsMessage messages =
SmsMessage.createFromPdu((byte[]) pdu);
sb.append("Received SMS\nFrom: ");
sb.append(messages.getDisplayOriginatingAddress());
sb.append("\n----Message----\n");
sb.append( messages.getDisplayMessageBody());
}
}
Log.i(SMSPopUpReceiver.LOG_TAG,
"[SMSApp] onReceiveIntent: " + sb);
Toast.makeText
(context, sb.toString(), Toast.LENGTH_LONG).show();
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
}
}
解决方案
Principal issue: try to avoid placing time consuming functionalities into BroadcastReceiver. It should just receive and initiate further processing in bound Activity/Service.
UPDATE:
Please check following sources that might be helpful:
Similar questions on StackOverflow:
How to send data from BroadcastReceiver to an Activity in android?
Android SMS receiver not working
Android SDK demo example:
android-sdk-windows\samples\android-8\ApiDemos\src\com\example\android\apis\os\SmsMessagingDemo.java
And of course standard Android API documentation: http://developer.android.com/reference/android/content/BroadcastReceiver.html
UPDATE2:
Added app skeleton as it should look. Please note that no content view is defined. It is because your app will have transparent screen. To achieve that
@android:style/Theme.Translucent
is entered under Theme tag for this activity in AndroidManifest.xml.
public class NotifySMSReceived extends Activity
{
private static final String LOG_TAG = "SMSReceiver";
public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION);
this.registerReceiver(mReceivedSMSReceiver, filter);
}
private void displayAlert()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?").setCancelable(
false).setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private final BroadcastReceiver mReceivedSMSReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION.equals(action))
{
//your SMS processing code
displayAlert();
}
}
};
}