1. StandAlone Type
: 별도의 클래스에 Custom BroadcastReceiver Class생성
: Manifest파일에 Receiver등록 및 IntentFilter > Action 정의
+ Manifest :
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.networkreceiver" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8"/> <application android:label="@string/app_name" android:icon="@drawable/icon"> <receiver android:name="NetworkReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver> </application></manifest>
+ Java Code : (Custom Receiver Class)
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.util.Log; public class NetworkReceiver extends BroadcastReceiver { private static final String LOGTAG = "NetworkReceiver"; @Override public void onReceive(Context ctx, Intent intent) { Log.i(LOGTAG, "Action: " + intent.getAction()); if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); String typeName = info.getTypeName(); String subtypeName = info.getSubtypeName(); boolean available = info.isAvailable(); Log.i(LOGTAG, "Network Type: " + typeName + ", subtype: " + subtypeName + ", available: " + available); } }}
2. Inside Activity Type
: Activity내부에서 BroadcastReceiver와 IntentFilter 생성 및 등록
private IntentFilter mNetworkStateChangedFilter; private BroadcastReceiver mNetworkStateIntentReceiver; private String mTypeName = "Unknown"; private String mSubtypeName = "Unknown"; private boolean mAvailable = false; private TextView statusField; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNetworkStateChangedFilter = new IntentFilter(); mNetworkStateChangedFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); mNetworkStateIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); mTypeName = info.getTypeName(); mSubtypeName = info.getSubtypeName(); mAvailable = info.isAvailable(); Log.i(LOGTAG, "Network Type: " + mTypeName + ", subtype: " + mSubtypeName + ", available: " + mAvailable); updateScreen(); } } }; setContentView(R.layout.main); } private void updateScreen() { statusField = (TextView) findViewById(R.id.networkStatus); statusField.setText("Network Type: " + mTypeName + "\n" + "Network subtype: " + mSubtypeName + "\n" + "Available: " + mAvailable); } @Override protected void onResume() { Log.d(LOGTAG, "onResume"); super.onResume(); registerReceiver(mNetworkStateIntentReceiver, mNetworkStateChangedFilter); unregisterReceiver(mNetworkStateIntentReceiver); registerReceiver(mNetworkStateIntentReceiver, mNetworkStateChangedFilter); } @Override protected void onPause() { Log.d(LOGTAG, "onPause"); super.onPause(); unregisterReceiver(mNetworkStateIntentReceiver); }
댓글 없음:
댓글 쓰기