我有一个使用SQLite数据库来存储数据的android应用程序。其中一个字段是 match_date ,其中包含日期和时间。
我想要的是在将当前日期与数据库中存储的日期进行比较后创建包含团队名称和日期的通知,如果匹配,则必须出现通知。
应用程序工作完美,但没有收到任何通知,所以我的错误在哪里,如何解决此错误?
我将不胜感激。
这是我的数据库表:
CREATE TABLE [match_list] (
[_id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[stad_name] VARCHAR(50) NOT NULL,
[team1] VARCHAR(50) NOT NULL,
[team2] VARCHAR(50) NOT NULL,
[stage] varchar(50) NOT NULL,
[match_date] DATETIME NOT NULL,
[flags1] CHAR(255),
[flags2] CHAR(255),
[group_team] CHAR(1));
FirstActivity.java :
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();
Log.e("inside the onCreate", "inside onCreate");
}
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")));
// 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);
////////
Log.e("inside the Shownotification", "index is" + index);
if(time == i){
Log.e("inside the Shownotification", "time is" + time);
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();
}
}
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);
Log.e("inside the preparePendingIntent", "inside preparePendingIntent");
return pendingIntent;
}
private void showNotification() {
PendingIntent notificationIntent = preparePendingIntent();
getAllRows(notificationIntent);
c.moveToFirst();
}
private void displayNotification(Notification notification) {
Log.e("inside the displayNotification", "inside displayNotification");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(myNotificationId, notification);
}
}
答案 0 :(得分:1)
您比较日期的方式是错误的。
在比较它们之前,请先注销i
和time
的值 - 我认为它们不会相等。
而是看看这个问题,它显示了比较java中日期的更好方法:
为什么比较错误的方式?
(int)
尝试跟踪此类问题的一种好方法是首先使用静态数据从任何Notification
块外部创建if..then
。一旦你能做到这一点,那么你就知道你的Notification
技能很好。
然后,您将能够查明通知内容是否错误,或者数据库内容。