I have a problem, I need to make a method run every so often, the problem is that all my methods need an Activity to start and do not know how I need to give that activity. Try to create a Service and work with a timer but the problem radico that. I leave my methods for you to see.
public void push(Activity activity,String codigoEvento){
SessionManager manager = new SessionManager();
folioEvento = manager.getValue(activity,"folioEvento");
nombreCliente = manager.getValue(activity,"nombreCliente");
if(!(folioEvento.equals("") || nombreCliente.equals(""))){
ConnectionInternet cn = new ConnectionInternet();
if(cn.isNetworkAvailable(activity)){
String ids = "";
bdTickets = new TicketsBaseDatos(activity, "Eventrid", null, 1);
SQLiteDatabase db = bdTickets.getWritableDatabase();
Cursor c = db.rawQuery("SELECT inscripcion_id FROM Inscripcion WHERE validado=1 and sincronizado = 1 and codigo_usuario =SI-1",null);
if (c.moveToFirst()) {
do {
ids += c.getString(0)+",";
ContentValues valores = new ContentValues();
valores.put("sincronizado","1");
String[] args = new String[]{c.getString(0), codigoEvento};
db.update("Inscripcion", valores, "inscripcion_id=? AND codigo_evento=?", args);
} while(c.moveToNext());
}
db.close();
c.close();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(Config.URL_BASE+nombreCliente+"/"+Config.URL_PUSH+folioEvento+"/"+ids);
try {
String status = json.getString("status");
if(status.equals("1")){
System.out.println("BIEN");
}
else{
System.out.println("MAL");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
There are no lines in your code that require an Activity. There are many lines that require a Context. An Activity is a Context. A Service is a Context. If you change your Activity activity parameter to Context context, it would appear that your code should work using a Service instead of an Activity.
Related
I'm developing a recipe book and I'm implementing this method to insert my Recipe in the Database. In the for cycle I get the ingredient's name and quantity from multiples EditText, saving each of them in an Ingredient.class instance (newIngredient). Then I insert the instance into the DB and add it to an ArrayList. The followings "if conditions" are for the title, time and other Recipe's attributes. Finally, I also insert Recipe and Tag instances in the relatives DB's tables and I close DB.
public void saveRecipe() {
dbHelper = new DatabaseHelper(context);
// creating new recipe from user input
Ingredient newIngredient;
String title, childIngredient, instruction, tag;
int target, time, childQuantity, calories;
int countIngredients = parentIngredientLayout.getChildCount();
int countTags = chipGroup.getChildCount();
ArrayList<Ingredient> ingredients = null;
ArrayList<Tag> tags = null;
View childViewIng = null;
EditText childTextViewI = null;
EditText childTextViewQ = null;
// ingredients fields settings
for (int d=0; d<countIngredients; d++) {
childViewIng = parentIngredientLayout.getChildAt(d);
childTextViewI = childViewIng.findViewById(R.id.ingredientsField);
childTextViewQ = childViewIng.findViewById(R.id.quantityField);
childIngredient = childTextViewI.getText().toString();
childQuantity = Integer.parseInt(childTextViewQ.getText().toString());
newIngredient = new Ingredient(childIngredient, childQuantity);
dbHelper.insertIngredient(newIngredient);
ingredients.add(newIngredient);
}
//recipe fields settings
if (photoPath1 == null)
photoPath1 = "";
if (photoPath2 == null)
photoPath2 = "";
if (photoPath3 == null)
photoPath3 = "";
if (titleText.getText().toString().isEmpty()) {
title = "";
} else {
title = titleText.getText().toString();
}
if (targetNumber.getText().toString().isEmpty()) {
target = 0;
} else {
target = Integer.parseInt(targetNumber.getText().toString());
}
if (timeNumber.getText().toString().isEmpty()) {
time = 0;
} else {
time = Integer.parseInt(timeNumber.getText().toString());
}
if (instructionText.getText().toString().isEmpty()) {
instruction = "";
} else {
instruction = instructionText.getText().toString();
}
if (caloriesNumber.getText().toString().isEmpty()) {
calories = 0;
} else {
calories = Integer.parseInt(caloriesNumber.getText().toString());
}
if (tagName.getText().toString().isEmpty()) {
tag = "";
} else {
tag = tagName.getText().toString();
}
Recipe newRecipe = new Recipe(title, photoPath1, photoPath2, photoPath3, instruction, target, time, calories, ingredients);
Tag newTag = new Tag(tag);
dbHelper.insertRecipe(newRecipe);
dbHelper.insertTag(newTag);
dbHelper.close(); }
I found out by debugging that in this case is inserted only the first ingredient. I tried to move the FOR until the end of code, but in that case, are inserted both recipe and tag and always only the first ingredient. I think the problem is relative to the opening/closing of the DB. Can somebody help me?
Ingredient constructor:
public Ingredient(String ingredient_name, int quantity) {
this.ingredient_name = ingredient_name;
this.quantity = quantity;
}
dbHelper.insertIngredient(newIngredient) method:
public boolean insertIngredient(Ingredient ingredient) {
db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(INGREDIENT_NAME, ingredient.getIngredient_name());
contentValues.put(QUANTITY, ingredient.getQuantity());
contentValues.put(KEY_CREATED_AT, time.getTime().toString());
long result = db.insert(TBL_INGREDIENTS, null, contentValues);
//db.close();
Log.e(TAG, "Ingredient inserted!");
if (result == -1) {
return false;
} else {
return true;
}
}
Ok, thanks to your comment we got the problem :)
You are calling .add(newIngredient) on a list that you initialized with ArrayList<Ingredient> ingredients = null;
Change it to
ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();
and it will work :)
Good luck!
i have a quite specific problem. I have realized a Web App on an Android tablet, which will be used on an exhibition (Outform iDisplay). For this reason, the Web App has to start directly after boot. The after-boot thing is no problem (Broadcast with "android.permission.RECEIVE_BOOT_COMPLETED"), but i have a problem to start Chrome as Web-App. For getting the Intent, i have read the Icons in the launcher favorites with this snippet:
//Kitkat, therefore launcher3
url = "content://com.android.launcher3.settings/favorites?Notify=true";
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), null, null, null, null);
if (cursor != null && cursor.moveToFirst())
{
do
{
String ent1 = cursor.getString(0);
String ent2 = cursor.getString(1);
String ent3 = cursor.getString(2); //there is the Intent string
String ent4 = cursor.getString(3);
System.out.println("Test");
String ent5 = cursor.getString(4);
String ent6 = cursor.getString(5);
String ent7 = cursor.getString(6);
String ent8 = cursor.getString(7);
String ent9 = cursor.getString(8);
String ent10 = cursor.getString(9);
String ent11 = cursor.getString(10);
String ent12 = cursor.getString(11);
String ent14 = cursor.getString(13);
String ent15 = cursor.getString(14);
String ent17 = cursor.getString(16);
String ent18 = cursor.getString(17);
String ent19 = cursor.getString(18);
String ent20 = cursor.getString(19);
if(ent2.equals("History Book")) //Get the right intent
{
runAction = ent3;
}
System.out.println(ent3);
} while (cursor.moveToNext());
}
The Intent string contains something like this:
#Intent;action=com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP;package=com.android.chrome;S.org.chromium.chrome.browser.webapp_title=History%20Book;S.org.chromium.chrome.browser.webapp_id=86e362e4-a25d-4142-8a32-c02ffcb176a9;i.org.chromium.content_public.common.orientation=6;S.org.chromium.chrome.browser.webapp_icon=;S.org.chromium.chrome.browser.webapp_mac=3ZaXFbyWnJQaqFFOuUj3OssNz7DrBaaiWfzO2Dd7VIU%3D%0A;S.org.chromium.chrome.browser.webapp_url=http%3A%2F%2F192.168.5.148%2Fstyria%2Fhistorybook%2Findex.html;end
This looks quite good, but how can i start an Intent like this in a small app, which just has the single purpose to start this intent?
Just a small note at the end: I have tried to pack this thing into a webview, but the webview died constantly because of an libc error, so this is no option for me.
Finally i got this thing working. I was on the right way, but some Chrome.apk reverse engineering helped me for the last mile.
I have created a dummy activity with the following code in onCreate:
Search for the right entry on the homescreen, in my case for the AOSP launcher 3:
//Search for the History Book Shortcut on the Homescreen
String url = "";
String runAction="";
final String AUTHORITY = "com.android.launcher3.settings";
final Uri CONTENT_URI = Uri.parse("content://" +
AUTHORITY + "/favorites?notify=true");
final ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(CONTENT_URI,null,null,null,null);
cursor.moveToFirst();
do {
String id = cursor.getString(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String intent = cursor.getString(cursor.getColumnIndex("intent"));
if(title.equals(getResources().getString(R.string.homescreen_link)))
{
runAction = intent;
}
} while (cursor.moveToNext());
At this point, i have hopefully the intent as string. So, parse the string and create a new intent:
Intent intent = new Intent();
intent.setAction("com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP");
intent.setPackage("com.android.chrome");
intent.setClassName("com.android.chrome", "com.google.android.apps.chrome.webapps.WebappManager");
HashMap<String, String> intentVals = getIntentParams(runAction);
intent.putExtra("org.chromium.chrome.browser.webapp_title",intentVals.get("S.org.chromium.chrome.browser.webapp_title"));
intent.putExtra("org.chromium.chrome.browser.webapp_icon",intentVals.get("S.org.chromium.chrome.browser.webapp_icon"));
intent.putExtra("org.chromium.chrome.browser.webapp_id",intentVals.get("S.org.chromium.chrome.browser.webapp_id"));
intent.putExtra("org.chromium.chrome.browser.webapp_url",intentVals.get("S.org.chromium.chrome.browser.webapp_url"));
intent.putExtra("org.chromium.chrome.browser.webapp_mac",intentVals.get("S.org.chromium.chrome.browser.webapp_mac"));
int orientation = 6;
try
{
orientation = Integer.parseInt(intentVals.get("i.org.chromium.content_public.common.orientation"));
}
catch(NumberFormatException _nex)
{
Log.e(TAG, "Wrong format, using default (6)");
}
intent.putExtra("org.chromium.content_public.common.orientation", orientation);
try
{
byte[] abyte0 = Base64.decode(
intentVals.get("S.org.chromium.chrome.browser.webapp_mac"),
0);
System.out.println(new String(abyte0));
}
catch (IllegalArgumentException _iae)
{
Log.e(TAG,
"Wrong webapp_mac: "
+ intentVals
.get("S.org.chromium.chrome.browser.webapp_mac"));
}
startActivity(intent);
finish();
And this function parses the intent parameters out of the intent string:
private HashMap<String, String> getIntentParams(String _runAction)
{
HashMap<String, String> retMap = new HashMap<String, String>();
String[] pairs = _runAction.split(";");
for (int i = 0; i < pairs.length; i++)
{
String[] keyval = pairs[i].split("=");
if(keyval.length==2)
{
String key = keyval[0];
String value = "";
try
{
value = java.net.URLDecoder.decode(keyval[1], "UTF-8");
}
catch (UnsupportedEncodingException _uee)
{
Log.e(TAG, "Unsupported Encoding: " + _uee.getMessage());
}
retMap.put(key, value);
}
}
return retMap;
}
And the strings.xml in res/values:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WebAppStarter</string>
<string name="homescreen_link">History Book</string>
</resources>
That's it. You can configure the Homescreen link name to search for in strings.xml. When the app finds the string, it parses the intent string and creates a new intent to start Chrome as a Full Screen Activity Web App.
I downloaded a class from Catch The Cows, it is akin to a Google Map object or at least that is what I am using it for.
It parses an XML file which lists the areas of the screen that should be touchable, and then creates them with this method.
This is here for context, I have commented out some parts of code, and added my own to try and resolve my issue
private Area addShape( String shape, String name, String coords, String id) {
Log.v("IDS:", "id was "+id);
Area a = null;
String rid = id.replace("#+id/", "");
Log.v("IDS:", "rid was "+rid);
// Generate a new ID for the area.
int _id = 1;
View vi = findViewById(_id);
while (vi!=null) {
_id++;
vi = findViewById(_id);
}
//View.generateViewId(); //=0;
Log.v("IDS:", "After conversion final time "+_id);
/*
try {
Class<R.id> res = R.id.class;
Field field = res.getField(rid); // eg. rid = area10
_id = field.getInt(null);
Log.v("IDS:", "After conversion "+_id);
}
catch (Exception e) {
_id = 0;
Log.e("Exception ",e.getMessage());
} finally {
Log.v("IDS:", "After conversion final time "+_id);
}
*/
if (_id != 0) {
if (shape.equalsIgnoreCase("rect")) {
String[] v = coords.split(",");
if (v.length == 4) {
a = new RectArea(_id, name, Float.parseFloat(v[0]),
Float.parseFloat(v[1]),
Float.parseFloat(v[2]),
Float.parseFloat(v[3]));
}
}
if (shape.equalsIgnoreCase("circle")) {
String[] v = coords.split(",");
if (v.length == 3) {
a = new CircleArea(_id,name, Float.parseFloat(v[0]),
Float.parseFloat(v[1]),
Float.parseFloat(v[2])
);
}
}
if (shape.equalsIgnoreCase("poly")) {
a = new PolyArea(_id,name, coords);
}
if (a != null) {
addArea(a);
}
} else {
Log.v("Loading ID: ","_id was 0");
}
return a;
}
Unfortunately nothing was rendering on the screen, and this was because _id = 0. This should be changed with this bit of code:
try {
Class<R.id> res = R.id.class;
Field field = res.getField(rid); // eg. rid = area10
_id = field.getInt(null);
}
How ever I am not sure what it does to try and debug it, can anyone explain what this snippet is doing?
R is a Read-Only class. It is generate at compile time and You should not use reflection to modify its field. Also you should avoid reflection to access the fields values. You should use the official API.
The comment at the first row of the class is
/* AUTO-GENERATED FILE. DO NOT MODIFY. */
I'll tell you the aim of this part of my app. I've got a SQLite dB which has 3 columns. First is "Quantity", Second is "Product" and third is "Price". Well, what i want to do is to get the whole dB and send it by email.
This is what i have right now:
public class Visual extends Activity {
TextView tv;
Button sqlGetInfo;
long l ;
long b=1;
int c,i;
String returnedQuantity ,returnedProduct ,returnedPrice;
String[] filas;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.visual);
tv = (TextView) findViewById(R.id.tvSQLinfo);
sqlGetInfo = (Button) findViewById(R.id.EnviarPedido);
SQLManager info = new SQLManager(this);
info.open();
String data= info.getData();
info.close();
tv.setText(data);
Up to here, my code works fine, it displays the data in the textView. Here is the problem. My dB has a maximum of 15 rows. What i want to do is to store each row in a position of a string array (filas). First row = filas(0), second row = filas(1)...in order to be able to pass this array to another activity. If the array has less than 15 rows i think it would give an exception. So it's the time to open the other activity.
final SQLManager hon = new SQLManager(this);
sqlGetInfo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for (i = 1; i < 15; i++) {
try{
l = (long) i;
hon.open();
returnedQuantity = hon.getQuantity(l);
returnedProduct = hon.getProduct(l);
returnedPrice = hon.getPrice(l);
hon.close();
c=(int)(l-b);
filas[c]="" + returnedQuantity+" "+ returnedProduct+" "+ returnedPrice + "\n";
}catch (Exception e){
i = 16;
l = (long) i;
Intent abrePedidos = new Intent(Visual.this, Pedidos.class);
abrePedidos.putExtra("pedidoCompleto", filas);
startActivity(abrePedidos);
}
}
}
});
}
}
The other activity is this:
public class Pedidos extends Activity {
String[] filas;
long numProd;
boolean end;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
filas=getIntent().getStringArrayExtra("pedidoCompleto");
String subject = "Pedido a Domicilio";
String cabecera = "Unid. Producto Precio\n\n";
String[] emails = {"ulrickpspgo#gmail.com"};
String message = cabecera + filas;
Intent emailIntent = new Intent (android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emails);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
emailIntent.setType("plain/text");
startActivity(emailIntent);
}
}
What I get as the message of my email is "null".
I think you problem is the following: you never initialize String[] filas;. This means that it remains null all the time. Afterwards you go to your code:
try {
l = (long) i;
hon.open();
returnedQuantity = hon.getQuantity(l);
returnedProduct = hon.getProduct(l);
returnedPrice = hon.getPrice(l);
hon.close();
c=(int)(l-b);
// The next line throws null pointer exception
filas[c]="" + returnedQuantity+" "+ returnedProduct+" "+ returnedPrice + "\n";
} catch (Exception e) { // here you catch the null pointer exception
i = 16;
l = (long) i;
Intent abrePedidos = new Intent(Visual.this, Pedidos.class);
abrePedidos.putExtra("pedidoCompleto", filas); //filas is still null
startActivity(abrePedidos);
}
I have added comments for some of your lines. Why do you call the next activity only in the catch clause? Having so much code in the catch clause is very bad practise it is called expection handling. don't do it. Otherwise if you initialize your array (which I am not sure you have not already done, but this is my guess what the exception you hide is) you should be good to go. Do not forget to place the code outside the catch block, though, because I do not expect any exceptions after the modification.
EDIT I do not like the way you iterate over the elements in the database. I am not familiar with the SQLManager class you use. However the standard Android way is very similar to the JDBC model. You do a query and it returns a cursor over the results. See example of using cursors and SQLitehleprs over here: http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/2/
Actually I have written a method for updatiing server database using webservice from my application installed in the device using two IP Address.If one IP failed then it usess the second IP for upadting the data at server.
If Both the IP address failed i am saving the data to one of my sqllite database table tblTransaction.The code for that is given below.
private void Delay15Minute() throws IOException {
String server1IPAddress = "";
String server2IPAddress = "";
String deviceId = "";
Cursor cursorAdmin;
Cursor cursorTransaction;
adminhelper = new admin_helper(this);
cursorAdmin = adminhelper.GetAdminDetails();
if (cursorAdmin.moveToFirst())
server1IPAddress = cursorAdmin.getString(cursorAdmin
.getColumnIndex("RemoteServer1IPAddress"));
server2IPAddress = cursorAdmin.getString(cursorAdmin
.getColumnIndex("RemoteServer2IPAddress"));
deviceId = cursorAdmin.getString(cursorAdmin
.getColumnIndex("DeviceID"));
cursorAdmin.close();
ContentValues initialDelay15 = new ContentValues();
ContentValues initialTransaction = new ContentValues();
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
String RevisedEstimatedDate = sdf.format(date);
manifest_helper = new manifest_helper(this);
cursor = manifest_helper.GetDeliveries(pkManifest);
cursor.moveToFirst();
dbAdapter = new DatabaseAdapter(this);
dbAdapter.open();
for (int i = 0; i < cursor.getCount(); i++) {
cursor.getString(cursor.getColumnIndex("PKDelivery"));
String RevisedTime=cursor.getString(cursor.getColumnIndex("RevisedEstimatedDeliveryTime"));
// get hour and minute from time string
StringTokenizer st1 = new StringTokenizer(RevisedTime, ":");
int j = 0;
int[] val = new int[st1.countTokens()];
// iterate through tokens
while (st1.hasMoreTokens()) {
val[j] = Integer.parseInt(st1.nextToken());
j++;
}
// call time add method with current hour, minute and minutesToAdd,
// return added time as a string
String dateRevisedEstimatedDeliveryTime = addTime(val[0], val[1], 15);
initialDelay15.put("RevisedEstimatedDeliveryTime",
dateRevisedEstimatedDeliveryTime);
dbAdapter.UpdateRecord("tblDelivery", initialDelay15, "PKDelivery"
+ "=" + cursor.getString(cursor.getColumnIndex("PKDelivery")), null);
}
dbAdapter.close();
dataXmlExporter=new DataXmlExporter(this);
dataXmlExporter.StartDataSet();
cursor = manifest_helper.GetDeliveries(pkManifest);
dataXmlExporter.AddRowandColumns(cursor,"tblDelivery");
String sqlTransaction = "Select 6 as TransactionType,'Update Revised Estimated Delivery Time' as Description,"
+ " deviceId as DeviceID ,date() as TransactionUploadDate,time() as TransactionUploadTime from tblAdmin where PKAdmin > ?";
dbAdapter = new DatabaseAdapter(this);
dbAdapter.open();
cursorTransaction = dbAdapter.ExecuteRawQuery(sqlTransaction, "-1");
dataXmlExporter.AddRowandColumns(cursorTransaction, "Transaction");
String XMLTransactionData=dataXmlExporter.EndDataSet();
try {
if ((server1IPAddress != "") && (server2IPAddress != "")) {
try {
if (server1IPAddress != "") {
InsertUploadedTrancasctionDetails(server1IPAddress,
deviceId, XMLTransactionData);
}
} catch (Exception exception) {
if ((server1IPAddress != server2IPAddress)
&& (server2IPAddress != "")) {
InsertUploadedTrancasctionDetails(server2IPAddress,
deviceId, XMLTransactionData);
}
}
}
} catch (Exception exception) {
initialTransaction.put("ReceivedDate",
RevisedEstimatedDate);
initialTransaction.put("TransactionData",
XMLTransactionData);
dbAdapter.InsertRecord("tblTransaction", "",
initialTransaction);
}
dbAdapter.close();
LoadDeliveries(pkManifest);
}
The Problem is that i need to update the data to the server that stored in the tbltransaction automatically when we get connection to the serverIP along with my running application.I think it is possible by means of establishing backround running process along with my application that will check whethere data is there in the tbltransaction and connection with server is there.
So will any one have an idea for this ...if so please help meee...........
Here are a couple of methods that together will do just this. This is all within a Service, you could use a Handler instead and do this within an activity, but a Service is a wiser choice.
First, we need to be able to tell if we're online, this requires Network State and Wifi State Permissions:
public boolean isOnline() {
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
} catch (Exception e) {
return false;
}
}
Next we need to be able to set an alarm to retry. Add these variables:
private static AlarmManager am;
private static PendingIntent pIntent;
public static final int MSG_UPDATE = 0;
public static final String PENDING_REQ_KEY = "request";
And a set alarm method:
private synchronized void setAlarm() {
if (am == null) am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
if (Constants.LOG_DEBUG) Log.d(TAG,"Setting Alarm for 5 mins");
long delay = 300000L; //5 Mins
Intent i = new Intent(this,Service.class); //Reference the Service this method is in
i.putExtra(PENDING_REQ_KEY, MSG_UPDATE);
pIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC,System.currentTimeMillis()+delay,pIntent);
}
Next, Override the onStartCommand method, to capture the update request:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getExtras() != null && intent.getExtras().containsKey(PENDING_REQ_KEY)) {
int request = intent.getExtras().getInt(PENDING_REQ_KEY);
if (request == MSG_UPDATE) update();
}
return START_STICKY;
}
finally, your update method:
public void update() {
if (isOnline()) {
/** Push data to server here */
}
else {
setAlarm();
}
}