我正在编写一个应用程序,即使应用程序关闭后也应该可以正常运行这是我写的接受传入短信的代码。
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);
}
}
}
我在我的主要活动中注册了这个广播。而且我不会取消注册广播。
这在我的应用程序启动时有效,但在应用程序关闭时无效。
即使应用关闭后,我如何才能收到短信?任何帮助表示赞赏。
由于
答案 0 :(得分:2)
您可以让接收器作为服务运行。这是我使用的代码:
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();
}
}
}
}
}
}
请记住将其添加到清单中:
<service android:name="yourpakage.Communicator" />
现在,您可以通过启动服务来开始侦听发送短信:
startService(new Intent(this, Communicator.class));
我在我的Activity的onDestroy中停止服务,但我想你可以保持它的运行,甚至注册它以便在启动设备时启动,就像使用任何其他服务一样。
以下是停止服务的代码:
stopService(new Intent(this, Communicator.class));
如果你还想在某个时候发送短信,我在这里就这个问题做了一个相当广泛的答案:Send SMS until it is successful
答案 1 :(得分:1)
接收者应该在清单中;还要记住,即使声明了接收者的最高优先级,您也没有保证接收短信 - 其他安装的应用程序碰巧从OS接收SMS Intent可能会取消进一步的Intent传播
答案 2 :(得分:0)
如果您在活动中注册接收者,则必须取消注册!避免内存泄漏非常重要。
如果您希望随时收到短信,则必须在AndroidManifest file
中注册短信