Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Friday, 13 July 2012

Starting an new Activity

Before starting a new activity from a current one, you need to setup the classes.

Intent display = new Intent(this, DisplayMessageActivity.class);
EditText editText = findViewById(R.id.edit_text);
String message = editText.getText().toString();
display.putExtra(EXTRA_MESSAGE, message);
startActivity(display);

From the DisplayMessageActivity then, you can get the extra like this.

Intent intent = getIntent();
String message = intent.getStringExtra(PreviousActivityClassname.EXTRA_MESSAGE);

Thursday, 12 July 2012

Android UI Weight

Android weights are like drink mixes, "2 parts vodka, 1 part coffee liqueur". If you set one widget to a weight of 2 and another to a weight of 1 - the first will get 2/3rds of the space and the other will get 1/3rd.
When using weights, it's a good idea to set the layout_width to 0 to improve performance. It improves performance because it doesn't need to work out something that ultimately is irrelevant.

Sunday, 8 July 2012

Android UI basic

Basic LinearLayout

<?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="wrap_content" >
</LinearLayout>


When this code is saved, the ADK will use the Android Asset Packaging Tool (AAPT) to generate R.java

In Android, a widget is called a View and a layout container like LinearLayout is called a ViewGroup. Above has created an empty ViewGroup

Asking a Question

In order to ask a question, we'll need to add a TextView to the top of the layout. To add this, put the following in between the LinearLayout element.

<TextView
    android:id="@+id/question"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Please wait..."
  />

Now for the answers we're going to want multiple choices. If we use buttons for starters that'll do, but since we can't set a variable number of buttons we'll need to contain them. So let's create a new LinearLayout.

<LinearLayout 
    android:id="@+id/answers"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</LinearLayout>

Then, for each button we want something similar to this:

<Button
    android:id="@+id/yes"
    android:text="Yes!"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

Monday, 2 July 2012

Android Services

A Service is a process that runs in the background, without interacting with the user. Android provides some pre-defined services which can be accessed through getSystemService().

To declare your own service, it must be defined in the manifest as a service and the implementing class must extend Service or one of its subclasses. A service will not automatically run in its own thread without the process attribute. They by default run in the main thread of the hosting process. To define the process, in the manifest set the process under the application tab. An example is :my_service. The colon at the start tells android that the process is private to declaring applications. Otherwise the process would be globally accessible.

An Activity can start the Service by calling the startService() method and stop the service via the stopService() method. After a service is started the onCreate() method is called and later the onStartCommand gets invoked with the Intent.

Starting services automatically


To start services automatically after boot involves registering a BroadcastReceiver to the Android android.intent.action.RECEIVE_BOOT_COMPLETED system event. This requires the RECEIVE_BOOT_COMPLETED permission. In the broadcast receiver class, override the onReceive() method and call the service like:

Intent service = new Intent(context, MyService.class); context.startService(service);

Related Information

Android Networking
Android Services

Friday, 22 June 2012

Android SSL/TLS

The javax.net.ssl packages provide all the necessary classes and interfaces required to program secure socket abstractions based on the SSL protocols SSLv3.0 / TLSv1.1.
All of the details of the SSL handshake protocol are accounted for and the client or a server can specify the cipher set to use. X.509 Certificates are verified and if desired, the client and server each have the option of verifying the entire certificate chain until the root Certificate Authority is reached.
Android uses code from the Legion of the Bouncy Castle and OpenSSL.