如何在安卓手机上始终监听短信

3

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

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

你在AndroidManifest.xml中定义了这个broadcastReceiver吗? - AndRSoid
3个回答

3

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

请注意:

  • 你仍然无法接收到所有短信,因为短信广播是有序的,某些优先级高于你的接收器可以终止广播

  • 在设备上首次安装你的接收器时,直到用户启动你的活动或其他组件明确运行你的一个组件之前,你的接收器将无法工作

此外,请注意Android 4.4显著改变了SMS处理方式,请牢记这一点。


0

0
你是否在AndroidManifest.xml中定义了这个BroadcastReceiver? 如果你是为系统事件注册的,就不需要在代码中使用接收器。当事件发生时,它会自动调用。因此,请将你的BroadcastReceiverService中移除,并将其放入AndroidManifest.xml中,然后放松一下。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接