Save and retrieve date in android

//To obtain shared preferences
SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
//To read preferences
String dateTimeKey = "com.example.app.datetime";
// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime());
System.out.println("_____fecha de pref"+l);

//long to Date
Date datata = new Date(l);
System.out.println("Data convert of long to date"+datata);

// Date to Calendar
Calendar t1=Calendar.getInstance();
t1.setTime(datata);
System.out.println("convert date to calendar"+t1.getTime());

Calendar t2 = Calendar.getInstance();
System.out.println("second date"+t2.getTime());

//deduct date
int second = t2.get(Calendar.SECOND) - t1.get(Calendar.SECOND);
System.out.println("the seconds:"+second);

//To edit and save preferences
Date dato = new Date();
System.out.println("el dato:"+dato);
prefs.edit().putLong(dateTimeKey, dato.getTime()).commit();

Alert Dialog

Toast
To interact with the user from the application to show messages sometimes require or ask how you want to happen proceed flow depending on a particular response or choice. For this type have several tools which can display messages popup information or require action.

Some examples are, and you will be using each in the most convenient.

The first option is the Toast messages that are above the screen no matter what application is running, but they have no interaction button, rather serves to show rapid, short and not so important.

Toast toast = Toast.makeText(getApplicationContext(), "Example de Message for Android", Toast.LENGTH_SHORT);
toast.show();

Alert Dialog
The next option is to display a dialog box with the OK button, they are seen only if the Activity is active on the screen but ensures that the user reads it and to press a button to continue the flow.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Example de Message for Android")
.setTitle("Attention!!")
.setCancelable(false)
.setNeutralButton("Accept",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

One option that is widely used is to ask the user whether to continue with an activity or not, with the typical buttons yes or no and in some cases with a third option that is often omitted or out. Depending on the option chosen by the user performs different action.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to continue with the theme?")
.setTitle("Warning")
.setCancelable(false)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setPositiveButton("Continue",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
TransferirDinero();
}
});
AlertDialog alert = builder.create();
alert.show();

List
It could also be a case where we need to show more than one option, type multichoise and responses are not dry. This can display a list of options.

final CharSequence[] items = {"Android OS", "iOS", "Windows Phone", "Meego"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("your preferred OS?");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast toast = Toast.makeText(getApplicationContext(), "you choise the option: " + items[item] , Toast.LENGTH_SHORT);
toast.show();
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

This option can be added a radioButtons.

final CharSequence[] items = {"Android OS", "iOS", "Windows Phone", "Meego"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("your preferred OS?");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast toast = Toast.makeText(getApplicationContext(), "you choise the option:: " + items[item] , Toast.LENGTH_SHORT);
toast.show();
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

Service on android

Service on android

A Service is an application component representing either an application’s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding declaration in its package’s AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().

Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.

Service Lifecycle

There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed.

Example in GitHub

Intent initial concepts in android

definition

Intents are the way invoke the activities. Otherwise Intent is a class that lets you specify a run Activity

Two forms

There are two primary forms of intents you will use.

Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)), which provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application.
Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.

Intent Structure

A Intent can associate an action, a data and a category.

category — Gives additional information about the action to execute. For example, CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application, while CATEGORY_ALTERNATIVE means it should be included in a list of alternative actions the user can perform on a piece of data.

type — Specifies an explicit type (a MIME type) of the intent data. Normally the type is inferred from the data itself. By setting this attribute, you disable that evaluation and force an explicit type.

component — Specifies an explicit name of a component class to use for the intent. Normally this is determined by looking at the other information in the intent (the action, data/type, and categories) and matching that with a component that can handle it. If this attribute is set then none of the evaluation is performed, and this component is used exactly as is. By specifying this attribute, all of the other Intent attributes become optional.

extras — This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.