我有一个BroadcastListener
,用于检查输入文本消息并处理它们以获取警报系统的潜在状态。
如果文本符合我的条件以及我的应用程序在前台的活动,我想在我的活动的GUI中显示状态。
为此我尝试使用这样的意图:
public class SmsReader extends BroadcastReceiver implements Runnable
{
private static final String LOG_TAG = "SMSReceiver";
private String alarm = "15555215554";
public static final String INCOMING_SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";
public static final String ALARM_STATUS_ON_FULL = "moorehojer.alarmcontrol.intent.action.ALARM_STATUS_FULL_ON";
public static final String ALARM_STATUS_ON_PART = "moorehojer.alarmcontrol.intent.action.ALARM_STATUS_PART_ON";
public static final String ALARM_STATUS_OFF = "moorehojer.alarmcontrol.intent.action.ALARM_STATUS_OFF";
@Override
public void onReceive(Context _context, Intent intent)
{
String intentActionToCall = "";
if (intent.getAction().equals(INCOMING_SMS_ACTION))
{
//this stops notifications to others
this.abortBroadcast();
boolean releaseBroadcast = false;
try
{
Bundle myBundle = intent.getExtras();
SmsMessage [] messages = null;
if (myBundle != null)
{
Object [] pdus = (Object[]) myBundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
if(!Util.UnifyPhoneNumberPrefix(messages[i].getOriginatingAddress()).equals(Util.UnifyPhoneNumberPrefix(this.alarm)))
{
releaseBroadcast = true;
}
else
{
if(messages[i].getMessageBody().substring(0, 5).equals("FS-C "))
{
boolean frakoblet01 = messages[i].getMessageBody().contains("FRAKOBLET1");
boolean frakoblet02 = messages[i].getMessageBody().contains("FRAKOBLET2");
boolean tilkoblet01 = messages[i].getMessageBody().contains("TILKOBLET1");
boolean tilkoblet02 = messages[i].getMessageBody().contains("TILKOBLET2");
boolean deltilkoblet01 = messages[i].getMessageBody().contains("DELTILKOBLET1");
boolean deltilkoblet02 = messages[i].getMessageBody().contains("DELTILKOBLET2");
if(deltilkoblet01 || deltilkoblet02)
{
intentActionToCall = SmsReader.ALARM_STATUS_ON_PART;
}
else if(tilkoblet01 && tilkoblet02)
{
intentActionToCall = SmsReader.ALARM_STATUS_ON_FULL;
}
else if(frakoblet01 && frakoblet02)
{
intentActionToCall = SmsReader.ALARM_STATUS_OFF;
}
else
{
releaseBroadcast = true;
}
}
}
}
}
}
catch(Exception e)
{
this.makeToast(_context, "Ex 1: "+e.toString());
}
/**/
try
{
if(releaseBroadcast)
{
this.clearAbortBroadcast();
}
}
catch (Exception e)
{
this.makeToast(_context, e.toString());
}
}
if(!intentActionToCall.equals(""))
{
Intent myIntent = new Intent(intentActionToCall);
this.makeToast(_context, "Intent created:\n"+intentActionToCall);
this.makeToast(_context, "Trying broadcast");
myIntent.putExtra("message", "Hallelujah");
try
{
_context.sendBroadcast(myIntent);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
private void makeToast(Context context, String message)
{
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
public void run() {
// TODO Auto-generated method stub
}
}
但每次代码到达该行:
_context.sendBroadcast(myIntent);
操作系统会发出一条消息,指出该进程已经意外停止工作,并且在try-catch单步执行时关闭。
我尝试删除该行,代码运行到最后没有问题。