我已经搜查了很多网站上和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的手机短信接收器不工作
发布评论