onBroadcastReceiver 是否在后台工作?
我有一个自动回复短信应用程序。其功能是当短信到来时,应用程序自动回复繁忙消息给发件人。 我还创建了一个活动。在此活动中,我有一个广播接收器方法,当短信到来时,该方法使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要在收到消息后致电服务人员。您应该将所有功能移至 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 .