我已经创建了一个类,并在类构造函数中为 SMS创建了一个broadcastreceiver ,并在finalize方法类中注销了接收器。
现在:我的应用程序有 3 个选项卡,每个选项卡都有活动,当活动的OnCreate我创建我的类的实例(为当前活动创建 BroadcastReceiver)和OnStop我完成了类。
问题是当我在标签中移动很多次时,我会强制关闭应用程序,但本文最后一次展示的除外。
分类代码:
package com.example.tawasol1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import com.example.tawasol1.Cls_stringManager.MessageType;
import com.example.tawasol1.Cls_stringManager.QueryMessge;
abstract class RecieverEvents{
abstract void OnAccountNotRegistered(String Sender,String Message,String Provider);
abstract void OnAccountNoBalance(String Sender,String Message,String Provider);
abstract void OnAccountActivatedOk(String Sender,String Message,String Provider);
abstract void OnAccountRegisterNoMoney(String Sender,String Message);
abstract void OnMessage(String Sender,String Message,String FromNumber);
abstract void OnEnvitationMessage(String Sender,String Message,String FromNumber);
}
public class Cls_CreateReceiver {
private Context mContext ;
private RecieverEvents mReceiverEvnts ;
private BroadcastReceiver CurrentReceiver ;
private String smsSender ;
private String smsBody ;
// private Intent CheckPage ;
Cls_CreateReceiver(Context c ,int Priority, RecieverEvents RecEvents){
mContext =c ;
mReceiverEvnts = RecEvents ;
//====================== Body Of receiver
IntentFilter fp=new IntentFilter();
fp.addAction("android.provider.Telephony.SMS_RECEIVED");
fp.setPriority(Priority);
CurrentReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context conx, Intent recIntent) {
//===================== Get Message Extras
Bundle IntExtras = recIntent.getExtras() ;
if (IntExtras == null)
return;
Object[] pdus = (Object[]) IntExtras.get("pdus");
// get message from bundle intent
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
smsSender = SMessage.getOriginatingAddress();
smsBody = SMessage.getMessageBody().toString();
}
//======================End Message Extras
Boolean isSystemMessage = false ;
QueryMessge retMsgQuery = QueryMessge.None ;
cls_database smsCheck = new cls_database(mContext) ;
Cls_stringManager StrManager = new Cls_stringManager();
isSystemMessage = smsCheck.isOurSystemMessage(smsSender);
//================= isSystemMessage
if (isSystemMessage)
{
this.abortBroadcast() ;
if (StrManager.isQueryMessage(smsBody))
{
Log.d("isQueryMessage:", "YES");
retMsgQuery = StrManager.GetQueryMsg(smsBody) ;
switch (retMsgQuery) {
case None:
// Toast.makeText(c,"Error Message Query Type" , Toast.LENGTH_LONG).show();
break;
case NoBalance :
mReceiverEvnts.OnAccountNoBalance(smsSender, smsBody, "");
break ;
case AccountActivated :
mReceiverEvnts.OnAccountNotRegistered(smsSender, smsBody, "");
break ;
case NoRegisteredAccount :
mReceiverEvnts.OnAccountNotRegistered(smsSender, smsBody, "") ;
break ;
default :
Log.d("Receiver Other Msg:", smsBody);
Log.d("Receiver Other Msg:", smsSender);
if(smsSender.equals("TASHBIK"))
{
Log.d("Receiver Other Msg:Tashbik", smsBody);
mReceiverEvnts.OnAccountRegisterNoMoney(smsSender, smsBody) ;
}
} //switch Case
}//StrManager.isQueryMessage
else
{
Log.d("isQueryMessage:", "NOT");
if (StrManager.isAccountExpireNoRegistered(smsBody)){
mReceiverEvnts.OnAccountNotRegistered(smsSender, smsBody, "") ;
return;
}
if (StrManager.isNoBalaceMessage(smsBody)){
mReceiverEvnts.OnAccountNoBalance(smsSender, smsBody, "") ;
return;
}
String ChatterMobile = "" ;
String ChatterMessage="" ;
MessageType MsgType = MessageType.None ;
MsgType = StrManager.GetMessageType(smsBody);
Log.d("Reciver>HasBody:", StrManager.hasBody(smsBody).toString());
Log.d("Reciver>HasMobile:", StrManager.hasMobile(smsBody).toString());
Log.d("Reciver>Mobile:", StrManager.GetMobileFromMessage(smsBody));
Log.d("Reciver>Msg:", StrManager.GetMessageBody(smsBody));
if(StrManager.hasMobile(smsBody))
ChatterMobile = StrManager.GetMobileFromMessage(smsBody);
if (StrManager.hasBody(smsBody))
ChatterMessage = StrManager.GetMessageBody(smsBody);
switch (MsgType) {
case Chatting:
mReceiverEvnts.OnMessage(smsSender, smsBody, "CHAT");
break;
case DirectMessage:
mReceiverEvnts.OnMessage(smsSender, ChatterMessage.trim(), ChatterMobile.trim());
break;
case Invitation :
mReceiverEvnts.OnEnvitationMessage(smsSender, smsBody, ChatterMobile.trim());
break ;
default:
if(smsSender.equals("TASHBIK"))
{
Log.d("Receiver Other Msg:Tashbik", smsBody);
mReceiverEvnts.OnAccountRegisterNoMoney(smsSender, smsBody) ;
}
break;
}
}//StrManager.isQueryMessage
}//================= isSystemMessage or Not
} // End onReceive event
};
//====================End Of Receiver
mContext.registerReceiver(CurrentReceiver,fp);
} //End Of Constructor
public void UnregisterReceiver(){
mContext.unregisterReceiver(CurrentReceiver);
}
@Override
protected void finalize() throws Throwable {
mContext.unregisterReceiver(CurrentReceiver);
super.finalize();
}
}
错误日志:
03-04 14:48:57.331: E/System(22346): Uncaught exception thrown by finalizer
03-04 14:48:57.331: E/System(22346): java.lang.IllegalArgumentException: Receiver not registered: com.example.tawasol1.Cls_CreateReceiver$1@41d64278
03-04 14:48:57.331: E/System(22346): at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:654)
03-04 14:48:57.331: E/System(22346): at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1297)
03-04 14:48:57.331: E/System(22346): at com.example.tawasol1.Cls_CreateReceiver.finalize(Cls_CreateReceiver.java:223)
03-04 14:48:57.331: E/System(22346): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:186)
03-04 14:48:57.331: E/System(22346): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:169)
03-04 14:48:57.331: E/System(22346): at java.lang.Thread.run(Thread.java:856)