読者です 読者をやめる 読者になる 読者になる

Androidアプリ開発パーフェクトマスター - 1日目

またまた先輩が持ってたので、先輩が読む前に借りた。ありがとうございます!

Chapter 0 ゼロからスタート!

Chapter 1 Androidで何ができるのか?

Chapter 3 Android Studio を設定する


Genymotion
f:id:yossk:20141213145013j:plain

Chapter 4 はじめてのAndroidアプリ開発!

app/src/main/res/values/strings.xml
の値を変更して実行するだけ。
Javaの説明もあって、優しい作りになっている。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My Application</string>
    <string name="hello_world">こんにちはAndroid!</string>
    <string name="action_settings">Settings</string>
</resources>

Chapter 5 Androidアプリ開発完全理解!

  • p.148の/protは/proc
  • GUIでポトペタできるのは楽でいいね!

f:id:yossk:20141213153911j:plain

  • Version表示
  • try - resource - with というのがあるようだが、APIがロック(一定上? @TargetApi(Build.VERSION_CODES.KITKAT)を記入する必要がある)されるようなので使わないでおく

try-with-resources 文

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String version = null;
        try {
            BufferedReader reader = new BufferedReader(new FileReader("/proc/version"));
            version = reader.readLine();
            reader.close();
            TextView textView = (TextView) findViewById(R.id.textView);
            textView.setText("カーネルのバージョン:\n" + version);
        } catch (FileNotFoundException e) {
            Log.e("tag", e.toString());
        } catch (IOException e) {
            Log.e("tag", e.toString());
        }
    }

f:id:yossk:20141213154824j:plain

  • 時間表示
    • プロジェクト分けるのもなんだし、消すのもなんなので、別メソッドにしてみた
    void writeTime() {
        long ut = SystemClock.uptimeMillis();
        TextView textView = (TextView)findViewById(R.id.textView);
        textView.setText(
            String.format(
                "起動後の経過時間%n(ミリ秒単位):%s%n(秒単位):%s%n" +
                    "(分単位):%s%n(時間単位):%s:%s",
                ut, ut / 1000, ut / 1000 / 60, ut / 1000 / 60 / 60,
                ut / 1000 / 60 % 60
            )
        );
    }

f:id:yossk:20141213160651j:plain

  • クラス作成、値表示
    void writeTest() {
        Test a = new Test();
        Test b = new Test();

        a.x = 100;
        b.x = 100;

        b = a;
        a.x = 10;

        int c = 100;
        int d = 500;

        d = c;
        c = 10;

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(String.format("b.x = %s%nd = %s%n", b.x, d));
    }
  • 地域情報と言語圏
    void writeLocale() {
        Locale df = Locale.getDefault();
        String dc = df.getDisplayCountry();
        String c = df.getCountry();
        String ic = df.getISO3Country();
        String dl = df.getDisplayLanguage();
        String l = df.getLanguage();
        String il = df.getISO3Language();

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(
            String.format(
                "現在の地域設定と言語設定%n%n" +
                    "国名:%s%n" +
                    "国コード:%s%n" +
                    "ISO国コード:%s%n" +
                    "言語名:%s%n" +
                    "言語コード:%s%n" +
                    "ISO言語コード:%s%n",
                dc, c, ic, dl, l, il
            )
        );
    }

f:id:yossk:20141213165423j:plain

public class Test {
    int x;

    double calc(double p) {
        return p * 1.08;
    }

    double average(double a, double b, double c) {
        return sum(a, b, c) / 3;
    }

    double sum(double a, double b, double c) {
        return a + b + c;
    }
}
  • 現在日時
    void writeCalendar() {
        Calendar calendar = Calendar.getInstance();

        String am_pm = calendar.get(Calendar.AM_PM) == 0 ? "午前" : "午後";

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int date = calendar.get(Calendar.DATE);
        int hour12 = calendar.get(Calendar.HOUR);
        int hour24 = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        int msecond = calendar.get(Calendar.MILLISECOND);

        String format1 = String.format("%s年%s月%s日", year, month, date);
        String format2 = String.format(
            "%s%s時%s分%s秒%s", am_pm, hour12, minute, second, msecond
        );
        String format3 = String.format(
            "%s時%s分%s秒%s", hour24, minute, second, msecond
        );

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(
            String.format(
                "現在の日時:%n%n%s%n%s%n%s%n", format1, format2, format3
            )
        );
    }
public class Calculate {
    private double tax;

    Calculate(double tax) {
        this.tax = tax;
    }

    int taxCalculate(int p) {
        return (int)(p * (1 + tax));
    }
}
public class SetNumber {
    int numA, numB;
    double numC;

    SetNumber(int a, int b, double c) {
        numA = a;
        numB = b;
        numC = c;
    }

    SetNumber(int a) {
        numA = a;
        numB = 10;
        numC = 1.234;
    }

    SetNumber(double c) {
        numA = 500;
        numB = 10;
        numC = c;
    }
}
    void writeSetNumber() {
        SetNumber setNumber1 = new SetNumber(11, 22, 33.456);
        SetNumber setNumber2 = new SetNumber(20);
        SetNumber setNumber3 = new SetNumber(11.55);

        TextView textView = (TextView)findViewById(R.id.textView);
        textView.setText(
            String.format(
                "setNumber1 numA = %s, numB = %s, numC = %s%n" +
                    "setNumber2 numA = %s, numB = %s, numC = %s%n" +
                    "setNumber3 numA = %s, numB = %s, numC = %s%n",
                setNumber1.numA, setNumber1.numB, setNumber1.numC,
                setNumber2.numA, setNumber2.numB, setNumber2.numC,
                setNumber3.numA, setNumber3.numB, setNumber3.numC
            )
        );
    }

f:id:yossk:20141213174541j:plain

Chapter 6 Androidの構造と仕組みを知る

  • ライフサイクルとログ

Activity | Android Developers
f:id:yossk:20141213202436j:plain

  • コンテンツプロバイダへの問い合わせはコンテンツリゾルバ
    void brightCheck() {
        String brightness = Settings.System.getString(
            this.getContentResolver(), "screen_brightness"
        );
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText("画面の明るさ:" + brightness);
    }
  • ART(Android RunTime)
    • Dalvikより高速
    • 4.4から実験的に導入
  • ブロードキャスト
    private BroadcastReceiver br =
        new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
                    int level = intent.getIntExtra("level", 0);
                    int scale = intent.getIntExtra("scale", 0);
                    TextView textView = (TextView) findViewById(R.id.textView);
                    textView.setText(
                        "バッテリーの残量:" +
                            ((float) level / (float) scale * 100) + " (%)"
                    );
                }
            }
        };

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(br, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(br);
    }

f:id:yossk:20141213205901j:plain

  • 継承
public class AddNumber {
    protected int num;

    void add(int num) {
        this.num += num;
    }

    int getNum() {
        return num;
    }
}
public class AddNumberSub extends AddNumber {
    private int record;

    void recorder() {
        record = num;
    }

    int getRecord() {
        return record;
    }
}
public class SubtractNumber extends AddNumberSub {
    void subtract(int val) {
        num -= val;
    }
}
    void writeNumber() {
        SubtractNumber sub = new SubtractNumber();
        sub.add(30);
        sub.recorder();
        sub.subtract(20);

        TextView textView = (TextView) findViewById(R.id.textView2);
        textView.setText(
            "num = " + sub.getNum() + "\n" +
                "recoder = " + sub.getRecord()
        );
    }

Chapter 7 イベンを処理する

  • ボタンをクリックでテキスト変更
public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView textView = (TextView) findViewById(R.id.textView1);
        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText("Button Pushed1.");
            }
        });
    }

    public void onClickMsg(View v) {
        TextView textView = (TextView) findViewById(R.id.textView1);
        textView.setText("Button Pushed2.");
    }
}

f:id:yossk:20141213230244j:plain

  • イベントリスナーを直接アクティビティに登録する
public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((Button) findViewById(R.id.button1)).setOnClickListener(this);
        ((Button) findViewById(R.id.button2)).setOnClickListener(this);
        ((Button) findViewById(R.id.button3)).setOnClickListener(this);
    }

    private void onClickButton1() {
        ((TextView) findViewById(R.id.textView)).setText("Button[1] Pushed.");
    }

    private void onClickButton2() {
        ((TextView) findViewById(R.id.textView)).setText("Button[2] Pushed.");
    }

    private void onClickButton3() {
        ((TextView) findViewById(R.id.textView)).setText("Button[3] Pushed.");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                onClickButton1();
                break;

            case R.id.button2:
                onClickButton2();
                break;

            case R.id.button3:
                onClickButton3();
                break;
        }
    }
}
public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((Button) findViewById(R.id.button1)).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((TextView) findViewById(R.id.textView)).setText("Button[1] Pushed.");
                }
            }
        );

        ((Button) findViewById(R.id.button2)).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((TextView) findViewById(R.id.textView)).setText("Button[2] Pushed.");
                }
            }
        );

        ((Button) findViewById(R.id.button3)).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((TextView) findViewById(R.id.textView)).setText("Button[3] Pushed.");
                }
            }
        );
    }
}

f:id:yossk:20141213234004j:plain

今日の感想

コード書いて、動作確認すると動きは分かるのだけれど、正直合間合間のAndroidの仕組みの話は難しくてまだ理解できていない。
繰り返し復習が必要だ。