More Related Content Similar to 안드로이드스터디 14 (20) 안드로이드스터디 141. 1. 안드로이드 추가 하드웨어 API 안드로이드 기기가 제공하는 저수준 하드웨어 기능에 접근하기 위해 다양한 API 가 사용된다 . 안드로이드 기기에 장착 될수 있는 장치로는 WIFI, 방향계 , 나침반들이 있고 , 이는 가속계 ( 속력을 감지 ), 나침반 ( 방향을 감지 ) 의 용도로 쓰일수 있으며 우리는 이러한 장치를 사용할 수 있는 클래스 및 메써드를 다양하게 사용할 것이다 . 단 , 에뮬레이터는 완전한 기기가 아니므로 실습의 성공여부를 확인 할수 없고 , 메써드를 보고 한번 코딩해보는 것에 목적을 두겠다 . 2. 2. 장치 감지기 사용하기 응용프로그램은 유요한 센서매니저 인스턴스를 이용하여 감지기의 자료를 받아 볼 수 있다 . 이것은 서비스 시스템 !! 따라서 getSystemService 메서드로 얻는다 . 4. 4. 감지기들의 측정 판독 감지기들의 측정을 판독 하라면 SensorListener 의 주요 메서드를 구현해야 한다 . 반드시 구현해야 하는 메서드로는 onAccuracychanged() 와 onSensorChanged() 가 있다 . 5. 5. 감지기들의 측정판독 2 첫인수는 감지기의 식별자이며 , 두번째 인수는 감지기의 측정자료이다 .(ex 온도 , 거리 , 속도 ) 6. 6. 감지기의 측정치 감지기의 측정치가 갱신 되었을 때 , 메서드를 다시 호출 하려면 , 다음과같이 SensorListener 에 구현을 등록해야 합니다 . 7. 7. 장치 감지기 사용하기 코드 1- sensors.XML <?xml version= "1.0" encoding="utf-8"?> <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent"> <ScrollView android:layout_width= "wrap_content" android:layout_height="wrap_content"> <LinearLayout android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "Sensors Info" /> <RadioGroup android:id= "@+id/sensor_group" android:layout_height= "wrap_content" android:layout_width= "wrap_content" > <RadioButton android:id= "@+id/sensor_accel" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= " 가속기 " /> <RadioButton android:id= "@+id/sensor_temp" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= " 온도계 " /> <RadioButton android:id= "@+id/sensor_light" android:layout_height= "wrap_content" android:text= " 조명계 " android:layout_width= "wrap_content" /> <RadioButton android:id= "@+id/sensor_mag" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= " 나침반 " /> <RadioButton android:id= "@+id/sensor_orient" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= " 방향계 " /> <RadioButton android:id= "@+id/sensor_prox" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= " 거리계 " /> </RadioGroup> <LinearLayout android:orientation= "horizontal" android:layout_width= "fill_parent" android:layout_height= "wrap_content"> <Button android:id= "@+id/start_sensor" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Start Sensor" /> <Button android:id= "@+id/stop_sensor" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Stop Sensor" android:visibility= "gone" /> </LinearLayout> </LinearLayout> </ScrollView> <LinearLayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_alignParentBottom= "true"> <TextView android:id= "@+id/status" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "... data will go here ..." android:layout_gravity= "right" android:gravity="right"/> </LinearLayout> </RelativeLayout> 8. 8. 장치 감지기 사용하기 코드 2- 자바코드 9. 9. 배터리 잔량과 외부 전원 정보 모바일 기기들은 배터리를 전원으로 작동한다 . 모든 응용프로그램들이 배터리의 정보를 알필요는 없으나 , 요즘 응용프로그램들은 배터리의 전원 여부를 파악하여 현 프로그램이 작동방식에 영향을 주게 만들고 있다 . 고로 우리의 응용프로그램도 이를 이용할수 있게 하기 위하여 이 장을 준비 했다 . 응용 프로그램에서 배터리 상태 정보에 접근을 하기 위하여 , 특수한 권한이 필요한데 , android.Manifast.xml 에 다음과 같은 요소를 추가한다 . 10. 10. 배터리 잔량과 외부 전원 정보 배터리로 부터 여러가지 정보를 받아서 다양한 필드들의 수치를 뽑아 낸다 . 각 필드들은 현 배터리의 절대적인 수치가 아닌 사용자들에게 필요한 상대적인수치이다 . 이 수치들에 대한 설명은 p450 에 나와있고 , 이 수치에 대한 데이터를 받는 것은 , 코드를 보면서 알아 보도록 한다 . 11. 11. 배터리 잔량과 외부 전원 정보 각각의 배터리에 대한 수치들은 BroadcastReceiver 객체의 onReceive 함수를 통해 받아 올수 있다 . 잘린 부분은 그 상태를 표시하기 위한 스트링이다 . Intent 객체를 이용 각각의 상대적인 수치를 지정 . 그리고 받아와서 출력할수 있는 스트링을 만들수 있다 . 각각의 수치에 대한 설명은 p450 에 있다 . 12. 12. 배터리 정보의 각각의 필드 감지기의 식별자 처럼 각각의 필드에 있는 수치에 대한 식별자도 정의하여 입력해야 우리가 원하는 정보를 찾아서 쓸 수 있다 . 13. 13. 배터리 잔량과 외부 전원 정보 - Battery.xml <?xml version= "1.0" encoding="utf-8"?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent"> <TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "Battery Info" /> <LinearLayout android:orientation= "horizontal" android:layout_width= "fill_parent" android:layout_height= "wrap_content"> <Button android:id= "@+id/start" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Start" /> <Button android:id= "@+id/stop" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Stop" android:visibility= "gone" /> </LinearLayout> <TextView android:id= "@+id/status" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "... data will go here ..." /> <ImageView android:id= "@+id/icon" android:layout_width= "wrap_content" android:layout_height= "wrap_content"></ImageView> </LinearLayout> 14. 14. 배터리 잔량과 외부 전원 정보 - 자바코드 package A5.com; import android.app.Activity; import android.os.Bundle; import java.util.HashMap; import java.util.Map; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class com extends Activity { private BroadcastReceiver batteryRcv = null; private static final Map<Integer, String> healthValueMap = new HashMap<Integer, String>() { { put(BatteryManager. BATTERY_HEALTH_DEAD, "Dead"); put(BatteryManager. BATTERY_HEALTH_GOOD, "Good"); put(BatteryManager. BATTERY_HEALTH_OVER_VOLTAGE, "Over voltage"); put(BatteryManager. BATTERY_HEALTH_OVERHEAT, "Over heating"); put(BatteryManager. BATTERY_HEALTH_UNKNOWN, "Unknown"); put(BatteryManager. BATTERY_HEALTH_UNSPECIFIED_FAILURE, "Failure, but unknown"); put(-1, "Not Reported"); } }; private static final Map<Integer, String> statusValueMap = new HashMap<Integer, String>() { { put(BatteryManager. BATTERY_STATUS_CHARGING, "Charging"); put(BatteryManager. BATTERY_STATUS_DISCHARGING, "Discharging"); put(BatteryManager. BATTERY_STATUS_FULL, "Full"); put(BatteryManager. BATTERY_STATUS_NOT_CHARGING, "Not Charging"); put(BatteryManager. BATTERY_STATUS_UNKNOWN, "Unknown"); put(-1, "Not Reported"); } }; private static final Map<Integer, String> pluggedValueMap = new HashMap<Integer, String>() { { put(BatteryManager. BATTERY_PLUGGED_AC, "On AC"); put(BatteryManager. BATTERY_PLUGGED_USB, "On USB"); put(-1, "Not Reported"); } }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. battery); final ImageView icon = (ImageView) findViewById(R.id. icon); final Button start = (Button) findViewById(R.id. start); final Button stop = (Button) findViewById(R.id. stop); final TextView status = (TextView) findViewById(R.id. status); batteryRcv = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int level = intent.getIntExtra("level", -1); int maxValue = intent.getIntExtra("scale", -1); int batteryStatus = intent.getIntExtra("status", -1); int batteryHealth = intent.getIntExtra("health", -1); int batteryPlugged = intent.getIntExtra("plugged", -1); String batteryTech = intent.getStringExtra("technology"); int batteryIcon = intent.getIntExtra("icon-small", -1); float batteryVoltage = (float) intent.getIntExtra("voltage", -1) / 1000; boolean battery = intent.getBooleanExtra("present", false); float batteryTemp = (float) intent.getIntExtra("temperature", -1) / 10; /* used to determine keys and types * Bundle extras = intent.getExtras(); Set<String> keys = * extras.keySet(); Iterator<String> allKeys = keys.iterator(); * while (allKeys.hasNext()) { String key = allKeys.next(); * Log.v("Battery", key); } */ int chargedPct = (level * 100) / maxValue; String batteryInfo = "Battery Info:Health=" + healthValueMap.get(batteryHealth) + "" + "Status=" + statusValueMap.get(batteryStatus) + "" + "Charged % = " + chargedPct + "%" + "Plugged = " + pluggedValueMap.get(batteryPlugged) + "" + "Type = " + batteryTech + "" + "Voltage = " + batteryVoltage + " volts" + "Temperature = " + batteryTemp + " 좧 " + "Battery present = " + battery + ""; status.setText(batteryInfo); icon.setImageResource(batteryIcon); Toast. makeText(com. this, "Battery state changed", Toast.LENGTH_LONG).show(); } }; start.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { registerReceiver(batteryRcv, new IntentFilter(Intent. ACTION_BATTERY_CHANGED)); Toast. makeText(com. this, "Battery monitoring started", Toast.LENGTH_SHORT).show(); start.setVisibility(View. GONE); stop.setVisibility(View. VISIBLE); } }); stop.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { unregisterReceiver(batteryRcv); Toast. makeText(com. this, "Battery monitoring stopped", Toast.LENGTH_SHORT).show(); stop.setVisibility(View. GONE); start.setVisibility(View. VISIBLE); } }); } @Override protected void onPause() { if (batteryRcv != null) { unregisterReceiver(batteryRcv); batteryRcv = null; } super.onPause(); } } 15. 15. 후기 . 본 응용프로그램의 버튼이나 텍스트뷰등의 구현은 이미 배운것이므로 설명하지 않았습니다 . 이제우리도 intent 를 사용하여 페이지를 넘긴다든가 , 텍스트뷰를 조정하는것 , 그리고 버튼 및 메뉴 활용의 빈도를 높여 프로젝트를 진행할수 있었으면 좋겠습니다 . 시간이 얼마 남지 않았고 , 앞으로 매우 바쁜 나날을 보내 겠지만 조금만더 힘을내서 프로젝트를 성공적으로 완성 시켰으면 좋겠습니다 . A5 파이팅 !! 중요한건 .. PDF 파일 연동인데 ㅠ 최선을 다하도록 하겟슴돠 <~!~!~