如何在不活动创建广播接收器 [英] How to create BroadcastReceiver without Activity

查看:161
本文介绍了如何在不活动创建广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造条件,接收短信回应并显示一个对话框,一个应用程序。如何注册清单中的接收器无活动中定义?

我试图保持清单中的接收器/意向的过滤器标记出的活动​​代码,但由于没有推出活动模拟器将不会安装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:机器人=htt​​p://schemas.android.com/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>< /使用许可权>< /清单>


解决方案

你正在尝试做的是错的,至少有以下原因...


  1. MAIN / LAUNCHER仅适用于活动,并没有延伸活动在code类,这是什么原因造成的错误。

  2. 虽然没有什么错与应用这仅仅实现了一个BroadcastReceiver或服务,这是很好的做法,让用户知道事情已被正确初始化。即使你可以在所有应用列表中的接收器/服务,如果他们选择它,看看会发生什么他们是不会得到幸福 - 用户喜欢看一些反馈

总之,创建一个将出现在所有应用一个简单的活动,并有主/ 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="http://schemas.android.com/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...

  1. MAIN/LAUNCHER only apply to activities and as you don't have a class which extends Activity in your code, this is what is causing the error.
  2. Although there's nothing wrong with an 'app' which implements just a BroadcastReceiver or Service, it is good practice to let the user know that things have been initialized correctly. Even if you could list a receiver/service in 'All apps', if they select it and see nothing happens they're not going to be happy - users like to see some feedback.

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.

这篇关于如何在不活动创建广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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