如何经常收听短信安卓android

时间:2014-01-06 16:46:01

标签: android service sms broadcastreceiver

基本上,我正在尝试创建一个始终收听新SMS的应用。 关键是,我已经制作了一个接收器,它将在设备启动时自动启动,它也可以监听新的短信,但只有当应用程序打开时,如果我关闭它,那么它就不再起作用了。 这是我的服务调用BroadCastReceiver:

public class ServiceCommunicator extends Service {
private SMSReceiver mSMSreceiver;
private IntentFilter mIntentFilter;
private boolean receiverAlive = false;

@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);
}

public class SMSReceiver extends BroadcastReceiver {
    public SmsMessage messages[] = null;

    private void showNotification(Context context, String sms) {
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("My notification")
                        .setContentText(sms);
        mBuilder.setContentIntent(contentIntent);

        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        try {
            if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
                Bundle bundle = intent.getExtras();
                messages = null;
                if (bundle != null) {
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    String sms = "";
                    String mobile = "34821049283";

                    for (int i = 0; i < pdus.length; i++) {
                        SmsMessage tmp = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        String senderMobile = tmp.getOriginatingAddress();

                        if (senderMobile.equals(mobile)) {
                            sms = tmp.getMessageBody();
                            showNotification(context, sms);

                            break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            Log.d("Exception caught", e.getMessage());
        }

        receiverAlive = false;
        mSMSreceiver = new SMSReceiver();
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(mSMSreceiver, mIntentFilter);
    }
}

这是我的MainClass

public class MainActivity extends Activity {
    private static SmsMessage[] messages = null;
    public static Activity mActivity = null;
    public static String mobile = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart(){
        super.onStart();

        mActivity = this;
        mobile = ((EditText) findViewById(R.id.txtMobile)).getText().toString();

        Intent intent = new Intent(this, ServiceCommunicator.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startService(intent);
    }
}

3 个答案:

答案 0 :(得分:3)

使用BroadcastReceiver元素在清单中注册<receiver>,而不是使用registerReceiver()

请注意:

  • 您仍然无法收到所有短信,因为订购了短信广播,而某些优先级高于您的接收方可以中止广播

  • 首次安装在设备上时,您的接收器将无法工作,直到用户启动您的活动或其他显式运行您的某个组件

另请注意Android 4.4 significantly overhauled how the SMS process works,请记住这一点。

答案 1 :(得分:0)

尝试查看此GIT项目

https://github.com/vivdub/DynamicSMSReceiver

这个就是你想做的......

答案 2 :(得分:0)

您是否在BroadcastReceiver中定义了此AndroidManifest.xml? 如果要注册系统事件,则无需在代码中使用接收器。事件发生时会自动调用它。因此,请移除BroadcastReceiver表单Service并将其放入AndroidManifest.xml,然后放松一下。