在Android Broadcast Receiver中的onReceive外部使用clearAbortBroadcast()方法
我正在开发一个Android应用程序来阻止收到的短信,并在完成一些处理后释放它们 我想做的是,当收到短信时,需要阻止该消息并将其推送到在线服务器。之后,管理员可以查看通过网站推送到服务器的消息,并可以批准或拒绝这些消息。如果管理员批准该邮件,则需要将其发布到手机收件箱,否则丢弃该邮件 除了一件事,我什么都完成了。我所做的是将消息推送到服务器后,将启动一个定时任务,每5分钟读取一次在线服务器,并检查消息是否被批准。我使用“BroadcastReceiver”来跟踪传入的消息,我知道要释放被阻止的消息,我应该在“onReceive”方法结束之前使用“clearAbortBroadcast()”方法。但我的计时器就像一根线。所以,如果我在计时器内调用“clearAbortBroadcast()”方法,“onReceive”方法已经完成执行,消息不会被释放 有人能帮我克服这个问题吗 我的广播接收机类在Android Broadcast Receiver中的onReceive外部使用clearAbortBroadcast()方法,android,broadcastreceiver,Android,Broadcastreceiver,我正在开发一个Android应用程序来阻止收到的短信,并在完成一些处理后释放它们 我想做的是,当收到短信时,需要阻止该消息并将其推送到在线服务器。之后,管理员可以查看通过网站推送到服务器的消息,并可以批准或拒绝这些消息。如果管理员批准该邮件,则需要将其发布到手机收件箱,否则丢弃该邮件 除了一件事,我什么都完成了。我所做的是将消息推送到服务器后,将启动一个定时任务,每5分钟读取一次在线服务器,并检查消息是否被批准。我使用“BroadcastReceiver”来跟踪传入的消息,我知道要释放被阻止的消
public class SmsReceiver extends BroadcastReceiver{
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;
public static long NOTIFY_INTERVAL = 15000;
int id;
Context context;
SmsReceiver sr;
GetMsgStatus status;
Bundle bundle;
@Override
public void onReceive(Context context, Intent intent)
{
//this stops notifications to others
this.abortBroadcast();
this.context = context;
//create a instance of GetMsgStatus class
status = new GetMsgStatus(context);
//---get the SMS message passed in---
bundle = intent.getExtras();
SmsMessage[] msgs = null;
String from = null;
String to = null;
String msg= null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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();
from = msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
msg = msgs[i].getMessageBody().toString();
str += "\n";
}
System.out.println("from "+from);
System.out.println("msg "+msg);
Toast.makeText(context, "SMS Received : ",Toast.LENGTH_LONG).show();
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
to = mTelephonyMgr.getLine1Number();
//push msg to the server
SendMsgToServer send = new SendMsgToServer(context, to, from, msg);
//get the msg id of the pushed msg
id = send.getId();
// cancel if already existed
if(mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
}
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
@Override
public void run()
{
status.getDataFromServer(id);
//if status is 0 means msg rejected
if(status.getStatus()==0)
{
mTimer.cancel();
}
//if status is 1 means msg approved
else if(status.getStatus()==1)
{
sr.clearAbortBroadcast();
mTimer.cancel();
}
//pending
else
{
System.out.println("pending");
}
}
});
}
}
公共类SmsReceiver扩展了BroadcastReceiver{
//在另一个线程上运行以避免崩溃
私有处理程序mHandler=新处理程序();
//计时器处理
专用定时器mTimer=null;
公共静态长通知间隔=15000;
int-id;
语境;
SMS接收器sr;
GetMsgStatus状态;
束;
@凌驾
公共void onReceive(上下文、意图)
{
//这将停止向其他人发送通知
这个.abortBroadcast();
this.context=上下文;
//创建GetMsgStatus类的实例
状态=新的GetMsgStatus(上下文);
//---获取传入的SMS消息---
bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串from=null;
字符串to=null;
字符串msg=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i广播接收器不应该活得那么久,所以您不能依赖clearAbortBroadcast()
在您的情况下,我认为您的流程是:
接收广播并发送到服务器
中止广播并删除手机短信(应提醒用户)
等待服务器的批准
如果批准,将消息保存到SMS并通知用户
我不认为这种审查是好的,但你可能有你的理由,只要确保安装这种审查的用户知道它的作用。如果你共享一些代码,可能会有所帮助。我建议使用谷歌云消息将批准的短信发送到通知中,从服务器发送到手机。这样可以避免“池”和5分钟的“池”是一个重型电池排水器。