私はクラスを作成し、クラスコンストラクター内で SMS 用のブロードキャストレシーバーを作成し、ファイナライズメソッド クラスでレシーバーの登録を解除しました。
今:私のアプリケーションには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)