середа, 12 грудня 2012 р.

Android. Problem in database access from service

Few months ago I worked on my first android application. I use AlarmManager for work in background and call from it my intents receiver. Here the code:

public static final int FIRST_RUN = 5000; // 5 seconds
public static final int INTERVAL = 30000; // 30 sec
private AlarmManager alarmManager = null;
....
Intent intent = new Intent(this, FingerNotesServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
this.alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
this.alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + FIRST_RUN, INTERVAL,
pendingIntent);
FingerNotesServiceReceiver - this is my receiver class. It extends BroadcastReceiver class. Everything looks fine, but there are one problem. If you try to open connection to sqlite database file with write permissions from your receiver you will catch the error described below:

android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here


Sqlite don't allow you to create more than one write connections to db file. To prevent this in main process of application you can use static instance of SQLiteOpenHelper. But receiver still throws this error. This happens, because onReceive functions calls on separate process. When you try to gain access to database from receiver - application creates new static SQLiteOpenHelper object. Because existing object is staying in main process. To solve this I was add ContentProvider into application with needed functionality. Receiver interact with database through provider and code looks like I send queries to sqlite directly. As a result I got this scheme for application modules:




You can check my app on google play: FingerNotes

пʼятниця, 7 вересня 2012 р.

Git pocket tutorial

My first post about git repository.

At the beginning we must initialize out repo. Let's say it have tempo name. For now we guess that repo was created on git server and we only must create a local copy on our development machine. First of all we must install git client. Further i will talk about linux console version of client. On the next step we must initialize our local repo:
  1. git init
  2. git add <file1> <file2> ...
  3. git commit -m "init commit"
  4. git remote add origin git@remoterepo.host.addr:username/path/to/tempo.git
  5. git push -u origin master
General format for git remote add(4) is:
git remote add remote_name remote_location
Also we have git clone with similar format:
git clone remote_location
You can ask, what the difference between "remote add" and "close" and i answer you:
  1. remote add - creates an entry in your git config and you can either push changes to remote repo
  2. clone - creates new repo (local copy from remote_location). It similar to this code:
git init
git remote add origin remote_location
git pull origin master
Now let's talk about work with branches. First of all you need to create one by command:
git branch
After that you can view branches:
git branch - show your local branches
git branch -a - showing all branches including remote ones
When our new branch was founded in list - we can switch to it:
git checkout <branchname>
After work on it - we can remove it
# delete local
git branch -d <branchname>
# remote
git push origin :<branchname>
We can push changes to remote branch after committing:
git push origin <branchname>:refs/heads/<branchname>
When work on branch was finished - it can pushed to master:
git checkout master
git merge <branchname> --no-ff
git push origin master - push master branch to remote repo
If you need update code from remote repo:
git remote update; - update all remote repos
git pull <remoterepo> <local>