Android的手机短信接收器不工作

编程入门 行业动态 更新时间:2024-10-12 05:54:24
本文介绍了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="schemas.android/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的手机短信接收器不工作

本文发布于:2023-11-27 12:57:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638176.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收器   手机短信   工作   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!