为什么通知不出现



我有一个android应用程序,使用SQLite数据库来存储数据。其中一个字段是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);
    }  
}

你比较日期的方式是错误的。

在比较itime之前,注销它们的值-我认为它们不会相等。

不妨看看这个问题,它展示了在java中比较日期的更好方法:

    如何在Java中比较日期?

为什么比较的方式是错误的?

  1. 首先,你现在比较的是毫秒级
  2. 其次,你正在丢失信息铸造到(int)

尝试和跟踪这样的问题的一个好方法是首先使用静态数据从任何if..then块外部创建Notification。一旦你能做到这一点,那么你就知道你的Notification技能是好的。

您将能够找出是否通知的东西是错误的,或DB的东西。

相关内容

  • 没有找到相关文章

最新更新