무제한 요금제를 사용하지만 Wi-Fi의 환경이 더 빠르기 때문에 사무실이나 집에서 전원을 연결하고 Wi-Fi를 사용합니다.
그리고 이동 중에는 Wi-Fi를 off합니다.
그래서 자동으로 충전 중이면 Wi-Fi를 켜주고 충전이 아니면 Wi-Fi를 끄는 app을 만들어 봤습니다.
추가적으로 빈 Activity에 사용여부 확인 가능한 checkbox를 추가하였습니다.
http://dl.dropbox.com/u/4485660/WiFiAuto.apk
아주 간단한 프로그램인데 BroadcastReceiver 상속받은 class만으로 app을 만들어 보니 ACTION_POWER_CONNECT intent를 못 받아서 삽질 좀 했네요.
그래서 아무 동작하지 않는 Activity를 추가 하였습니다.
--- ACConnectReceiver.java ---
package com.hardkernel.odroid.wifiauto;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
public class ACConnectReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
manager.setWifiEnabled(true);
} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
manager.setWifiEnabled(false);
}
}
}
--- MainActivity.java ---
package com.hardkernel.odroid.wifiauto;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
CheckBox cbUse = (CheckBox)findViewById(R.id.cb_use);
cbUse.setOnCheckedChangeListener(new OnCheckedChangeListener () {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
SharedPreferences prefs = getSharedPreferences("PreName", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("Use", isChecked);
editor.commit();
}
});
SharedPreferences prefs = getSharedPreferences("PreName", MODE_PRIVATE);
cbUse.setChecked(prefs.getBoolean("Use", true));
}
}
--- AndroidManifest.xml ---
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hardkernel.odroid.wifiauto"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="10" />
<uses-feature android:name="android.hardware.wifi" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.DEVICE_POWER"/>
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:label="@string/app_name"
android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".ACConnectReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
</application>
</manifest>
댓글 없음:
댓글 쓰기