SCRIPT PROJECT PEMROGRAMAN
JAVA MOBILE
DataUAS145610018
DisusunOleh:
Nama :Hamdani
Nim : 145610018
SEKOLAH TINGGI
MANAJEMEN INFORMATIKA DAN KOMPUTER AKAKOM
YOGYAKARTA
2017
GPSTracker.java
package uas.satu.a145610018.si.hamdani.datauas145610018;
import android.Manifest;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
booleanisGPSEnabled = false;
// flag for network status
booleanisNetworkEnabled = false;
// flag for GPS status
booleancanGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10
meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1
minute
// Declaring a Location Manager
protected LocationManagerlocationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled&& !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions,
and then overriding
// public void
onRequestPermissionsResult(intrequestCode, String[] permissions,
//
int[] grantResults)
// to handle the case where the user grants
the permission. See the documentation
// for ActivityCompat#requestPermissions
for more details.
return null;
}
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS() {
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then
overriding
// public void onRequestPermissionsResult(intrequestCode,
String[] permissions,
// int[]
grantResults)
// to handle the case where the user grants the permission.
See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
public booleancanGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.BuilderalertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings
menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
@Override
public IBinderonBind(Intent arg0) {
return null;
}
MainActivity.java
package uas.satu.a145610018.si.hamdani.datauas145610018;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button plh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] option = {"Tambah Data", "Tampil Data", "Keluar"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item, option);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pilihan");
AlertDialog.Builder builder1 = builder.setAdapter(adapter, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
if (i == 0) {
Intent oke = new Intent(MainActivity.this,
TambahData.class);
startActivity(oke);
} else if (i == 1) {
Intent oke = new Intent(MainActivity.this,
TampilData.class);
startActivity(oke);
} else {
finish();
moveTaskToBack(true);
}
}
});
final AlertDialog a = builder.create();
plh = (Button) findViewById(R.id.btnMenu);
plh.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
a.show();
}
});
}
}
MySQLHelper.java
package uas.satu.a145610018.si.hamdani.datauas145610018;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "sidiq.db";
private static final int DATABASE_VERSION = 1;
// Table name
public static final String TABLE = "datauas";
// Columns
public static final String nim = "nim";
public static final String nama = "nama";
public static final String alamat = "alamat";
public static final String lat = "lat";
public static final String lng = "lng";
public MySQLHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabasedb) {
String sql = "create table " + TABLE + "( _id"
+ " integer primary key autoincrement, " + nim + " text not
null, "
+ nama + " text not null, " + alamat + " text not null, " +
lat + " text not null, "+ lng + " text not null);";
Log.d("Data", "onCreate: " + sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabasedb, intoldVersion, intnewVersion) {
// TODO Auto-generated method stub
}
}
TambahData.java
package uas.satu.a145610018.si.hamdani.datauas145610018;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class TambahData extends AppCompatActivity {
EditTextnim, nama, alamat;
TextViewlat, lng;
Button simpan, kembali;
ImageButtonlok;
GPSTrackergps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tambah_data);
nim = (EditText) findViewById(R.id.editNIM);
nama = (EditText) findViewById(R.id.editNama);
alamat = (EditText) findViewById(R.id.editAlamat);
lat = (TextView) findViewById(R.id.txtLat);
lng = (TextView) findViewById(R.id.txtLng);
simpan = (Button) findViewById(R.id.btnSimpan);
kembali = (Button) findViewById(R.id.btnKembali);
lok = (ImageButton) findViewById(R.id.btnLokasi);
}
public void onKlikLok(View v){
gps = new GPSTracker(TambahData.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
lat.setText(String.valueOf(latitude));
lng.setText(String.valueOf(longitude));
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
public void onKlikSim(View v) {
MySQLHelperdbhelper;
dbhelper = new MySQLHelper(this);
SQLiteDatabasedb = dbhelper.getWritableDatabase();
try {
db.execSQL("insert into " + MySQLHelper.TABLE +
" values(null, '" + nim.getText().toString() + "', '" +
nama.getText().toString() +
"', '" + alamat.getText().toString() + "', '" +
lat.getText().toString() + "', '" +
lng.getText().toString() + "')");
Toast.makeText(getBaseContext(), "Data telahdisimpan",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Kesalahan: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
public void onKlikKem(View v){
startActivity(new Intent(TambahData.this, MainActivity.class));
finish();
}
}
TampilData.java
package uas.satu.a145610018.si.hamdani.datauas145610018;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class TampilData extends AppCompatActivity {
MySQLHelperdbhelper;
protected Cursor cursor;
ListViewdaftar;
ArrayAdapter<String> adapter;
SQLiteDatabasedb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tampil_data);
try {
dbhelper=new MySQLHelper(this);
db=dbhelper.getReadableDatabase();
cursor=db.rawQuery("SELECT * FROM "+MySQLHelper.TABLE, null);
daftar=(ListView)findViewById(R.id.listMan);
List<String>infoData=new ArrayList<>();
String nim, nama, alamat, lat, lng;
intjum=cursor.getCount();
cursor.moveToFirst();
int i=0;
while (i<jum){
nim=cursor.getString(1);
nama=cursor.getString(2);
alamat=cursor.getString(3);
lat=cursor.getString(4);
lng=cursor.getString(5);
infoData.add(nim+" \n" +nama+"\n"
+alamat+"\n Lat: "+lat+" \n Lng: "+lng);
cursor.moveToNext();
i++;
}
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, infoData);
daftar.setAdapter(adapter);
}
catch (NullPointerException e){
Toast.makeText(getBaseContext(), "Kesalahan "+e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="HAMDANI"
android:id="@+id/nama"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:textColor="@android:color/background_dark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="145610018"
android:id="@+id/nim"
android:layout_below="@+id/nama"
android:layout_centerHorizontal="true"
android:textColor="@android:color/background_dark"
android:textSize="16sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pilihan"
android:id="@+id/btnMenu"
android:layout_below="@+id/foto"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp" />
<ImageView
android:layout_width="165dp"
android:layout_height="165dp"
android:id="@+id/foto"
android:src="@drawable/dani"
android:layout_marginTop="44dp"
android:layout_below="@+id/nim"
android:layout_centerHorizontal="true" />
</RelativeLayout>
activity_tambah_data.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="NIM"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editNIM"
android:layout_below="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_alignParentEnd="true"
android:layout_alignLeft="@+id/textView"
android:layout_alignParentRight="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Nama"
android:id="@+id/textView2"
android:layout_below="@+id/editNIM"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editNama"
android:layout_below="@+id/textView2"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Alamat"
android:id="@+id/textView3"
android:layout_below="@+id/editNama"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editAlamat"
android:layout_below="@+id/textView3"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:lines="2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnLokasi"
android:src="@android:drawable/ic_menu_mylocation"
android:layout_below="@+id/editAlamat"
android:layout_alignParentStart="true"
android:onClick="onKlikLok"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude"
android:id="@+id/textView4"
android:layout_below="@+id/btnLokasi"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtLat"
android:layout_below="@+id/textView4"
android:layout_alignStart="@+id/textView4"
android:layout_alignLeft="@+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude"
android:id="@+id/textView5"
android:layout_below="@+id/txtLat"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtLng"
android:layout_below="@+id/textView5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simpan"
android:id="@+id/btnSimpan"
android:layout_alignBottom="@+id/btnLokasi"
android:layout_alignParentEnd="true"
android:onClick="onKlikSim"
android:layout_alignParentRight="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kembali"
android:id="@+id/btnKembali"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:onClick="onKlikKem"
android:layout_alignParentLeft="true" />
</RelativeLayout>
activity_tampil_data.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="NIM"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editNIM"
android:layout_below="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_alignParentEnd="true"
android:layout_alignLeft="@+id/textView"
android:layout_alignParentRight="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Nama"
android:id="@+id/textView2"
android:layout_below="@+id/editNIM"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editNama"
android:layout_below="@+id/textView2"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Alamat"
android:id="@+id/textView3"
android:layout_below="@+id/editNama"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editAlamat"
android:layout_below="@+id/textView3"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:lines="2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnLokasi"
android:src="@android:drawable/ic_menu_mylocation"
android:layout_below="@+id/editAlamat"
android:layout_alignParentStart="true"
android:onClick="onKlikLok"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude"
android:id="@+id/textView4"
android:layout_below="@+id/btnLokasi"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtLat"
android:layout_below="@+id/textView4"
android:layout_alignStart="@+id/textView4"
android:layout_alignLeft="@+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude"
android:id="@+id/textView5"
android:layout_below="@+id/txtLat"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtLng"
android:layout_below="@+id/textView5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simpan"
android:id="@+id/btnSimpan"
android:layout_alignBottom="@+id/btnLokasi"
android:layout_alignParentEnd="true"
android:onClick="onKlikSim"
android:layout_alignParentRight="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kembali"
android:id="@+id/btnKembali"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:onClick="onKlikKem"
android:layout_alignParentLeft="true" />
</RelativeLayout>