8. @Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new
MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydne
y));
}
10. zoom
CameraPosition cameraPosition = new
CameraPosition.Builder()
.target(sydney)// Sets the center of the map to Mountain
View
.zoom(17)// Sets the zoom
.bearing(90)// Sets the orientation of the camera to east
.tilt(30)// Sets the tilt of the camera to 30 degrees
.build();// Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPos
ition(cameraPosition));
11. Posición actual
• Permisos necesarios
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms.location.sample.basiclocationsa
mple" >
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
12. Libreria para solicitar la ubicación a Google
dependencies {
compile 'com.google.android.gms:play-services-location:9.4.0'
}
13. Implementar listeners
public class MainActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
...
}
14.
15.
16. Conectar con Google Play Services
private GoogleApiClient mGoogleApiClient;//variable global
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
19. Al conectarse solicitamos la ubicacion
@Override public void onConnected(@Nullable Bundle
bundle) { mLastLocation =
LocationServices.FusedLocationApi.getLastLocation(mGoogl
eApiClient);
if (mLastLocation != null) {
double lati = mLastLocation.getLatitude();
double longi = mLastLocation.getLongitude();
}
else { Toast.makeText(this, "Ubicación no encontrada",
Toast.LENGTH_LONG).show();
}
}
20. Solicitud de permisos al usuario
• https://developers.google.com/android/guides/permissions
21. @Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Aquí muestras confirmación explicativa al usuario
// por si rechazó los permisos anteriormente
} else {
ActivityCompat.requestPermissions(
this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION);
}
} else {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
double lati = mLastLocation.getLatitude();
double longi = mLastLocation.getLongitude();
} else {
Toast.makeText(this, "Ubicación no encontrada", Toast.LENGTH_LONG).show();
}
}
}
22. @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) {
if (requestCode == REQUEST_LOCATION) {
if (grantResults.length == 1
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
mLastLocation =
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
double lati = mLastLocation.getLatitude();
double longi = mLastLocation.getLongitude();
} else {
Toast.makeText(this, "Ubicación no encontrada",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Permisos no otorgados", Toast.LENGTH_LONG).show();
}
23. Aprovechar los distintos intents
• Las aplicaciónes en Android pueden interactuar con otras
aplicaciones utilizando intents pre diseñados
• Aumentando la funcionalidad de las aplicaciones
24. Enviar un correo
public void composeEmail(String[] addresses, String subject, Uri
attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
25. Tomar una foto (1)
static final int REQUEST_IMAGE_CAPTURE = 1;
static final Uri mLocationForPhotos;
public void capturePhoto(String targetFilename) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.withAppendedPath(mLocationForPhotos, targetFilename));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
27. Hacer una llamada
• Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phoneNumber));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
28. Abrir una pagina web
public void openWebPage(String url) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
29. Crear una alarma
public void createAlarm(String message, int hour, int minutes) {
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
.putExtra(AlarmClock.EXTRA_MESSAGE, message)
.putExtra(AlarmClock.EXTRA_HOUR, hour)
.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
31. PHP & MVC
The model view controller pattern is the most used
pattern for today’s world web applications
It has been used for the first time in Smalltalk and
then adopted and popularized by Java
At present there are more than a dozen PHP web
frameworks based on MVC pattern
32. PHP & MVC
• The model is responsible to manage the
data
• The view (presentation) is responsible to
display the data provided by the model in
a specific format
• The controller handles the model and view
layers to work together
35. • En muchas ocaciones se necesita conectar nuestras aplicaciones con
servidores externos para acceder a bases de datos o para
interconectarlas con servicios externos.
• En esta sesión vamos a crear un servidor web php y conectar una
aplicación movil para acceder a una base de datos mysql alojada en
ese servidor
36. • Se debe instalar XAMPP de la siguiente dirección
• https://www.apachefriends.org/es/index.html
37. • E instalar Phpmyadmin para manejar facilmente la base de datos
38. • Al instalar XAMPP se crea un directorio llamado www
• Para probar que el servidor fue correctamente instalado se usa el
siguiente codigo en un archivo .php
• <?php
• echo "Welcome, I am connecting Android to PHP, MySQL";
• ?>
39. • Usando phpmyadmin se creara una nueva base de datos
• CREATE DATABASE unab;
40. • Y una tabla para almacenar productos
• CREATE TABLE products(
• pid int(11) primary key auto_increment,
• name varchar(100) not null,
• price decimal(10,2) not null,
• description text,
• created_at timestamp default now(),
• updated_at timestamp
• );
41. • Para realizar la conección con la base de datos se necesitan 2
archivos
• db_config.php
• db_connect.php
42. db_config.php
• <?php
•
• /*
• * All database connection variables
• */
•
• define('DB_USER', "root"); // db user
• define('DB_PASSWORD', ""); // db password (mention your db
password here)
• define('DB_DATABASE', "androidhive"); // database name
• define('DB_SERVER', "localhost"); // db server
• ?>
51. App Android
• Para interactuar con el servidor se usa la libreria OkHttp y para la
traduccíon de json a objetos se usa la librería Gson
52. En el gradle (Module: App)
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.google.code.gson:gson:2.6.1'
53. Creacion de POJOs
• Se utiliza la pagina http://www.jsonschema2pojo.org para generar
los objetos que usaremos en la aplicación facilmente a partir de las
respuestas en json que ya tenemos
54. • Los objetos generados los colocamos en la aplicación Android en
clases aparte
55. Request al api
• final Gson gson = new Gson();
//Creación del cliente que se conectara al API
OkHttpClient cliente = new OkHttpClient();
//Parametros que se enviarán en forma POST al servidor
RequestBody formBody = new FormEncodingBuilder()
.add("pid", "1")
.build();
Request request = new
Request.Builder().url("http://192.168.0.102/prueba1/get_product_
details.php").post(formBody).build();
Call call = cliente.newCall(request);
56. Manejo de la respuesta
• call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
if(response.isSuccessful()){
String rta = response.body().string();
Log.i("exito", "rta " + rta);
Body p1 = new Body();
p1= gson.fromJson(rta, Body.class);
Log.i("exito","object "+p1.getSuccess());
}
}
});