platform specials

How to make a phone call from your application

SDK Version: 
M3

To enable your application to initiate a phone call, you must set permissions in the manifest file:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.bubudsadasdas"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".phonecalls"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.  
  15.     </application>
  16.     <uses-sdk android:minSdkVersion="3" />
  17. <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
  18. </manifest>

Now create a new activity with a call method. It should look like this:
 

Android unit testing

SDK Version: 
M3

For unit testing you can use the built in JUnit framework just like in "standard" java applications.

A unit test is to test some low-level part - usually a single class - of a project, working individually.

The only trick is to extend AndroidTestCase in your test case instead of TestCase some Android specific methods, like getContext() which is often required by android functions.

Lets see a very simple example using eclipse:

- Create a new project to test, create a new class in it, with a simple method that , like this:

  1. public class ClassToTest{
  2.        
  3.    public int add(int arg1,int arg2){
  4.            return arg1+arg2;
  5.    }
  6.    
  7. }

- Create a new test project: File/new/Other.../Android/Android Test Project


new Android Test Project

Add the previously created project as the test target, the other fields can be left as they are auto filled.

Calling system settings from an Android app - GPS example

SDK Version: 
M3
This tutorial shows how to redirect the user to a system settings screen asking to modify some settings the application depends on. We will make a specific example with GPS: The application can be used only if GPS is available.

The android systems GPS setting screen can be called just like any other Activities:

  1. startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);

To check GPS availability use the code code below: (the Activity must implement LocationListener)

Is this the first run?

SDK Version: 
M3

Ever wanted to have a different flow of actions on the second or third run of your app? I had that many times, in almost every project that I haver worked on. So here is a little snippet, that you can use to store, and check the fact, if this is the first run of your app. You can modify it easily, to suit your needs.

How to display a custom dialog in your Android application

SDK Version: 
M3

How to display a custom dialog in your Android application

Yesterday Jozsi showed you, how to make an alert dialog, today I'm going to show you, how to make a custom dialog/popup window.
Sometimes, it's better to make your own dialog, because this way, you can display whatewer you want., the way you want it.
First, make your own layout, with the needed elements. Here, I'm going to use two buttons, a textview inside a scrollview, and an imageview...

How to display an AlertDialog in your Android application

SDK Version: 
M3

Here is an example of an applacation, that illustrates how you can create a simple AlertDialog. The dialog can display maximum three buttons.



Connecting to MySQL database

SDK Version: 
M3
The most spread method to connect to a remote MySQL database from an android device, is to put some kind of service into the middle. Since MySQL is usually used together with PHP, the easiest and most obvious way to write a PHP script to manage the database and run this script using HTTP protocol from the android system. mysql logo

We can code the data in JSON format, between Android and PHP with the easy to use built in JSON functions in both languages.

I present some sample code, which selects data from a database depending on a given condition and creates a log message on the android side with the received data.

Lets suppose that we have a MySQL database named PeopleData, and a table int created, with the following SQL:

  1. CREATE TABLE `people` (
  2. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,

Using an Android phone's sensors

SDK Version: 
M3

Accessing the sensor data of a phone, is not too complicated, but since API level 3, a lot of things got deprecated. Using only the emulator, you have only a few (here is one) options, to simulate sensor data. I would recommend the use of a physical phone. Keep in mind, that not all phones have the same sensors integrated! Cheaper phones might not have a temperature sensor, or a gyroscope, but I'm pretty sure, that all Android phones have at least an accelerometer, and an orientation sensor.

Here is an example, how to access acceleration and orientation sensor data (in API level 3 and above), without using any deprecated methods.

Mastering Android Widget Development - Part5 - Final

SDK Version: 
M3

In this last part of the tutorial we will implement buttons to the appWidget, which will directly interact with the appWidget functionality.
We will have 2 buttons, a plus button to add one more day to the target date, and a minus button to decrease time left by one day.

First add the buttons to the countdownwidget.xml layout:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.   xmlns:android="http://schemas.android.com/apk/res/android"
  4.   android:layout_width="wrap_content"
  5.   android:layout_height="wrap_content" android:orientation="vertical">
  6.  
  7. <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Time left"></TextView>
  8. <LinearLayout android:id="@+id/LinearLayout01"; android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">

Hello Baby widget feature update - notification tutorial

SDK Version: 
M3

We have updated our Hello Baby widget, with a notification feature. So I'm using this opportunity, to show you how to use notifications with widgets.

Hello readers, my name is Tamas, and I'm the newest member of the HelloAndroid.com team :)
Today we decided to update our Hello Baby widget, with a notification feature. So I'm using this opportunity, to show you how to use notifications with widgets.
The plan was to alert the user with a status bar notification 1,2,3 and 7 days before the days-left counter reaches 0.
Syndicate content