i am building a chat application using push notification and it is working well but the problem is when a new message comes in it gets stored int the sqlite but does not refresh in the chat class until i go to the previous class and then back to the chat class before it shows. i don't want to refresh the chatting class every second is their a way to make the database refresh when a new message comes? .When am on chatting.java class and a notification message comes i want the sqlite database to auto refresh. i have succeed in making it save
this is the notification class
package com.mall.first;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Dialog;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.mall.dbs.ContactListAdapter;
import com.mall.dbs.ContactListItems;
import com.mall.dbs.SqlHandler;
import com.mall.our.Chatting;
public class GCMNotificationIntentService extends IntentService {
// Sets an ID for the notification, so it can be updated
public static final int notifyID = 9001;
NotificationCompat.Builder builder;
Dialog pDialog;
int flag = 0;
SqlHandler sqlHandler;
ListView lv;
private static final String TAG_SUCCESS = "success";
private static final String url = "http://ift.tt/1cBzOGX";
ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
JSONParser jsonParser = new JSONParser();
public GCMNotificationIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
lv= getListView();
lv.setDivider(null);
sqlHandler = new SqlHandler(this);
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString(), "");
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString(), "");
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
sendNotification(""
+ extras.get(ApplicationConstants.MSG_KEY),""
+ extras.get(ApplicationConstants.NAME_KEY));
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(final String msg,final String names) {
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("msg", msg);
resultIntent.putExtra("names", names);
SqlHandler sqlHandler = new SqlHandler(this);
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
String post_username = sp.getString("username", "anon");
String query = "INSERT INTO PHONE_CONTACTS(froms,message,too) values ('"+ names+ "','" + msg + "','" + post_username + "')";
sqlHandler.executeQuery(query);
String time = "fdd";
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mNotifyBuilder;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Alert")
.setContentText("You've received new message.")
.setSmallIcon(R.drawable.ic_launcher);
// Set pending intent
mNotifyBuilder.setContentIntent(resultPendingIntent);
// Set Vibrate, Sound and Light
int defaults = 0;
defaults = defaults | Notification.DEFAULT_LIGHTS;
defaults = defaults | Notification.DEFAULT_VIBRATE;
defaults = defaults | Notification.DEFAULT_SOUND;
mNotifyBuilder.setDefaults(defaults);
// Set the content for Notification
mNotifyBuilder.setContentText("New message from "+names);
// Set autocancel
mNotifyBuilder.setAutoCancel(true);
// Post a notification
mNotificationManager.notify(notifyID, mNotifyBuilder.build());
}
private void showlist() {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
String friend = sp.getString("value", "anon");
ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS";
Cursor c1 = sqlHandler.selectQuery(query);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
ContactListItems contactListItems = new ContactListItems();
contactListItems.setFrom(c1.getString(c1
.getColumnIndex("state")));
contactListItems.setSlno(c1.getString(c1
.getColumnIndex("time")));
contactListItems.setName(c1.getString(c1
.getColumnIndex("froms")));
contactListItems.setPhone(c1.getString(c1
.getColumnIndex("message")));
contactList.add(contactListItems);
} while (c1.moveToNext());
}
}
c1.close();
ContactListAdapter contactListAdapter = new ContactListAdapter(
this, contactList);
contactListAdapter.notifyDataSetChanged();
}
}
this is my database class
package com.mall.dbs;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class SqlDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_TABLE = "PHONE_CONTACTS";
public static final String COLUMN1 = "slno";
public static final String COLUMN2 = "froms";
public static final String COLUMN3 = "message";
public static final String COLUMN4 = "too";
public static final String COLUMN5 = "time";
public static final String COLUMN6 = "state";
public static final String KEY_TIMESTAMP = "timestampColumn";
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ DATABASE_TABLE + " (" + COLUMN1
+ " integer primary key autoincrement, " + COLUMN2
+ " text not null, " + COLUMN3 + " text, " + COLUMN4 + " text, " + COLUMN5 + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " + COLUMN6 + " text);";
/*private static final String SCRIPT_CREATE_DATABASE = "CREATE TABLE "+DATABASE_TABLE+" (slno integer primary key autoincrement,from TEXT," +
"message TEXT,too TEXT,time VARCHAR)";
*/
public SqlDbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
the database helper class
package com.mall.dbs;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
public class SqlHandler {
public static final String DATABASE_NAME = "MY_DATABASE";
public static final int DATABASE_VERSION = 1;
Context context;
SQLiteDatabase sqlDatabase;
SqlDbHelper dbHelper;
public SqlHandler(Context context) {
dbHelper = new SqlDbHelper(context, DATABASE_NAME, null,
DATABASE_VERSION);
sqlDatabase = dbHelper.getWritableDatabase();
}
public void executeQuery(String query) {
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
sqlDatabase.execSQL(query);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
}
public Cursor selectQuery(String query) {
Cursor c1 = null;
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
c1 = sqlDatabase.rawQuery(query, null);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
return c1;
}
public void deleteContact(){
sqlDatabase = dbHelper.getWritableDatabase();
String deleteQuery = "DELETE FROM PHONE_CONTACTS ";
sqlDatabase.execSQL(deleteQuery);
}
}
Aucun commentaire:
Enregistrer un commentaire