有一个服务接收短信 [英] Having a Service receive SMS messages

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

问题描述

我已经看了几个短信的例子和活动通常用于接收短信。不过,我希望做的是有我的后台服务收到的短信(该服务将处理该消息,并决定是否适用于应用程序 - 再通知用户)

在我的清单中,服务定义如下:

 <服务机器人:名称=。service.myService
        机器人:启用=真正的>
        <意向滤光器>
            <作用机器人:名称=package.com.service.myService/>
        &所述; /意图滤光器>
< /服务>
 

有服务接收的短信,将这项工作?

 <接收器安卓名=机器人service.myService。:出口=真正的>
  <意图过滤器的Andr​​oid版本:优先=999>
    <作用机器人:名称=android.provider.Telephony.SMS_RECEIVED/>
  &所述; /意图滤光器>
< /接收器>
 

样品code我研究了来自:<一href="http://www.apriorit.com/our-company/dev-blog/227-handle-sms-on-android">http://www.apriorit.com/our-company/dev-blog/227-handle-sms-on-android

我无法测试它尚未因为我的开发模块没有发送短信的电话号码。

解决方案

我找到了解决办法。为了有一个服务接收短信:

  1. 更新清单,让您的应用程序来接收短信的权限 (WRITE_SMS,READ_SMS,RECEIVE_SMS)
  2. 请不要更新清单与接收器意图过滤器! (哪 每个样本code网上似乎做)
  3. 在你的服务,你的服务类中创建一个嵌套的BroadcastReceiver类

     私有类SMSreceiver扩展的BroadcastReceiver
    {
        私人最终字符串变量= this.getClass()getSimpleName()。
    
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图)
        {
            捆绑额外= intent.getExtras();
    
            字符串strMessage =;
    
            如果(临时演员!= NULL)
            {
                [对象] smsextras =(对象[])extras.get(的PDU);
    
                的for(int i = 0; I&LT; smsextras.length;我++)
                {
                    SmsMessage smsmsg = SmsMessage.createFromPdu((字节[])smsextras [I]);
    
                    。字符串strMsgBody = smsmsg.getMessageBody()的toString();
                    串strMsgSrc = smsmsg.getOriginatingAddress();
    
                    strMessage + = + strMsgSrc +的短信:+ strMsgBody;
    
                    Log.i(TAG,strMessage);
                }
    
            }
    
        }
    
    }
     

  4. 在服务类,登记领取 android.provider.Telephony.SMS_RECEIVED 意图过滤器:

     公共类ServiceCommunicator扩展服务
    {
        私人SMSreceiver mSMSreceiver;
        私人的IntentFilter mIntentFilter;
    
        @覆盖
        公共无效的onCreate()
        {
            super.onCreate();
    
            //短信事件接收器
            mSMSreceiver =新SMSreceiver();
            mIntentFilter =新的IntentFilter();
            mIntentFilter.addAction(android.provider.Telephony.SMS_RECEIVED);
            registerReceiver(mSMSreceiver,mIntentFilter);
        }
    
        @覆盖
        公共无效的onDestroy()
        {
            super.onDestroy();
    
            //注销短信接收器
            unregisterReceiver(mSMSreceiver);
        }
    }
     

这就是它!

注意: 包住你想知道为什么我没有从一个单独的BroadcastReceiver类中绑定到我的服务 - 这是行不通的,因为 bindService()不可用

I've looked at a few SMS message examples and Activities are typically used to receive an SMS. However, what I'd like to do is have my background service receive the SMS (the service will process the message and decide whether it is applicable to the app - then inform the user)

In my Manifest, the service is defined as follows:

    <service android:name=".service.myService"
        android:enabled="true">
        <intent-filter>
            <action android:name="package.com.service.myService"/>
        </intent-filter>
</service>

to have the service receive the SMS, will this work ?

<receiver android:name=".service.myService" android:exported="true" > 
  <intent-filter android:priority="999"> 
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-filter> 
</receiver>

The sample code I studied came from: http://www.apriorit.com/our-company/dev-blog/227-handle-sms-on-android

I can't test it yet because my development module doesn't have a phone number to send an SMS to.

解决方案

I found the solution. To have a Service receive SMS messages:

  1. Update the manifest to give your app the permissions to receive SMS (WRITE_SMS, READ_SMS, RECEIVE_SMS)
  2. DO NOT update the manifest with the receiver intent filter ! (which every sample code online seems to do)
  3. In your Service, create a nested BroadcastReceiver class within your Service class

    private class SMSreceiver extends BroadcastReceiver
    {
        private final String TAG = this.getClass().getSimpleName();
    
        @Override
        public void onReceive(Context context, Intent intent)
        {
            Bundle extras = intent.getExtras();
    
            String strMessage = "";
    
            if ( extras != null )
            {
                Object[] smsextras = (Object[]) extras.get( "pdus" );
    
                for ( int i = 0; i < smsextras.length; i++ )
                {
                    SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);
    
                    String strMsgBody = smsmsg.getMessageBody().toString();
                    String strMsgSrc = smsmsg.getOriginatingAddress();
    
                    strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;                    
    
                    Log.i(TAG, strMessage);
                }
    
            }
    
        }
    
    }
    

  4. In your Service class, register to receive the android.provider.Telephony.SMS_RECEIVED intent filter :

    public class ServiceCommunicator extends Service
    {
        private SMSreceiver mSMSreceiver;
        private IntentFilter mIntentFilter;
    
        @Override
        public void onCreate()
        {
            super.onCreate();
    
            //SMS event receiver
            mSMSreceiver = new SMSreceiver();
            mIntentFilter = new IntentFilter();
            mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
            registerReceiver(mSMSreceiver, mIntentFilter);
        }
    
        @Override
        public void onDestroy()
        {
            super.onDestroy();
    
            // Unregister the SMS receiver
            unregisterReceiver(mSMSreceiver);
        }
    }
    

That's it !

note: encase you're wondering why I didn't bind to my service from within a separate BroadcastReceiver class - it doesn't work because bindService() isn't available.

这篇关于有一个服务接收短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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