• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps how to create android notification after compare the currentDate with the stored date in sqlite ?

devleb

Lurker
i have an android application that stored data in sqlite database and what i need is to create a notification according to that date stored in the database in match_date field

when i run the application it take long time to start and after that it force close .

anyone can help me ??? where is my error what is that i am doing wrong??

what is the best way to get my goal i need it so badly.

i will appreciate any help.

i had tried many tutorials and read many tutorials but until now i did not get my answer.


FirstActivity.java


[HIGH] package com.devleb.expandablelistdemo3;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class FirstActivity extends Activity {

TextView txtDate;

private SQLiteDatabase database;
private ExternalDbOpenHelper extDB;

private static final String DB_NAME = "world_cup.db";
// *****Tables name**************************//
private static final String TABLE_NAME = "match_list";

// *****Tbale name******************************

public static final String PLACE_ID = "_id";
public static final String STAD_NAME = "stad_name";
public static final String TEAM1 = "team1";
public static final String TEAM2 = "team2";
public static final String MATCH_DATE = "match_date";
public static final String FLAG1 = "flags1";
public static final String FLAG2 = "flags2";

public static final String[] ALL_KEYS = new String[] { PLACE_ID, STAD_NAME,
TEAM1, TEAM2, MATCH_DATE, FLAG1, FLAG2, };
// *****Tbale name******************************

ItemDetails listdetail;

Cursor c;

private static final int myNotificationId = 1;

// /////////////////////
long diffSeconds;
long diffMinutes;
long diffHours;
long diffDays;
// /////////////////////

// for the alarm amanager and the notification
public static int eventID = 1;

Date d1 = new Date();

SimpleDateFormat format;

// ////////////////////////////////////////////

// private DatabaseHelper dbHelper = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
getDiffDate();

extDB = new ExternalDbOpenHelper(this, DB_NAME);
database = extDB.openDataBase();

showNotification();

}

public void listTeams(View v) {
Intent in = new Intent(getBaseContext(), MainActivity.class);
startActivity(in);
}

public void listGroups(View v) {
Intent in = new Intent(getBaseContext(), GroupList.class);
startActivity(in);
}

public void DisplaySched(View v) {

Intent in = new Intent(getBaseContext(), MatchScheduleList.class);
startActivity(in);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}

public void getDiffDate() {

String dateStop = "06/12/2014 23:00:00";

// HH converts hour in 24 hours format (0-23), day calculation
format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

d1 = new Date();

Date d2 = null;

try {

d2 = format.parse(dateStop);

// in milliseconds
long diff = d2.getTime() - d1.getTime();

long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);

System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");

txtDate = (TextView) findViewById(R.id.txtDifDate);
String resultDate = diffDays + "days " + diffHours + "Hours "
+ diffMinutes + "Min " + diffSeconds + "S";
txtDate.setText(resultDate);

} catch (Exception e) {
e.printStackTrace();
}

}

public Cursor getAllRows(PendingIntent notificationIntent) {
String where = null;

c = database.query(true, TABLE_NAME, ALL_KEYS, where, null, null, null,
null, null);
if (c != null) {

c.moveToFirst();

while (!c.isAfterLast()) {
listdetail = new ItemDetails();

listdetail.setTeam1(c.getString(c.getColumnIndex("team1")));

listdetail.setTeam2(c.getString(c.getColumnIndex("team2")));

listdetail.setDate_match(c.getString(c
.getColumnIndex("match_date")));

}

}
return c;

}

// for the notification

private PendingIntent preparePendingIntent() {
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}

private void showNotification() {
PendingIntent notificationIntent = preparePendingIntent();
getAllRows(notificationIntent);

// if the pick up date from the database is equal to the current date
// the notification will pop up



long i = (int) (new Date().getTime() / 1000);

// // from the internet
int index = c.getColumnIndex("match_date");
long time = c.getLong(index);
// //////

if (time == i) {

NotificationCompat.Builder builder = new Builder(
getApplicationContext());
Notification notification = builder
.setSmallIcon(R.drawable.fifa_2014_4)
.setContentTitle("Up Comming Match")
.setContentText(
c.getString(c.getColumnIndex("match_date"))
+ (c.getColumnIndex("team1"))
+ (c.getColumnIndex("team2"))).build();

displayNotification(notification);

c.moveToNext();

}

}

private void displayNotification(Notification notification) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(myNotificationId, notification);
}

}

[/HIGH]
 
Back
Top Bottom