Android: Difference between revisions

From 太極
Jump to navigation Jump to search
Line 193: Line 193:
* See [http://stackoverflow.com/questions/3572463/what-is-context-in-android stackoverflow].
* See [http://stackoverflow.com/questions/3572463/what-is-context-in-android stackoverflow].
* Beginning android application book Chapter 2: Activities, fragments and intents -> Understanding Activities -> Displaying a Dialog Window.
* Beginning android application book Chapter 2: Activities, fragments and intents -> Understanding Activities -> Displaying a Dialog Window.
== Rotate AVD screen ==
# Enable NUMLOCK;
# Open your emulator and press the 7 followed by 9 on the NUMPAD on the right side of your keyboard;


= Android nerd ranch book =
= Android nerd ranch book =

Revision as of 00:09, 17 February 2014

SDK

http://developer.android.com/sdk/index.html#ExistingIDE

Redhat

At the step of Running your app I cannot run app from Eclipse. The command line way does not work.After several tries, it seems the RHEL does not have an updated Ant package (currently it is 1.7 but I need 1.8). Give up.

Ubuntu

Download the bundle version. The file name I got is <adt-bundle-linux-x86_64-20131030.zip>.

On 64-bit Ubuntu, we need to run

sudo apt-get install libstdc++6:i386 lib32z1 lib32ncurses5 lib32bz2-1.0

For both 32-bit and 64-bit, we need to get openjdk

sudo apt-get install openjdk-6-jre openjdk-6-jdk icedtea6-plugin

Then uncompress <adt-bundle-linux-xxx.zip> and run the file <eclipse> from either file manager or command line. It just works!

We can furthermore set up the PATH variable by editing ~/.bashrc file.

export PATH=$PATH:~/adt-bundle-linux-x86_64-20131030/sdk/platform-tools/
export PATH=$PATH:~/adt-bundle-linux-x86_64-20131030/sdk/tools/

Then source the file by

source ~/.bashrc

A good reference is https://help.ubuntu.com/community/AndroidSDK.

Windows

Genymotion

Genymotion is a complete set of tools that provides a virtual environment for Android. Genymotion is much better than the ADV included in Android SDK. Before we use download/use it, we need to register.

For some reason, on my Ubuntu 12.04, I need to run the following.

sudo apt-get install ia32-libs

The genymotion can be integrated into Eclipse. The document about genymotion can be found on https://cloud.genymotion.com/page/doc/

Qt + Android

Installation

On Ubuntu we can install requirements by

  • SDK -> No need to download a special copy. Just use adt-bundle-linux-x86-2013xxxx one.
  • Apache Ant -> sudo apt-get install ant
  • NDK -> android-ndk-r9c-linux-x86.tar.bz2. Use tar -xjvf android-ndk-r9c-linux-x86.tar.bz2 to extract.
  • Download qt-linux-opensource-5.2.0-android-x86-offline.run and install it into a new location, eg ~/Qt5.2.0Android (Cannot be the same as Desktop version).
  • On a new machine, we need to install opengl library as instructed here and here.
sudo apt-get install mesa-common-dev
sudo apt-get install libglu1-mesa-dev -y

The Qt needs to change the setting from Tools > Options > Android where we can specify the locations of Android SDK, Android NDK, Ant and JDK. On Tools > Options > Devices, we can change the Device from default (Local PC) to Run on Android. See the instruction on qt.project.org. Basically we go to project's setting to do changes. It does take a little longer to get the app on device.

Help, Applications

Books/Tutorials

Android Programming: The Big Nerd Ranch Guide

It seems the solution files still have bugs. For example in Chapter 1 "01_FirstApp", the <layout/activity_quiz.xml> file should have

android:text="@string/question_text_view"

instead of

android:id="@+id/question_text_view"

within <TextView> tag.

<AndroidProgramming.zip>

Beginning Android 4 Application Development

<Source_code_download_for_Beginning_Android_4_AD.zip>

Android Tutorials

Java Books

  • Head First Object-Oriented Analysis and Design
  • Head First Design Patterns

Useful Apps

Network Signal Info

Play store

CPU-Z

Github

The source code is hosted on https://github.com/github/android

Bitbucket

Basic(s)

Check connected devices

Enable USB debugging on your device first.

$ sdk/platform-tools/adb devices
List of devices attached 
2011060116402004	device

Where to find the apk files on devices

See here. It is located under /data/app/.

The Android device needs

  • If connecting for the first time, User is prompted to install the drivers in which case, point to $ANDROID_SDK_PATH/usb_driver, the drivers will get installed.
  • Change your settings to permit apps that don’t come from the Google Market by going to the home screen and choosing MENU > Settings > Applications > Unknown Sources.
  • Turn USB debugging ON on your phone, On your device go to the home screen, press MENU > Settings > Applications > Development, then enable USB debugging.

How to copy an apk file out to PC

Use adb pull. For example,

adb pull data/app/com.dotgears.flappybird-1.apk .

How to put an apk file on device

Use adb install. For example,

adb install com.dotgears.flappybird-1.apk 

How to uninstall/delete apk files on devices

http://www.botskool.com/geeks/how-uninstall-apk-files-android-windows-sdk-emulator

cd SDK/tools
adb shell ls data/app
adb shell rm data/app/com.androidcan.asudoku.apk

Failed to install HelloWorld.apk on device (null) Error

http://stackoverflow.com/questions/5229906/android-failed-to-install-helloworld-apk-on-device-null-error

Solution: Try changing the ADB connection timeout. I think it defaults that to 5000ms and I changed mine to 10000ms to get rid of that problem. If you are in Eclipse, you can do this by going through Window -> Preferences and then it is in DDMS under Android.

Build without running an application

Building vs running: Building will just generate apk file but running will install the app to the device.

See here. In Eclipse menu, Windows -> Preferences. From this Preferences window: Android -> Build. Now uncheck "Skip packaging and dexing until export or launch. (Speeds up automatic builds on file save.)". From now on, as long as we save any changes in source files, the apk file will be recreated.

Modified the 'name' shown on Eclipse Package Explorer

Modify the name tag in <.project> file (hidden file).

Structure of an App project

Before we create a new project in Eclipse, we have to enter some information.

  • Application Name: the name that appears to users
  • Project Name: the name of project directory and the name visible in Eclipse
  • Package name: com.example.GUIDemo
  • Minimum Required SDK: API 8
  • Target SDK: API 18
  • Compile With: API 19 (By default, this is set to the latest version of Android available in your SDK)
  • Activity Name: MainActivity (will create <MainActivity.java> and MainActivity class)
  • Layout Name: activity_main (will create <activity_main.xml>)
  • Navigation type: None
src +-- com.example.guidemo
           +-- MainActivity.java

res +-- layout +-- activity_main.xml
       +-- values +-- strings.xml
AndroidManitest.xml
proguard-project.txt
project.properties
.project

Command Line

$ android update project -p .
$ ant debug            // create an .apk file
$ adb install bin/your-project-name-debug.apk // only install, will not run

The Context Object

  • See stackoverflow.
  • Beginning android application book Chapter 2: Activities, fragments and intents -> Understanding Activities -> Displaying a Dialog Window.

Rotate AVD screen

  1. Enable NUMLOCK;
  2. Open your emulator and press the 7 followed by 9 on the NUMPAD on the right side of your keyboard;

Android nerd ranch book

01_FirstApp_GeoQuiz Project

activity_quiz.xml

Note that

  • A layout is a resource, a piece of your application that is not code.
  • To generate a resource ID for a widget, you include android:id attribute in the widget's definition.
  • There is a + sign in the values for android:id but not n the values for android:text. This is because you are creating the IDs and only referencing the strings.
  • Since we want to interact with 2 buttons in code, so only they need resource IDs.
  • All resource IDs can be found in <gen/com.example.geoquiz/R.java>.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center"
  android:orientation="vertical" >

  <TextView    
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/question_text_view"
    android:padding="24dp" />
  
  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
      android:id="@+id/true_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/true_button" />

    <Button
      android:id="@+id/false_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/false_button" />

  </LinearLayout>

</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <string name="app_name">GeoQuiz</string>
  <string name="question_text_view">The Pacific Ocean is larger than the Atlantic Ocean.</string> 
  <string name="true_button">True</string>
  <string name="false_button">False</string>  
  <string name="correct_toast">Correct!</string>
  <string name="incorrect_toast">Incorrect!</string>
  <string name="menu_settings">Settings</string>

</resources>

QuizActivity.java

  • Add two member variables to access the buttons (through resource IDs)
  • Notice the prefix m on the member variable names.
  • Import the class android.widget.Button for the buttons. It can be added by organizing imports. This can be done by keyboard shortcut Ctrl + Shift + O.
  • To get references to widgets, we use the Activity method: findViewById(int id).
  • To set listeners (review Java!), use setOnClickListener(OnClickListener) method; something like
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 // Does nothing yet.
            }
        });
  • Making toasts - a pop-up message. To create a toast, you call the method: makeText(Context context, int resId, int duration). The 2nd parameter is the resource ID of the string that the toast should display. After created a toast, you call Toast.show() on it to get it on screen.

So the complete java code looks like

package com.bignerdranch.android.geoquiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class QuizActivity extends Activity {

    Button mTrueButton;
    Button mFalseButton;

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

        mTrueButton = (Button)findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(QuizActivity.this, 
                        R.string.incorrect_toast, Toast.LENGTH_SHORT)
                        .show();
            }
        });

        mFalseButton = (Button)findViewById(R.id.false_button);		
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(QuizActivity.this, 
                        R.string.correct_toast, Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_quiz, menu);
        return true;
    }

}

02_MVC_GeoQuiz

Works.

03_ActivityLifecycle_GeoQuiz

Crash AVD for some reason. The whole practice is the same as Chapter 2 in Beginning Android 4 Application Development (the code works there).

05_SecondActivity_GeoQuiz

07_UIFragments_CriminalIntent

  • One critical difference between the fragment lifecycle and the activity lifecycle is the fragment lifecycle methods are called by the hosting activity, not the OS.