Anda di halaman 1dari 17
MODUL PERKULIAHAN MOBILE PROGRAMMING Universitas Mercu Buana 2015 Fakultas Program Studi Tatap Muka Kode MK

MODUL PERKULIAHAN

MOBILE

PROGRAMMING

Universitas Mercu Buana

2015

Fakultas

Program Studi

Tatap Muka

Kode MK

Disusun Oleh

Ilmu Komputer

Teknik Informatika

10

87036

Abdul Khaliq Arrachman,S.Kom.,M.Kom

Abstract

Android merupakan Operating System

yang dikembangkan oleh Google untuk kepentingan handphone layar sentuh seperti smartphone dan tablet.

.

Kompetensi

Mahasiswa dapat menjelaskan isi pokok dari mata kuliah teori dan aplikasi dari mobile programming serta membuat Aplikasi Android.

MOBILE PROGRAMMING ANDROID

1. LOCATION BASE SERVICE (LBS)

LBS adalah suatu teknologi yang saat ini umum digunakan oleh mobile Android, LBS dengan teknologi GPS memudahkan kita untuk mengetahui dimana letak posisi kita berada.

Satelit yang saat ini berpartisipasi adalah GPS america, GLONASS rusia, Baidu Cina (gratis) dengan mempunyai salah satu feature di smartphone diatas maka semakin baik untuk mengetahui posisi kita terakhir,

GLONASS saat ini mempunyai 18 satelit , semakin banyak satelit makin akurasi keberadaan GPS

Selain satelit ada juga A-GPS ini adalah GPS yang berasal dari Network provider dan ini membutuhkan pulsa.

Berikut ini adalah sample dari Aplikasi GPS

1.1.GPS Aplikasi

dari Network provider dan ini membutuhkan pulsa. Berikut ini adalah sample dari Aplikasi GPS 1.1.GPS Aplikasi

Langkah-langkah membuat GPS.

1.Activity Mylocation

package com.mercu.gps;

import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast;

public class Mylocation extends Activity { private LocationManager lm; private LocationListener locListener; private TextView latTxt, lonTxt;

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

latTxt = (TextView) findViewById(R.id.latitudeTxt); lonTxt = (TextView) findViewById(R.id.longitutdeTxt);

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locListener = new MyLocationListener(); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3, 10, locListener);

register

/*

Parameters :

First(provider)

Second(minTime)

:

the name of the provider with which to

: the minimum time interval for

notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.

Third(minDistance) : the minimum distance interval for notifications, in meters

Fourth(listener)

: a {#link LocationListener} whose

onLocationChanged(Location) method will be called for each location update */

}

private class MyLocationListener implements LocationListener {

/************* Called after each 3 sec **********/ @Override public void onLocationChanged(Location loc) { // TODO Auto-generated method stub if (loc != null) { latTxt.setText(String.valueOf(loc.getLatitude())); lonTxt.setText(String.valueOf(loc.getLongitude()));

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

Toast.makeText( getBaseContext(), "Location Changed : Lat " +

loc.getLatitude() +

Toast.LENGTH_SHORT).show();

}

}

"lgt: "+ loc.getLongitude(),

@Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub /******** Called when User off Gps *********/

Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();

}

@Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub

/******** Called when User on Gps *********/

Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();

}

@Override public void onStatusChanged(String provider, int status, Bundle

extras) {

// TODO Auto-generated method stub

}

}

}

Perhatikan parameternya!!

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locListener = new MyLocationListener(); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3, 10, locListener);

Parameters :

First(provider)

Second(minTime)

milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.

Third(minDistance) : the minimum distance interval for notifications, in meters

Fourth(listener)

method will be called for each location update */

: a {#link LocationListener} whose onLocationChanged(Location)

: the name of the provider with which to register : the minimum time interval for notifications, in

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

PerHatikan On Location change akan terpanggil tiap 3 detik / jarak berubah

/************* Called after each 3 sec **********/ @Override public void onLocationChanged(Location loc) { // TODO Auto-generated method stub if (loc != null) { latTxt.setText(String.valueOf(loc.getLatitude())); lonTxt.setText(String.valueOf(loc.getLongitude()));

Toast.makeText( getBaseContext(), "Location Changed : Lat " +

loc.getLatitude() +

Toast.LENGTH_SHORT).show();

}

}

"lgt: "+ loc.getLongitude(),

2.Buat Layout 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:orientation="vertical" >

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lonText" android:textStyle="bold" > </TextView>

<TextView android:id="@+id/longitutdeTxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/unknown" />

<TextView

android:id="@+id/textView3"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/latText" android:textStyle="bold" > </TextView>

<TextView android:id="@+id/latitudeTxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/unknown" > </TextView>

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

3.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mercu.gps"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >

</uses-permission> <uses-permission android:name="android.permission.INTERNET" > </uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name" > <activity android:name="com.mercu.gps.Mylocation" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>

</manifest>

Perhatikan ini harus ada

ACCESS_COARSE_LOCATION : izin dari cellphone atau wifii

ACCESS_FINE_LOCATION : izin dari GPS/Glonass/Baidu

INTERNET:izin koneksi internet

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >

</uses-permission> <uses-permission android:name="android.permission.INTERNET" > </uses-permission>

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

4. di folder res\values\ String.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">My Location</string> <string name="app_name">GPS</string> <string name="latText">Latitude :</string> <string name="lonText">Longitude :</string> <string name="unknown">Unknown</string> </resources>

2.Google MAP version 2

string > </ resources > 2.Google MAP version 2 Gambar 2.Google Map V2 base on GPS

Gambar 2.Google Map V2 base on GPS

Google map ini dapat kita manfaatkan menunjuk alamat GPS yang kita punya sehingga dapat map

yang akurat.

Langkah-langkah membuat google map v2

1. Install google play services dan google usb driver

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id
Gambar 3. Install tool google map di SDK manager 2.Import google play service dari

Gambar 3. Install tool google map di SDK manager

2.Import google play service dari

android-sdk-windows\extras\google\google_play_services\libproject\google-play-

services_lib

services_lib Gambar 4.Import Library google play services 2015 10 Mobile

Gambar 4.Import Library google play services

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id
Gambar 5a.Centang V is Library Pada aplikasi yang dibuat properties Mercu10_GoogleMapV2 masukan library tsb Gambar

Gambar 5a.Centang V is Library

Pada aplikasi yang dibuat properties Mercu10_GoogleMapV2 masukan library tsb

Gambar 5b.Tambahkan library map 2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan
Gambar 5b.Tambahkan library map
2015
10 Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

3.Membuat / mendapatkan Google API KEY Buka command.com jalankan script dibawah dan catat SHA1 untuk didaftarkan ke google API Key

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

-alias androiddebugkey -storepass android -keypass android Gambar 6.SHA1 dicatat 4.Buka google developer console

Gambar 6.SHA1 dicatat 4.Buka google developer console https://console.developers.google.com

developer console https://console.developers.google.com Gamabar 7.Ikut langkah1 daftar API key 2015 10 Mobile
Gamabar 7.Ikut langkah1 daftar API key 2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan
Gamabar 7.Ikut langkah1 daftar API key
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id
Gambar 7.langkah 2 masukan SHA1 dan pakage di aplikasi android yang telah dibuat Gambar 8.API

Gambar 7.langkah 2 masukan SHA1 dan pakage di aplikasi android yang telah dibuat

SHA1 dan pakage di aplikasi android yang telah dibuat Gambar 8.API key hasil generated by Google

Gambar 8.API key hasil generated by Google Map Api Key Hasil ini nantinya dimasukan di Manifest.xml Aplikasi

<!-- Goolge API Key --> <meta-data

android:name="com.google.android.maps.v2.API_KEY"

android:value="AIzaSyAnY8y6mdVyNCB2j8KMDFnhWZqXlPwHovQ" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

Google maps needs following permissions and features.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 --> <uses-feature

android:glEsVersion="0x00020000"

android:required="true" />

<!-- Requires OpenGL ES version 2 --> <uses-feature

android:glEsVersion="0x00020000"

android:required="true" />

Untuk Aplikasinya Sbb:

1.Buat Main Activity

package com.mercu.googlemapsv2;

import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.CameraPosition; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {

// Google Map private GoogleMap googleMap;

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

try {

// Loading map

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

initilizeMap();

// Changing map type googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); // googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); // googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);

// Showing / hiding your current location googleMap.setMyLocationEnabled(true);

// Enable / Disable zooming controls googleMap.getUiSettings().setZoomControlsEnabled(false);

// Enable / Disable my location button googleMap.getUiSettings().setMyLocationButtonEnabled(true);

// Enable / Disable Compass icon googleMap.getUiSettings().setCompassEnabled(true);

// Enable / Disable Rotate gesture googleMap.getUiSettings().setRotateGesturesEnabled(true);

// Enable / Disable zooming functionality googleMap.getUiSettings().setZoomGesturesEnabled(true);

double latitude = -6.22875311; double longitude = 106.71580335;

// lets place some 10 random markers for (int i = 0; i < 10; i++) { // random latitude and logitude double[] randomLocation = createRandLocation(latitude, longitude);

// Adding a marker MarkerOptions marker = new MarkerOptions().position( new LatLng(randomLocation[0],

randomLocation[1]))

.title("Hello Maps " + i);

Log.e("Random", "> " + randomLocation[0] + ", " + randomLocation[1]);

// changing marker color if (i == 0) marker.icon(BitmapDescriptorFactory

.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)); if (i == 1) marker.icon(BitmapDescriptorFactory

.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)); if (i == 2) marker.icon(BitmapDescriptorFactory

.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)); if (i == 3)

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

marker.icon(BitmapDescriptorFactory

.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); if (i == 4) marker.icon(BitmapDescriptorFactory

.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); if (i == 5) marker.icon(BitmapDescriptorFactory

.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)); if (i == 6) marker.icon(BitmapDescriptorFactory

.defaultMarker(BitmapDescriptorFactory.HUE_RED)); if (i == 7) marker.icon(BitmapDescriptorFactory

.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)); if (i == 8) marker.icon(BitmapDescriptorFactory

.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)); if (i == 9) marker.icon(BitmapDescriptorFactory

.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

googleMap.addMarker(marker);

// Move the camera to last position with a zoom level if (i == 9) { CameraPosition cameraPosition = new

CameraPosition.Builder()

LatLng(randomLocation[0],

.target(new

randomLocation[1])).zoom(15).build();

googleMap.animateCamera(CameraUpdateFactory

.newCameraPosition(cameraPosition));

}

}

} catch (Exception e) { e.printStackTrace();

}

}

@Override protected void onResume() { super.onResume(); initilizeMap();

} /** * function to load map If map is not created it will create
}
/**
* function to load map If map is not created it will create it for you
*
*/
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

private void initilizeMap() { if (googleMap == null) { googleMap = ((MapFragment) getFragmentManager().findFragmentById( R.id.map)).getMap();

// check if map is created successfully or not if (googleMap == null) { Toast.makeText(getApplicationContext(), "Sorry! unable to create maps",

Toast.LENGTH_SHORT)

}

}

}

.show();

/* * creating random postion around a location for testing purpose only */ private double[] createRandLocation(double latitude, double longitude) {

return new double[] { latitude + ((Math.random() - 0.5) / 500), longitude + ((Math.random() - 0.5) / 500), 150 + ((Math.random() - 0.5) * 10) };

}

}

2.Layout activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" >

<fragment

android:id="@+id/map"

android:name="com.google.android.gms.maps.MapFragment"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</RelativeLayout>

3.Layout AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.mercu.googlemapsv2"

android:versionCode="1"

android:versionName="1.0" >

<permission

android:name="com.mercu.googlemapsv2.permission.MAPS_RECEIVE"

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

android:protectionLevel="signature" />

<uses-permission android:name="com.mercu.googlemapsv2.permission.MAPS_RECEIVE"

/>

<uses-sdk

android:minSdkVersion="12"

android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 --> <uses-feature

android:glEsVersion="0x00020000"

android:required="true" />

<!-- Requires OpenGL ES version 2 --> <uses-feature

android:glEsVersion="0x00020000"

android:required="true" />

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name"> <activity

android:name="com.mercu.googlemapsv2.MainActivity"

android:label="@string/app_name" android:theme="@style/AppBaseTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

<!-- Goolge API Key --> <meta-data

android:name="com.google.android.maps.v2.API_KEY"

android:value="AIzaSyAnY8y6mdVyNCB2j8KMDFnhWZqXlPwHovQ" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application>

</manifest>

4.res/value/string.xml 2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
4.res/value/string.xml
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id

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

<string name="app_name">Google Maps V2</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string>

</resources>

Tips

Gunakan distanceTO untuk meghitung suatu jarak dari titk A ke titik B misalnya

double distance; Location locationA = new Location("point A"); locationA.setLatitude(latitude); locationA.setLongitude(longitude); Location locationB = new Location("point B");

locationB.setLatitude(randomLocation[0]);

locationB.setLongitude(randomLocation[1]);

distance = locationA.distanceTo(locationB);

//distance = locationA.distanceTo(locationB)/1000;

//in meters

//in km

DAFTAR PUSTAKA

1. http://developer.android.com/

2.Abdul Khaliq A.,”Meter Reading Online Application”

3.Arif Akbar Huda,24 jam pintar pemograman android

4. www.info.androidhive.googlemapsv2

2015 10 Mobile Programming Abdul Khaliq Arrachman,S.Kom,M.Kom. Pusat Bahan Ajar dan eLearning
2015 10
Mobile Programming
Abdul Khaliq Arrachman,S.Kom,M.Kom.
Pusat Bahan Ajar dan eLearning
http://www.mercubuana.ac.id