我正在编写一个应用程序,即使在应用程序关闭后它也应该可以运行。这是我为接受传入的短信而编写的代码。
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);
}
}
}
我已经在我的主要 Activity 中注册了这个广播。而且我没有注销广播。
这在我的应用程序启动时有效,但在应用程序关闭时无效。
如何在应用程序关闭后仍能收到短信?感谢您的帮助。
谢谢
最佳答案
您可以让接收器作为服务运行。这是我使用的代码:
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();
}
}
}
}
}
}
记得将其添加到 list 中:
<service android:name="yourpakage.Communicator" />
现在您可以通过启动服务来开始监听传入的 SMS 消息:
startService(new Intent(this, Communicator.class));
我在我的 Activity 的 onDestroy 中停止了该服务,但我想您可以让它继续运行,甚至注册它以便在设备启动时像任何其他服务一样启动。
这是停止服务的代码:
stopService(new Intent(this, Communicator.class));
如果您还想在某个时候发送 SMS,我在这里就此主题做了相当广泛的回答:Send SMS until it is successful
关于android:即使应用程序关闭也能收到短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19813707/