Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓&x2B;短信接收器+;土司=失败_Android_Toast - Fatal编程技术网

Android 安卓&x2B;短信接收器+;土司=失败

Android 安卓&x2B;短信接收器+;土司=失败,android,toast,Android,Toast,我正试图让此代码在我的戴尔Streak上运行,但当我收到短信时,我仍然没有收到祝酒词通知。。。我已在manifest.xml文件中添加了“receive”标记。。。在这方面我是个十足的傻瓜,需要一点帮助来开始:) package net.learn2develope.sms消息; 导入android.content.BroadcastReceiver; 导入android.content.Context; 导入android.content.Intent; 导入android.os.Bundle

我正试图让此代码在我的戴尔Streak上运行,但当我收到短信时,我仍然没有收到祝酒词通知。。。我已在manifest.xml文件中添加了“receive”标记。。。在这方面我是个十足的傻瓜,需要一点帮助来开始:)

package net.learn2develope.sms消息;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.sms消息;
导入android.widget.Toast;
公共类SMSReceiver扩展了BroadcastReceiver
{
@凌驾
公共void onReceive(上下文、意图)
{
//---获取传入的SMS消息---
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];

对于(int i=0;i您不应该从非UI组件创建
Toast
s。您应该只从
Activity
s创建
Toast
s。之所以没有显示
Toast
是因为
BroadcastReceiver
没有在带有
Looper
的线程上运行,
Toast
取决于它,除此之外为UI线程设置的事件。用于非UI组件通知用户

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();
        }                         
    }
}