브로드캐스트리시버의 등록과 해제

[UI Activity...]

    @Override

public void onCreate(Bundle savedInstanceState) {
this.savedInstanceState=savedInstanceState;
        super.onCreate(savedInstanceState);
        
/-* 브로드캐스트리시버 등록 *-
this.registedReceiver =
ReceiverUtil.registerReceiver(this,
"com.test.app.ui.activity.common.ULNetworkReceiver", ConnectivityManager.CONNECTIVITY_ACTION);
}



@Override
protected void onDestroy()
{

super.onDestroy();

/-* 브로드캐스트리시버 해제 *-
if(this.registedReceiver != null) {
ReceiverUtil.unregisterReceiver(this, this.registedReceiver);
}
}


[ReceiverUtil]


public class ReceiverUtil
{
public final static String NETWORK_RECEVIER_CLASS =
                    "com.xxx.xxx.ui.activity.common.ULNetworkReceiver"; // NETWORK RECEVIER 클래스
/-*
 * recevier를 등록한다.
 * @param activity   recevier를 등록할 activity
 * @param recevierName 호출될 BroadcastReceiver 클래스
 * @param recevierEventName recevier event 이름
 * @return BroadcastReceiver
 *-
public static BroadcastReceiver registerReceiver(Activity activity, String recevierName,
String recevierEventName)
{
IntentFilter filter = null;
Class recevierClass = null;
BroadcastReceiver receiver = null;

if(receiver == null)
{
try
{
filter = new IntentFilter(recevierEventName);
recevierClass = Class.forName(recevierName);
receiver = (BroadcastReceiver) recevierClass.newInstance();
activity.registerReceiver(receiver, filter);
Log.d("receiver", "registed BroadcastReceiver...");
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InstantiationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
filter = null;
recevierClass = null;
}
}

return receiver;
}

/-*
 * recevier를 해제한다.
 * @param activity
 * @param receiver
 *-
public static void unregisterReceiver(Activity activity, BroadcastReceiver receiver)
{
if(receiver != null) {
activity.unregisterReceiver(receiver);

Log.d("receiver", "unregisted BroadcastReceiver...");
}
}
}

[ULNetworkReceiver]

public class ULNetworkReceiver extends BroadcastReceiver
{
private Activity activity;

public ULNetworkReceiver() {
super();
}

public ULNetworkReceiver(Activity activity) {
this.activity = activity;
}

@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();

// 네트웍에 변경이 일어났을때 발생하는 부분
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
try {
ConnectivityManager connectivityManager =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo =
 connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

/-* 3G, WIFI 둘 중 하나라도 있을 경우 *-
if(activeNetInfo != null && mobNetInfo != null){
String str = "";
if(activeNetInfo.getTypeName().toString().equals("WIFI"))
{
str += "Wi-Fi로 연결중";
}
else if(activeNetInfo.getTypeName().toString().equals("mobile"))
{
str += "3G로 연결합니다. Wi-Fi로 이용 할 수 없습니다.";
}
//Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
/-* 3G, WIFI 둘 다 없을 경우 *-
else{
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle("알림");
alert.setMessage("네트워크 연결불가");
alert.setPositiveButton("확인", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
activity.onBackPressed();
}
});
alert.show();
}
} catch (Exception e) {

//Log.i("ULNetworkReceiver", e.getMessage());
}
}
}
}