安卓+短信接收器+吐司=失败 [英] Android + SMS Receiver + Toast = FAIL

查看:90
本文介绍了安卓+短信接收器+吐司=失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让这个code工作在我的戴尔Streak,但是当我收到短信我还没有得到Toast通知......我已经添加在manifest.xml文件中的接收的标签。 ..我在这一个完整的小白和需要一点点帮助入门:)

 包net.learn2develop.SMSMessaging;

进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.telephony.SmsMessage;
进口android.widget.Toast;

公共类SMSReceiver扩展的BroadcastReceiver
{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图)
    {
        // ---得到通过短信---
        捆绑包= intent.getExtras();
        SmsMessage []封邮件= NULL;
        字符串str =;
        如果(捆绑!= NULL)
        {
            // ---检索收到的短信---
            [对象]的PDU =(对象[])bundle.get(的PDU);
            封邮件=新SmsMessage [pdus.length]
            的for(int i = 0; I< msgs.length;我++){
                封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
                STR + =短信来自+封邮件[I] .getOriginatingAddress();
                STR + =;
                STR + =封邮件[I] .getMessageBody()的toString()。
                STR + =\ N的;
            }
            // ---显示新短信---
            Toast.makeText(背景下,海峡,Toast.LENGTH_SHORT).show();
        }
    }
}
 

解决方案

您不应创建吐司 - 从非UI组件。您应该只创建吐司 - 从活动秒。之所以吐司没有显示是因为的BroadcastReceiver 不允许在用一个线程运行的尺蠖吐司依赖于它,其中的获得建立一个UI线程其他的东西。 通知取值旨在用于非UI组件来通知用户。

I am trying to get this code to work on my Dell Streak but when I receive an SMS I still dont get a Toast notification... I have added the 'receive' tag in the manifest.xml file... I am a complete noob at this and need a little help getting started :)

package net.learn2develop.SMSMessaging;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();       
        SmsMessage[] msgs = null;
        String str = "";           
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];           
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);               
                str += "SMS from " + msgs[i].getOriginatingAddress();                     
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";       
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }                         
    }
}

解决方案

You shouldn't create Toasts from non-UI components. You should only create Toasts from Activitys. The reason why the Toast isn't showing is because the BroadcastReceiver isn't running on a thread with a Looper and Toast depends on it, among other things that get setup for a UI thread. Notifications are meant for non-UI components to notify the user.

这篇关于安卓+短信接收器+吐司=失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆