onBroadcastReceiver 是否在后台工作?

发布于 2025-01-04 17:59:59 字数 2631 浏览 3 评论 0原文

我有一个自动回复短信应用程序。其功能是当短信到来时,应用程序自动回复繁忙消息给发件人。 我还创建了一个活动。在此活动中,我有一个广播接收器方法,当短信到来时,该方法使用 MessageManager 发送结果短信。但这个应用程序在后台运行时似乎无法运行。我该怎么办。

这是我使用的代码:

public class AutoReplySMSliteActivity extends Activity {

    //-----------------------------------------------------RECEIVER LISTENER----------------------------------------------
    //Create auto- reply
    private void sendSMS(String cellNo, String msg)
    {
        SmsManager sender = SmsManager.getDefault();
        sender.sendTextMessage(cellNo, null, msg, null, null);
    }

    //Setup sender number which is got from receiver
    IntentFilter intentFilter;

    private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

    };

    //-------------------------------------------------------onCreate METHOD-----------------------------------------------------------
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        //Setup broadcast receiver
        intentFilter = new IntentFilter();
        intentFilter.addAction("SMS_RECEIVED_ACTION");
        registerReceiver(intentReceiver, intentFilter);




}

在 androidmanifest 中,我有一个接收器:

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

那是我的接收器类:

public class SMSReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    //Get the sender phone number and bind to string str
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if (bundle != null) {
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            str += msgs[i].getOriginatingAddress();
        }
        Log.i("SMSReceiver", "Sender number receives: " + str);

        //Send broadcast str to ListReceiver to compare
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("SMS_RECEIVED_ACTION");
        broadcastIntent.putExtra("sms", str);
        context.sendBroadcast(broadcastIntent);
        Log.i("SMSReceiver", "Broadcast data....");
    }
}

}

I have an auto reply sms application. The function is that when a sms is coming, the application auto reply a busy message to the sender.
I also create a activity. In this activity I have a broadcastReceiver method, when a sms is coming, this method uses MessageManager to send an outcome sms. But it seems like this application can not work when it is in the background. What do I have to do.

Here is the code I used:

public class AutoReplySMSliteActivity extends Activity {

    //-----------------------------------------------------RECEIVER LISTENER----------------------------------------------
    //Create auto- reply
    private void sendSMS(String cellNo, String msg)
    {
        SmsManager sender = SmsManager.getDefault();
        sender.sendTextMessage(cellNo, null, msg, null, null);
    }

    //Setup sender number which is got from receiver
    IntentFilter intentFilter;

    private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

    };

    //-------------------------------------------------------onCreate METHOD-----------------------------------------------------------
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        //Setup broadcast receiver
        intentFilter = new IntentFilter();
        intentFilter.addAction("SMS_RECEIVED_ACTION");
        registerReceiver(intentReceiver, intentFilter);




}

in androidmanifest, I have a receiver:

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

And that is my Receiver class:

public class SMSReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    //Get the sender phone number and bind to string str
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if (bundle != null) {
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            str += msgs[i].getOriginatingAddress();
        }
        Log.i("SMSReceiver", "Sender number receives: " + str);

        //Send broadcast str to ListReceiver to compare
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("SMS_RECEIVED_ACTION");
        broadcastIntent.putExtra("sms", str);
        context.sendBroadcast(broadcastIntent);
        Log.i("SMSReceiver", "Broadcast data....");
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑轻吟梦一曲 2025-01-11 17:59:59

我认为您需要在收到消息后致电服务人员。您应该将所有功能移至 Service 。所以它也在后台运行。

I think you need to call Service when the message comes. You should move all the functionality to Service . So it also runs in the background .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文