android:即使应用已关闭,也会收到短信 [英] android: receive sms even if app is closed

查看:148
本文介绍了android:即使应用已关闭,也会收到短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,即使关闭该应用程序也能正常工作。这是我编写的用于接受传入SMS的代码。

I am writing an app, which should work even after the app is closed. Here is the code which I wrote to accept the incoming SMS.

public class SMSReceiver extends BroadcastReceiver {

    private ResultReceiver receiver;
    private Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;

        // Parse the SMS.
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        if (bundle != null)
        {
            // Retrieve the SMS.
            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 += "SMS from " + msgs[i].getOriginatingAddress();
                str += " :\n";
                str += " :\n";
                str += "MessageBody: ";
                str += msgs[i].getMessageBody();

            }
            Log.i("SmsReceiver message := " + str);
        }
    }
}

我已经注册了此广播,在我的主要活动中。并且我没有注销广播。

I had registered this Broadcast, in the my main activity. and I am not unregistering broadcast.

此功能在启动我的应用程序时有效,但在应用程序关闭时不起作用。

This works when my App is launched, but doesn't work when app is closed.

即使关闭应用程序,如何接收短信?感谢您的帮助。

How can I receive SMS even after the app is closed? Any help is appreciated.

谢谢

推荐答案

您可以接收器作为服务运行。这是我使用的代码:

You can have the receiver run as a Service. Here is the code I use:

public class Communicator extends Service
{

private final String TAG = this.getClass().getSimpleName();

private SMSReceiver mSMSreceiver;
private IntentFilter mIntentFilter;

@Override
public void onCreate()
{
    super.onCreate();


    Log.i(TAG, "Communicator started");
    //SMS event receiver
    mSMSreceiver = new SMSReceiver();
    mIntentFilter = new IntentFilter();
    mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
    mIntentFilter.setPriority(2147483647);
    registerReceiver(mSMSreceiver, mIntentFilter);

    Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVED");
    List<ResolveInfo> infos = getPackageManager().queryBroadcastReceivers(intent, 0);
    for (ResolveInfo info : infos) {
        Log.i(TAG, "Receiver name:" + info.activityInfo.name + "; priority=" + info.priority);
    }
}

@Override
public void onDestroy()
{
    super.onDestroy();

    // Unregister the SMS receiver
    unregisterReceiver(mSMSreceiver);
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

private class SMSReceiver extends BroadcastReceiver
{
    private final String TAG = this.getClass().getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle extras = intent.getExtras();


        String strMessage = "";

        if ( extras != null )
        {
            Object[] smsextras = (Object[]) extras.get( "pdus" );

            for ( int i = 0; i < smsextras.length; i++ )
            {
                SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);

                String strMsgBody = smsmsg.getMessageBody().toString();
                String strMsgSrc = smsmsg.getOriginatingAddress();

                strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;                    


                if (strMsgBody.contains(Constants.DELIMITER)) {

                    Intent msgIntent = new Intent(Constants.INTENT_INCOMMING_SMS);
                    msgIntent.putExtra(Constants.EXTRA_MESSAGE, strMsgBody);
                    msgIntent.putExtra(Constants.EXTRA_SENDER, strMsgSrc);
                    sendBroadcast(msgIntent);

                    this.abortBroadcast();
                }
            }



        }

    }

}
}

记住将其添加到清单中:

Remember to add it to the manifest:

<service android:name="yourpakage.Communicator" />

现在,您可以通过启动服务来开始收听传入的SMS消息:

Now you can start listening for incomming SMS messages by starting the service:

startService(new Intent(this, Communicator.class));

我在我的Activity的onDestroy中停止了该服务,但我想您可以使其继续运行甚至注册

I stop the service in onDestroy of my Activity but I suppose you can keep it running and even register it to be started when the device is booted as with any other service.

以下是停止服务的代码:

Here is the code to stop the service:

stopService(new Intent(this, Communicator.class));

如果您也想在某个时间发送短信,我会就此问题做出相当广泛的回答此处:发送SMS直到成功

In case you also want to send SMS at some point I have made a rather extensive answer on the subject here: Send SMS until it is successful

这篇关于android:即使应用已关闭,也会收到短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆