android - 如何始终收听短信android

标签 android service sms broadcastreceiver

基本上,我正在尝试制作一个始终监听新SMS 的应用。 关键是,我制作了一个接收器,它将在设备启动时自动启动,它也可以监听新的 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);
    }
}

这是我的主类

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

最佳答案

注册您的 BroadcastReceiver在 list 中,使用 <receiver>元素,而不是使用 registerReceiver() .

注意:

  • 您仍然不会收到所有 SMS 消息,因为 SMS 广播是有序的,一些优先级高于您的接收者可以中止广播

  • 您的接收器首次安装在设备上时不会工作,直到用户启动您的 Activity ,或其他明确运行您的组件之一

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

关于android - 如何始终收听短信android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20954876/

相关文章:

android - windows下android模拟器768M以上如何分配内存?

java - Android Studio : Can I Refer to a Started Service in Java Code?

android - OKHttp 添加 header

android - 在 Android 自定义 ROM 中修改通话中的语音播放

WCF - EndpointNotFoundException,没有端点监听

calendar - 当我的日历有繁忙事件时,如何以编程方式将来电重定向到语音邮件

php - 从 PHP 发送短信

Android:如何从收件箱获取短信并删除特定短信

java - 'keytool' 未被识别为内部或外部命令

service - 如果未使用批处理文件安装服务,如何在 Windows 中检查