有新短信到达时启动广播接收器

时间:2013-03-25 04:59:47

标签: android sms broadcastreceiver

我已经创建了一个远程服务,它正在呼叫广播接收器,而广播接收器又调用警报管理器来触发警报,但是我希望在新消息到达时通过rempte服务触发接收器

这段代码如下:

     @Override
                public IBinder onBind(Intent arg0) {
                    Log.d(getClass().getSimpleName(), "onBind()");

                    return myRemoteServiceStub;
                }           

                    private IMyRemoteService.Stub myRemoteServiceStub = new IMyRemoteService.Stub() {
                        public int getCounter() throws RemoteException {
                //          startAlarmNotification();

                            IntentFilter filter = new IntentFilter();
                            filter.addAction("android.provider.Telephony.SMS_RECEIVED");
                            filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
                            filter.addAction("your_action_strings"); //further more
                            filter.addAction("your_action_strings"); //further more

                            registerReceiver(receiver, filter);
                            return counter;
                        }
                    };

                    @Override
                    public void onCreate() {
                        super.onCreate();
                        Log.d(getClass().getSimpleName(),"onCreate()");
                    }

                    @Override
                    public void onDestroy() {
                        super.onDestroy();
                        serviceHandler.removeCallbacks(myTask);
                        serviceHandler = null;
                        Log.d(getClass().getSimpleName(),"onDestroy()");
                    }

                    @Override
                    public void onStart(Intent intent, int startId) {
                        super.onStart(intent, startId);
                        serviceHandler = new Handler();
                        serviceHandler.postDelayed(myTask, 1000L);
                        Log.d(getClass().getSimpleName(), "onStart()");
                    }

                    class Task implements Runnable {
                        public void run() {
                            ++counter;
                            serviceHandler.postDelayed(this,1000L);
                            Log.i(getClass().getSimpleName(), "Incrementing counter in the run method");
                        }
                    }
                    public void  startAlarmNotification(){
                      Calendar calendar = Calendar.getInstance();
                      calendar.add(Calendar.MINUTE, 1);
                      Intent alarmintent = new Intent(RemoteService.this, AlarmReciver.class);
                      PendingIntent sender = PendingIntent.getBroadcast(this, notificationId, alarmintent,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
                      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                //        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),  60 * 1000, sender);
                          am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
                    }


                    private final BroadcastReceiver receiver = new BroadcastReceiver() {
                           @Override
                           public void onReceive(Context context, Intent intent) {
                              String action = intent.getAction();
                              if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
                                //action for sms received
                                        startAlarmNotification();
                                        unregisterReceiver(receiver);


                              }
                              else if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
                                   //action for phone state changed
                              }     
                           }
                        };

1 个答案:

答案 0 :(得分:1)

如果您的服务正在运行,您的代码正在运行...因此,只要您收到短信,就会运行以下代码。你可以修改它,就像收到短信后,你可以开始服务并将你的代码放入服务中。

SmsReceiver JAVA Class

    public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

           Bundle extras = intent.getExtras();
           if (extras == null)
           return;

          // To display a Toast whenever there is an SMS.
           Toast.makeText(context,"Recieved",Toast.LENGTH_LONG).show();

           Object[] pdus = (Object[]) extras.get("pdus");
           for (int i = 0; i < pdus.length; i++) {
              SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
              String sender = SMessage.getOriginatingAddress();
              String body = SMessage.getMessageBody().toString();
              Toast.makeText(context, ""+sender+":"+body , Toast.LENGTH_SHORT)
                        .show();

            }
           startAlarmNotification();
         }

        public void  startAlarmNotification(){
                      Calendar calendar = Calendar.getInstance();
                      calendar.add(Calendar.MINUTE, 1);
                      Intent alarmintent = new Intent(RemoteService.this, AlarmReciver.class);
                      PendingIntent sender = PendingIntent.getBroadcast(this, notificationId, alarmintent,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
                      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                //        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),  60 * 1000, sender);
                          am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
                    }
     }

在您的清单中执行以下代码:

<receiver android:name=".SmsReceiver">
                <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>

还添加以下权限:

<uses-permission android:name="android.permission.RECEIVE_SMS" />