1

I am new to android studio and successfully built a keyboard following a tutorial. I am now trying to capture a timestamped event. So when a button is pressed I get the timestamp. I tried using the SQLite database but was having issues with it. I am now trying to create a text file and save the data to a file. I was wondering if someone would be able to help me with this.

I did some research on SO and only found documentation for saving data to an external sdcard and when I tried to modify the code it did not work.

import android.content.Context;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.media.AudioManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputConnection;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;

public class SimpleIME extends InputMethodService
        implements KeyboardView.OnKeyboardActionListener {

    private KeyboardView kv;
    private Keyboard keyboard;

    private boolean caps = false;

    @Override
    public View onCreateInputView() {

        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.qwerty);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this); 
        return kv;
    }

    private void playClick(int keyCode){
        AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
        switch(keyCode){
            case 32:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
                break;
            case Keyboard.KEYCODE_DONE:
            case 10:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
                break;
            case Keyboard.KEYCODE_DELETE:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
                break;
            default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
        }
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();
        playClick(primaryCode);
        switch(primaryCode){
            case Keyboard.KEYCODE_DELETE :
                ic.deleteSurroundingText(1, 0);
                break;
            case Keyboard.KEYCODE_SHIFT:
                caps = !caps;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                break;
            case Keyboard.KEYCODE_DONE:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                break;
            default:
                char code = (char)primaryCode;
                if(Character.isLetter(code) && caps){
                    code = Character.toUpperCase(code);
                }
                ic.commitText(String.valueOf(code),1);
        }
    }
csciBeginner
  • 219
  • 1
  • 4
  • 15
  • Tell us what exactly didn't work, which documentation you used etc. It is a broad question and I wouldn't be surprised if anyone answered it without you providing more information. Another thing to note is that it is usually better to use some database model rather than save data explicitly in files. If you don't want to get right into "big" databases you could try SugarORM. It's an easy ORM for SQLite. – sp0rk Jul 21 '16 at 15:56
  • @spork basically I just want to know how to create a text file and insert a time stamp when a button is pressed. How can I do this using SQLite? – csciBeginner Jul 21 '16 at 15:58

2 Answers2

1

To simply write some text to a file you can use Java's inbuilt FileWriter or FileOutputStream.

File root = new File(DIRECTORY_PATH);
File mFile = new File(root, "samples.txt");
FileWriter writer = new FileWriter(gpxfile);
writer.append("String to write.");
writer.flush();
writer.close();

It has already been answered here before: How do I write to a .txt file in Android?

To get a timestamp, I would suggest using a Calendar and SimpleDateFormat as such.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
sdf.format(Calendar.getInstance()); //returns string
Community
  • 1
  • 1
sp0rk
  • 483
  • 4
  • 14
  • would I put this function in my on create view? public View onCreateInputView() { kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null); keyboard = new Keyboard(this, R.xml.qwerty); kv.setKeyboard(keyboard); kv.setOnKeyboardActionListener(this); return kv; } – csciBeginner Jul 21 '16 at 16:04
  • You want to save this timestamp on the press of a button, so the logic given above should be implemented in the onClick method of the button. Are you familiar with OnClickListener ? Also, checkout the edit – sp0rk Jul 21 '16 at 16:06
  • I added my .java file above. this is what I have and It works. – csciBeginner Jul 21 '16 at 16:11
  • I don't know if you still have any questions. I understood that you have a Button object in layout that you want to use to save it on the phone, is that right? If so, set a new OnClickListener on this button and create a method with aforementioned logic. Also, remember to upvote any useful information you get from other users and accept the best solution. – sp0rk Jul 21 '16 at 16:13
  • I do not have a button in the layout because there is no main activity this a keyboard and you can use instead of your android built-in one. I am just confused where I would add textfile – csciBeginner Jul 21 '16 at 16:19
0

About writing to the internal storage, check out the official docs about writing files to the internal storage. Basically you're writing a file in your app private folder, which only your app has access (it's usually in the "/data/data/" directory). As the docs said you'll use the "getFilesDir()" in order to get your private directory path, the you write your file using a FileOutputStream and it's up to you the format you want to write your file.

josemgu91
  • 719
  • 4
  • 8
  • my app does not have a main activity does that matter? – csciBeginner Jul 21 '16 at 16:08
  • @csciBeginner You should only need a Context object to get the file directory – OneCricketeer Jul 21 '16 at 16:11
  • Yes, the "getFilesDir()" method comes from the Context class, which your Activity or Service extends. – josemgu91 Jul 21 '16 at 16:13
  • I posted the code above. Where would I add the code? – csciBeginner Jul 21 '16 at 16:19
  • I recommend you to do a method to write the data, something like this "writeKeyboardEvent(int keyboardEvent, long timestamp)", for the timestamp it can be the "System.currentTimeMillis()" method (which gives you the current system time in unix epoch format in milliseconds). This method can be called from anywhere (like your switch keyboard code case), I recommend you to write to the file in another thread (because you'll lock the UI thread). – josemgu91 Jul 21 '16 at 16:36