I am building an android application which will listen to whatsapp notification and store it and display it... i have used notificationlistenerService
to do it... i am able to listen all whatsapp notifications my problem is when i restart my application all data from the list is lost ;( so i want to save it in db... and how can i run my app in background i am new(noob) any help any advice appreciated
o/p
public class NotificationService extends NotificationListenerService {
Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
// String ticker = sbn.getNotification().tickerText.toString();
String ticker ="";
if(sbn.getNotification().tickerText !=null) {
ticker = sbn.getNotification().tickerText.toString();
}
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
// String text = extras.getCharSequence("android.text").toString();
int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
Bitmap id = sbn.getNotification().largeIcon;
String text = null;
if (extras.getCharSequence("android.text") != null) {
text = extras.getCharSequence("android.text").toString();
}
if (text == null) {
if (extras.get("android.textLines") != null) {
CharSequence[] charText = (CharSequence[]) extras
.get("android.textLines");
if (charText.length > 0) {
text = charText[charText.length - 1].toString();
}
}
}
// Log.i("Package",pack);
// Log.i("Ticker",ticker);
// Log.i("Title",title);
// Log.i("Text",text);
if(pack.equals("com.whatsapp")) {
Intent msgrcv = new Intent("Msg");
// msgrcv.putExtra("package", pack);
msgrcv.putExtra("ticker", ticker);
msgrcv.putExtra("title", title);
msgrcv.putExtra("text", text);
if (id !=null) {
// if (text.equals("This message was deleted")){
// text="some messages may be deleted";
// msgrcv.putExtra("text", text);
// }
ByteArrayOutputStream stream = new ByteArrayOutputStream();
id.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
msgrcv.putExtra("icon", byteArray);
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}
} else{
return;
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("Msg","Notification Removed");
}
}
Actually, that's not a hard task to do. But, that's a long progress to understand as a beginner (as you you said you are).
I'll lead you to a sqlite
database tutorial in android [tutorial link].
Basically you can use any other database, but I suggest starting from sqlite.
What the tutorial explains is how to make some kind of a wrapper
(the helper class) for the sql connection.
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "notes_db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// create notes table
db.execSQL(Note.CREATE_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
// Create tables again
onCreate(db);
} }
This helper class will help you interact with the local sqlite
database.