Android 如何始终收听安卓手机短信

Android 如何始终收听安卓手机短信,android,service,sms,broadcastreceiver,Android,Service,Sms,Broadcastreceiver,基本上,我正在尝试制作一个总是收听新的短信的应用程序。 关键是,我做了一个接收器,它会在设备启动时自动启动,它也可以监听新的短信,但只有当应用程序打开,如果我关闭它,它才会停止工作。 这是我呼叫广播接收器的服务: public class ServiceCommunicator extends Service { private SMSReceiver mSMSreceiver; private IntentFilter mIntentFilter; private boolean receiv

基本上,我正在尝试制作一个总是收听新的
短信的应用程序。
关键是,我做了一个接收器,它会在设备启动时自动启动,它也可以监听新的短信,但只有当应用程序打开,如果我关闭它,它才会停止工作。
这是我呼叫广播接收器的服务:

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

尝试查看此GIT项目


这一个正好做你想要的…

你在
AndroidManifest.xml
中定义了这个
BroadcastReceiver
吗?
若要注册系统事件,则不需要在代码中使用receiver。当事件发生时,将自动调用它。因此,请删除您的
BroadcastReceiver
表单
服务
并将其放入
AndroidManifest.xml
中,然后冷静下来。

使用
元素在清单中注册您的
BroadcastReceiver

请注意:

  • 您仍然无法获得所有SMS消息,因为SMS广播已订购,并且某些优先级高于您的接收器可以中止广播

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


另外,请注意,请记住这一点。

您是否在AndroidManifest.xml中定义了此broadcastReceiver。
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);
    }
}