Android的手机短信接收器不工作 [英] Android sms receiver doesnt work

查看:115
本文介绍了Android的手机短信接收器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜查了很多网站上和SO我找不到的答案,我的情况或一个简单的教程,我下面的此内容我的问题是构造没有得到调用,也没有的onReceive()。

I've searched a lot on web and on SO I couldn't find an answer to my case or a straightforward tutorial, I was following this my problem is that the constructor doesn't get call nor onReceive().

public class Smsreceiver extends BroadcastReceiver
{
    public Smsreceiver()
    {
        Log.v("constructing", "constructing");
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.v("received", "received");
        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_LONG).show();
           }
    }
} 

和清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dwaik.testreceivesms"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" >
    </uses-permission>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver
            android:name=".SmsReceiver"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

我建立针对API-15级(4.0.3)和XPERIA新V(4.0.3)上运行,我缺少什么?或者我会做什么特别的运行旁边让我的服务运行?

I'm building against api-level 15 (4.0.3) and running on xperia neo v (4.0.3), what am I missing? or shall I do anything special beside run to get my service to run?

修改

我找不到我的服务之间运行的应用程序菜单,虽然它安装的应用程序菜单上不存在

I can't find my service between running apps menu, though it does exist on installed apps menu

推荐答案

填写您的接收器在您的主要活动:

Register your receiver in your main activity:

String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

SmsReceiver smsReceiver = new SmsReceiver();

registerReceiver(smsReceiver, new IntentFilter(SMS_RECEIVED));

记住在结束注销了。

Remember to unregister it at the end.

这篇关于Android的手机短信接收器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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