Creating a listener in an Android app

First we define the polling period.

private static long POLL_PERIOD = 20000; // period in ms.
privatestaticbooleancreated = false; // / safety check

Then, create the listener as a service. From there we call the service thread.

@Override
publicsynchronizedvoid onCreate() {
if (created) {
thrownew IllegalStateException(
"attempt to create listener twice!!");
}

created = true;
System.out.println("listener was created");
startServiceThread();
}

This thread will run the service. Has to be a thread in Android, can’t run on the main UI.

private void startServiceThread() {
new Thread(new Runnable() {
@Override
publicvoid run() {
runService();
}
}).start();
}

And here starts the endless loop of the listener. You let the thread sleep for your polling period, then you do your stuff after that call.

public void runService() {
System.out.println("listener running");
// the main loop.
while (true) {
try {
// first this, to ensure we always sleep in the loop.
Thread.sleep(POLL_PERIOD);
// do your stuff here
} catch (Throwable e) {
if (e instanceof UnknownHostException) {
System.out.println("check failed, no network");
} else {
e.printStackTrace();
}
}
}
}
Advertisements

A not-so-costly Google Location Listener implementation for Android apps

You need to, sometimes, get a very quick GPS position, even if not a very fine estimation, and then after that you can rely on a more relaxed location updates. This below is a way to speed up the initial requests and then later switched to “relaxed” mode.

Up top, you can have these variables.

private LocationClient locationClient = null;
private longinitialInterval = 2000; // 2 seconds
private long usualInterval = 10000; // 10 seconds
private int num Updates = 0;
LocationRequest locReq = new LocationRequest();

Then, on initial connection, set the request time to initialInterval (2 seconds).

@Override
public void onConnected(Bundle connectionHint){
System.out.println("Success connected to GooglePlayServices");
locReq.setInterval(initialInterval);
locationClient.requestLocationUpdates(locReq, this);
}

After a certain number of updates, change to usualInterval, which is 10 seconds (can be more).

@Override
public void onLocationChanged(Location location) {
LocationCache.getInstance().setPosition(getPos(location));
System.out.println("loc: " + location.getLatitude() 
+ "/" + location.getLongitude());

if (numUpdates<30){
locReq.setInterval(usualInterval);
locationClient.requestLocationUpdates(locReq, this);
numUpdates = numUpdates + 1;
}

}

Your own custom transitions within an Android app

Below is a function that takes you to another activity (namely, LocationsActivity). Instead of using the usual startActivity(), we call startActivity through this function:

public void locationsClick(View v) throws
JSONException, IOException {
Utils.forwardToActivity(this, new Intent
(this, LocationsActivity.class));
}

And in that function, we can override the transition, to our custom ones R.anim.move_in_right, R.anim.move_out_left.

/**
* go forward to given activity. Makes the new activity
* scroll in from * the right, hence the 'forward'.
**/

public static void forwardToActivity
(Activity currentActivity, Intent intent) {
currentActivity.startActivity(intent);
currentActivity.overridePendingTransition
(R.anim.move_in_right, R.anim.move_out_left);
}

Data Preparation #2

Edit: I just fixed the .java file by adding bw.flush() and bw.close() to the code. This fixed the remaining rows problem and now any data is almost ready to be used! Next: creating an M-script to prepare a ready matrix straight out of a .dat or .csv file!

———

I’ve managed to prepare an almost-ready matrix sample that can be tested in a neural network. It contained 4 columns for OHCL prices, one column for RSI, one column for MACD, two columns for SMA(10) and SMA(20), and finally the last column contains either 0 or 1, with 1 indicating that a moving average crossing happened in this bar.

There was 213 crossings in 3414 bars of hourly USDCAD (CAD = Canadian Dollars) prices, which originated from around 5000 unclean bars. That means that I will roughly need to use 20,000 bars to obtain a sample of around 1000 crossings and targets. 20,000 unclean hourly bars represent a little more than 2 years and three months. This is highly available.

What’s left to do is to add one more column where by next to every “1” in the latest column is the value of either the higest high (if this was an upcrossing) or lowest low (if this was a downcrossing).

The java application that cleans the data up and prepares it to be loaded into a fints is ready with only a minor problem, it does not load the last few rows correctly. I will look into it again tomorrow and try to get the matrix ready to be loaded.


import java.io.*;

class prep {

public static void main (String args[]){

String currentline;
int k=0;
int g=0;

try {

BufferedReader br = new BufferedReader(new FileReader(args[0]));
BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));

bw.write("DATE\tTIME\tVOLUME\tOPEN\tCLOSE\tLOW\tHIGH\n");

while ((currentline = br.readLine())!= null) {

if(currentline.contains("\t0\t") || currentline.contains("DATE"))

k++;

else {
bw.write(currentline);
bw.newLine();
}
g++;
}

catch (IOException e) {
System.err.println("Error: " + e);

}
}
}

Below is a screenshot of the fints data window with the USDCAD prices matrix loaded.

Screenshot-Financial Time Series Tool