Android Open Source - Phonegap-3-Boilerplate Android Service






From Project

Back to project page Phonegap-3-Boilerplate.

License

The source code is released under:

GNU General Public License

If you think the Android project Phonegap-3-Boilerplate listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rjnpnigrhi.myapp;
// ww w.j  a v  a 2s  .co  m
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;

@SuppressLint("DefaultLocale")
public class AndroidService extends BackgroundService {

  private String configSetting = "";

  public static final String PREFS_NAME = "myappSharedPreferences";
  public static final String LOG_PROV = "PhoneGapLog";
  public static final String LOG_NAME = "Background Service";

  Handler handler = new Handler();

  @Override
  protected JSONObject doWork() {
    JSONObject result = new JSONObject();
    String msg = "Service executed.";
    runService();
    try {
      result.put("Message", msg);
    } catch (JSONException e) {
      Log.e(LOG_PROV, LOG_NAME + ": Error.");
      e.printStackTrace();
    }
    Log.d(LOG_PROV, LOG_NAME + ": " + msg);
    return result;
  }

  @Override
  protected JSONObject getConfig() {
    JSONObject result = new JSONObject();
    try {
      result.put("mConfig", this.configSetting);
    } catch (JSONException e) {
    }
    return result;
  }

  @Override
  protected void setConfig(JSONObject config) {
    try {
      if (config.has("mConfig")) {
        this.configSetting = config.getString("mConfig");
      }
    } catch (JSONException e) {
    }

  }

  @Override
  protected JSONObject initialiseLatestResult() {
    return null;
  }

  @Override
  protected void onTimerEnabled() {
  }

  @Override
  protected void onTimerDisabled() {
  }

  private void runService() {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    String serviceToastDuration = settings.getString("serviceToastDuration", "");
    if (!serviceToastDuration.equals("long")) {
      serviceToastDuration = "short";
    }
    showToast("Service executed with toast duration: " + serviceToastDuration, serviceToastDuration);
  }

  private void showToast(final String message, final String duration) {
    handler.post(new Runnable() {
      @Override
      public void run() {
        Toast toast;
        if(duration.equals("long")) {
          toast = Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG);
        } else {
          toast = Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT);
        }
        toast.show();
      }
    });
  }

}




Java Source Code List

com.rjnpnigrhi.myapp.AndroidService.java
com.rjnpnigrhi.myapp.StartActivity.java
org.apache.cordova.batterystatus.BatteryListener.java
org.apache.cordova.camera.CameraLauncher.java
org.apache.cordova.camera.ExifHelper.java
org.apache.cordova.camera.FileHelper.java
org.apache.cordova.contacts.ContactAccessorSdk5.java
org.apache.cordova.contacts.ContactAccessor.java
org.apache.cordova.contacts.ContactManager.java
org.apache.cordova.device.Device.java
org.apache.cordova.devicemotion.AccelListener.java
org.apache.cordova.deviceorientation.CompassListener.java
org.apache.cordova.dialogs.Notification.java
org.apache.cordova.file.DirectoryManager.java
org.apache.cordova.file.EncodingException.java
org.apache.cordova.file.FileExistsException.java
org.apache.cordova.file.FileHelper.java
org.apache.cordova.file.FileUtils.java
org.apache.cordova.file.InvalidModificationException.java
org.apache.cordova.file.NoModificationAllowedException.java
org.apache.cordova.file.TypeMismatchException.java
org.apache.cordova.filetransfer.FileProgressResult.java
org.apache.cordova.filetransfer.FileTransfer.java
org.apache.cordova.filetransfer.FileUploadResult.java
org.apache.cordova.geolocation.CordovaLocationListener.java
org.apache.cordova.geolocation.GPSListener.java
org.apache.cordova.geolocation.GeoBroker.java
org.apache.cordova.geolocation.NetworkListener.java
org.apache.cordova.globalization.GlobalizationError.java
org.apache.cordova.globalization.Globalization.java
org.apache.cordova.inappbrowser.InAppBrowser.java
org.apache.cordova.inappbrowser.InAppChromeClient.java
org.apache.cordova.media.AudioHandler.java
org.apache.cordova.media.AudioPlayer.java
org.apache.cordova.media.FileHelper.java
org.apache.cordova.mediacapture.Capture.java
org.apache.cordova.mediacapture.FileHelper.java
org.apache.cordova.networkinformation.NetworkManager.java
org.apache.cordova.plugins.AndroidPreferences.java
org.apache.cordova.plugins.Appstore.java
org.apache.cordova.plugins.HomeButton.java
org.apache.cordova.plugins.PackageVersion.java
org.apache.cordova.plugins.PreferredScreenSize.java
org.apache.cordova.plugins.Share.java
org.apache.cordova.plugins.Toasts.java
org.apache.cordova.splashscreen.SplashScreen.java
org.apache.cordova.vibration.Vibration.java