Download Programming for Android - AUEB e

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Programming for Android
Boutsis Ioannis
[email protected]
History
●
●
Android is developed by the Open Handset Alliance led by
Google.
Google purchased the initial developer of the software,
Android Inc., in 2005.
Statistics
Architecture
●
Applications
All applications are written using the Java programming language.
●
Application Framework
By providing an open development platform, Android offers developers the ability to build extremely rich and
innovative applications. Developers are free to take advantage of the device hardware, access location information,
run background services, set alarms, add notifications to the status bar, and much, much more.
Developers have full access to the same framework APIs used by the core applications.
●
Libraries
Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are
exposed to developers through the Android application framework.
Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. Dalvik has
been written so that a device can run multiple VMs efficiently. The Dalvik VM relies on the Linux kernel for
underlying functionality such as threading and low-level memory management.
●
Linux Kernel
Android relies on Linux version 2.6 for core system services such as security, memory management, process
management, network stack, and driver model. The kernel also acts as an abstraction layer between the hardware
and the rest of the software stack.
Architecture
Components
●
●
●
●
●
Activities are basically a graphical component and there
should be a different activity for each UI screen of the
application.
Services are used as background tasks (e.g. playing music
on the background) and they can be bind with a specific
activity.
Content Providers are used to manage access on persistent
data that can be shared on different applications such as the
contacts phone numbers.
Broadcast Receivers are used to receive a notification and
respond to that such as when receiving an SMS where the
appropriate Activity should be invoked.
Components communicate with messages called Intents,
which are routed through Android runtime and the Kernel.
Programming...
●
Android Manifest
●
XML based User Interface
●
Java Code
http://developer.android.com/guide/topics/ui/index.html
Android Manifest
●
●
●
●
Every application must have an AndroidManifest.xml file (with
precisely that name) in its root directory.
The manifest presents essential information about the
application to the Android system.
Activities, Permissions, Minimum Android Level, etc. should
be declared in the manifest.
More Info:
http://developer.android.com/guide/topics/manifest/manifest-intro.html
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".TestAndroidActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name=".Activity2"
</activity>
</application>
</manifest>
>
Resources(/res)
●
/drawables (low, medium, high)
●
/layout (xml Uis)
●
/values eg string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="ds2011">Distributed Systems</string>
<string name="button_enter">Enter</string>
<string name="click_android">Click on the android</string>
<string name="changed_text">Text to be changed</string>
<string name="app_name">TestAndroid</string>
</resources>
Layouts
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/
apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ds2011"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center_vertical|center_horizontal"/>
<ImageView
android:id="@+id/androids"
android:layout_width="234dp"
android:layout_height="246dp"
android:src="@drawable/android1" />
<Button
android:id="@+id/button1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/button_enter" />
</LinearLayout>
Layouts
activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/
apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#330000"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/talking_android"
android:layout_width="180sp"
android:layout_height="80sp"
android:layout_marginBottom="50sp"
android:src="@drawable/android_look" />
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textSize="16sp"
android:textStyle="italic"
android:text="@string/click_android" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:background="#000066"
android:textColor="#ffcc00"
android:typeface="monospace"
android:text="@string/changed_text" />
</LinearLayout>
Java Code
TestAndroidActivity.java
package com.test.android;
import
import
import
import
import
android.app.Activity;
android.content.Intent;
android.os.Bundle;
android.view.View;
android.widget.Button;
public class TestAndroidActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),
Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Java Code
package com.test.android;
import
import
import
import
import
import
import
import
import
Activity2.java
private void androidClick() {
final AlertDialog.Builder alert = new
AlertDialog.Builder(this);
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Change text", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
whichButton) {
String value =
input.getText().toString().trim();
changeText(value);
}
});
android.app.Activity;
android.app.AlertDialog;
android.content.DialogInterface;
android.os.Bundle;
android.view.View;
android.widget.EditText;
android.widget.ImageView;
android.widget.TextView;
android.widget.Toast;
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
whichButton) {
dialog.cancel();
}
});
final ImageView iv =
(ImageView)findViewById(R.id.talking_android);
alert.show();
// set the listener
iv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
androidClick();
}
});
}
private void changeText(String value) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(value);
Toast.makeText(getApplicationContext(), "Text changed!",
Toast.LENGTH_SHORT).show();
}
}
}
Java Code
Διευθυνσιοδότηση
●
●
●
●
Στα κινητά οι διευθύνσεις ip δίνονται από το δίκτυο.
Για όσους χρησιμοποιούν τον emulator οι διευθύνσεις ip που χρησιμοποιεί είναι
διαφορετικές από αυτές του υπολογιστή
http://developer.android.com/guide/developing/devices/emulator.html#emulatornet
working
Έτσι, για να συνδεθεί ένας client στο ServerSocket του emulator πρέπει να γίνουν
τα εξής:
–
-το serverSocket του emulator ανοίγει στο port που θέλουμε(έστω 5000)
–
-Στη συνέχεια πρέπει να κάνουμε redirect κάποιο port σε αυτό.
–
Ανοίγουμε ένα telnet στο port που τρέχει ο emulator(πχ 5554) με telnet
localhost 5554
–
και εκτελούμε redir add tcp:5001:5000
–
-Τώρα κάθε client μπορεί να δημιουργήσει ένα socket για να συνδεθεί
στο αρχικό serverSocket με IP/port: 10.0.2.2:5001
http://developer.android.com/guide/developing/devices/emulator.html#connecting
Related documents