Pages

Friday, August 28, 2015

Passing data from notification click event to activity android

A lot of time spend until i made it work!

I had lots of notifications and all they should trigger different events in the activity.

The data passed through intent's putExtra methods was not reliable, because of way that intent works. Some extras did not change.

The solution i ended up with is like this:

1) In my custom receiver:
Intent intent = new Intent(null, Uri.parse("some data"), context, MainActivity.class);intent.putExtra("from_notification", true);intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Create notification here. Btw logo should be set like this, because lollipop and higher logo must be only from white color:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
    iconId = R.raw.logo_white;}
else{
    iconId = R.raw.logo;}

mBuilder.setContentIntent(contentIntent);




My MainActivity:
@Overrideprotected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    boolean fromNotification = intent.getBooleanExtra("from_notification", false);
    if (fromNotification){
        String someData = intent.getData().getSchemeSpecificPart();        //some data is the unique string for each notification
    }
}

Also you should call  the method from onCreate, for cases when activity is not yet running when you tap the notification:

onNewIntent(getIntent());

And dont forget to register the receiver in manifest file with appropriate filters.

Saturday, August 22, 2015

android SQLite best practices (how to SQLite, android sqlite guide tutorial)

Some info i feel obliged to persist.

Had lots of problems and knowledge gaps with android SQLite database, now all seems to fit in its places.


I will share the approach i came to in a few months time of debugging and error fixing.


1) To work with android SQLite you must create your own helper class extending SQLiteOpenHelper.


2) You should use a singleton pattern for this helper instance and get the instance where needed in Activity's onCreate method:

public static synchronized DBHelper getInstance(Context context) {
    // Use the application context, which will ensure that you
    // don't accidentally leak an Activity's context.       
    if (sInstance == null) {
        sInstance = new DBHelper(context.getApplicationContext());    
    }
    return sInstance;
}
3) Before each db interaction (insert, delete, etc) open the database: 
db = this.getWritableDatabase();

4) The helper class should have methods for fetching/persisting data. Methods using cursor must close the cursor after using it. Never close the database itself in this class.


5) Use transaction mode for looped interactions:

db = this.getWritableDatabase();
db.beginTransaction();
try {
    for (...) {
        db.insert(...);
    }

    db.yieldIfContendedSafely();    
    db.setTransactionSuccessful();
}catch(Exception exc){
    exc.printStackTrace();
}finally {
    db.endTransaction();
}
6) My approach to Cursors/queries:
db = this.getWritableDatabase();
Cursor c = db.rawQuery("Select * from TABLE where ID = ?",new String[] {id});
c.moveToPosition(-1);
while (c.moveToNext()) {
    String s = c.getString(c.getColumnIndex("col_name"));
}
c.close();
7) onUpgrade method - when you increment the database version, this method is  called. Mine looks like this:
public static final int DATABASE_VERSION = 238;
@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    dropAll(db); 
    makeDB(db);
}

private SQLiteDatabase dropAll(SQLiteDatabase db){
    db.execSQL("DROP TABLE IF EXISTS tasks");
    return db;
}
public static void makeDB(SQLiteDatabase db){
    db.execSQL(CREATE_TABLE_TASKS);
}
private static final String CREATE_TABLE_TASKS = "CREATE TABLE IF NOT EXISTS tasks(user VARCHAR, ID INTEGER PRIMARY KEY AUTOINCREMENT);";
8) onCreate method:
@Overridepublic void onCreate(SQLiteDatabase db) {
    makeDB(db);
}

9) Working with database is very fast, performance drops might be caused by something else usually, so put that code in AsyncTask.

Monday, August 3, 2015

Anonymous messenger Stealthy.ms

Hi!

Last months i'm developing android version of the already popular iphone app Anonymous messenger "Stealthy". (http://stealthy.ms or http://smsSekret.ru)

The initial idea was to enable people to open their hearts to loved ones, anonymously. Or maybe send secret insides to your business partners, or any other kind of 2 way conversation where you wish to remain anonymous.

The realisation came as ability of the app was to send anonymous SMS to anyone from your contact list, and if they download the app they can answer to your anonymous sms, not knowing who it is. You will know who you are talking to, but not they, they will see you as "Secret Friend".

This app acts just like a simple chat/irc, but you are anonymous! There are group chats with people from your locations (also lots of group chats on different topics - cars/games/ etc), where you can troll everybody or even find new friends.

Also a chat roulette functionality is available, where you will be connected to a random active user of the other gender.