我想创造条件,接收短信回应并显示一个对话框,一个应用程序。如何注册清单中的接收器无活动中定义?
我试图保持清单中的接收器/意向的过滤器标记出的活动代码,但由于没有推出活动模拟器将不会安装APK。保持的BroadcastReceiver为主要活动的结果无法实例化活动的错误在logcat中。
任何帮助吗?
谢谢,阳光明媚
Receiver类
公共类SMSReceiver扩展广播接收器{ // onCreat接收到短信时被调用。 //消息经由包附接到意图,存储在对象 //数组中的PDU格式。 公共无效的onReceive(上下文的背景下,意图意图){ //从包传递的短信 捆绑包= intent.getExtras(); 串bodyText的=; 从=字符串; 如果(捆绑!= NULL){ //获得对象数组内短信 [对象]的PDU =(对象[])bundle.get(的PDU); SmsMessage [] =封邮件新SmsMessage [pdus.length] 的for(int i = 0; I< msgs.length;我++) 封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]); 对于(SmsMessage消息:消息){ bodyText的= message.getMessageBody(); 从= + message.getOriginatingAddress(),从消息+: } //在弹出消息显示 Toast.makeText(上下文,从+ bodyText的,Toast.LENGTH_SHORT).show(); } }}清单
<?XML版本=1.0编码=UTF-8&GT?;<清单的xmlns:机器人=http://schemas.android/apk/res/android包=home.splttingatms.SMSReceiver安卓版code =1机器人:=的versionName1.0 > <应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME> <活动机器人:SMSReceiverNAME = 机器人:标签=@字符串/ APP_NAME> &所述;意图滤光器> <作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/> &所述; /意图滤光器> < /活性GT; <接收机器人:SMSReceiverNAME => &所述;意图滤光器> <作用机器人:名字=android.provider.Telephony.SMS_RECEIVED/> &所述; /意图滤光器> < /接收器> < /用途> <采用-SDK安卓的minSdkVersion =7/> <使用许可权的android:NAME =android.permission.RECEIVE_SMS>< /使用许可权>< /清单>解决方案
你正在尝试做的是错的,至少有以下原因...
总之,创建一个将出现在所有应用一个简单的活动,并有主/ LAUNCHER意图设置,并在启动时只是在用它创建一个对话框说像你好,欢迎来到...或一些其他反馈给用户,让他们知道,事情已经正确启动。有对话框上的确定按钮,当pressed,调用该活动的完成()方法离开接收器的地方。
I would like to create a application that would respond to receiving SMS messages and display a dialog. How can I register the receiver in the manifest without defining within an activity?
I tried to keep the receiver/intent-filter tags in the manifest out of the activity tag but the emulator will not install the apk since there is no launch activity. Keeping the BroadcastReceiver as the main activity results in an "Unable to instantiate activity" error in the Logcat.
Any help?
Thanks, Sunny
Receiver class
public class SMSReceiver extends BroadcastReceiver { // onCreat is invoked when an sms message is received. // Message is attached to Intent via Bundle, stored in an Object // array in the PDU format. public void onReceive(Context context, Intent intent) { // get the SMS message passed in from Bundle Bundle bundle = intent.getExtras(); String bodyText = ""; String from = ""; if (bundle != null) { //Retrieve sms message within Object array Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage[] msgs = new SmsMessage[pdus.length]; for (int i=0; i < msgs.length; i++) msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); for (SmsMessage message: msgs) { bodyText = message.getMessageBody(); from = "Message from " + message.getOriginatingAddress() + ": "; } // Display message in pop up Toast.makeText(context, from + bodyText, Toast.LENGTH_SHORT).show(); } } }Manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="schemas.android/apk/res/android" package="home.splttingatms.SMSReceiver" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SMSReceiver" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".SMSReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> </manifest>解决方案
What you are trying to do is wrong for at least the following reasons...
In short, create a simple Activity which will appear in 'All apps' and has the MAIN/LAUNCHER intent settings and when it starts just have it create a dialog saying something like "Hi, welcome to ..." or some other feedback to the user letting them know that things have started correctly. Have an 'OK' button on the dialog which when pressed, calls the Activity's finish() method leaving the receiver in place.
更多推荐
如何在不活动创建广播接收器
发布评论