b. android SDK 1

android OS 端末用アプリケーションの開発について

使用環境:

D:ドライブにインストールされた android SDK と eclipse を使用。

ADT (android development tools) プラグインを通して、java でアプリケーションを開発する。

d:ドライブのadt-bundle-windows-x86_64-20131030 フォルダを参照。

android device エミュレータ:

参照: http://qiita.com/gabu/items/8bc1a11f1382409f1d2a

intel が提供するHAXM を利用し、 atom cpu device用 android image を x86 CPU でエミュレートする。

課題1:

各自で、eclipse の avd managerを使用して仮想デバイスを作成する(手順を演習中に示す)。

eclipse の workplace は、 D:ドライブに 学籍番号 のフォルダで作成し、課題作成後レポートフォルダに提出する。

端末名 advprog2

OS android4.3

CPU atom

メモリ 768MB

host 側 GPU 使用

課題2:

avd avdprog2を起動。

android OS の環境確認(アプリの起動、終了。ホームボタン、戻るボタン など)

課題3:

Helloworld プロジェクト。

File -> new から android application project を作成し、avdから起動。

課題4:

Myworld プロジェクト。

3と同様にプロジェクト作成。

res の layout 内の activity_main.xml ファイルをダブルクリックして、

Graphical Layout を起動。

GUI部品を配置して動作確認。

課題5:

コードによる activity の作成。

Sample2013_12 プロジェクト。

参照サイト:

http://techbooster.org/android/ui/9039/

http://techbooster.org/android/ui/12424/

http://techbooster.org/android/ui/5812/

下のコードは、3番目の記事に対応。

MainActivity.java の内容。

package com.example.sample2013_12;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
public class MainActivity extends ExpandableListActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//setContentView(R.layout.activity_main);

List<Map<String, String>> groupList = new ArrayList<Map<String,String>>();

        List<List<Map<String, String>>> childList = new ArrayList<List<Map<String,String>>>();
        for (int i = 0; i < 3; i++) {
            // Group(親)のリスト
            Map<String, String> groupElement = new HashMap<String, String>();
            groupElement.put("GROUP_TITLE", "Group " + i);
            groupList.add(groupElement);
            // Childのリスト
            List<Map<String, String>> childElements = new ArrayList<Map<String, String>>();
            for (int j = 0; j < 5; j++) {
                Map<String, String> child = new HashMap<String, String>();
                child.put("CHILD_TITLE", "Child " + j);
                child.put("SUMMARY", "Summary " + j);
                childElements.add(child);
            }
            childList.add(childElements);
        }
        
        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
                this,
                // Group(親)のリスト
                groupList,
                // Group(親)のレイアウト
                android.R.layout.simple_expandable_list_item_1,
                // Group(親)のリストで表示するMapのキー
                new String []{"GROUP_TITLE"},
                // Group(親)のレイアウト内での文字を表示するTextViewのID
                new int []{android.R.id.text1},
                // Child(子)のリスト
                childList,
                // Child(子)のレイアウト
                android.R.layout.simple_expandable_list_item_2,
                // Child(子)のリストで表示するMapのキー
                new String []{"CHILD_TITLE", "SUMMARY"},
                // Child(子)のレイアウト内での文字を表示するTextViewのID
                new int []{android.R.id.text1, android.R.id.text2}
        );
        setListAdapter(adapter);

}

// 以下のメソッドは、課題6用のもの。課題5には不要。 @Override

public boolean onChildClick(ExpandableListView parent, View v,

int groupPosition, int childPosition, long id) {


        Intent intent=new Intent();
        intent.setClassName("com.example.sample2013_12","com.example.sample2013_12.Memo");
        String msg = String.format("%d : %d",groupPosition,childPosition);
        intent.putExtra("com.example.sample2013_12.intent.testString", msg);
        startActivity(intent);

return super.onChildClick(parent, v, groupPosition, childPosition, id);

}

}

課題6:

intent による、activity間の遷移。

Sample2013_12 プロジェクトを修正して、Memo Activity を作成。リストメニューから表示する様にコードを修正。

参照サイト:

http://techbooster.org/android/application/8346/

http://www.javadrive.jp/android/intent/

AndroidManifest.xml の内容。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sample2013_12"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sample2013_12.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       <activity
           android:name=".Memo"
           android:label="@string/app_name" >
       </activity>
        
    </application>
</manifest>

Memo.java の内容。

package com.example.sample2013_12;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
public class Memo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String str = null;
        if(intent != null){
            str = intent.getStringExtra("com.example.sample2013_12.intent.testString");

}

        EditText textview = new EditText(this);
        textview.setText(str);
        setContentView(textview);
    }
}

課題7:

android の GUIコンポーネントについて調べ、activityに配置し動作を確認する。

例)

課題6の Memo.java で利用している GUIコンポーネント EditText を別のテキストコンポーネントに変更し、利用するなど。