2017-07-04 35 views
0

我想開發一個應用程序,但我面對的一個問題創建通知... 在我的應用我有像時針和分針以及一些其他的東西一些數據的數據庫,我想創建一個通知時間列。使用SQLite

我創建了這些代碼通知:

public void scheduleNotification(Notification notification, int hour, int minute) { 
    Intent notificationIntent = new Intent(getContext().getApplicationContext(), TaskReceiver.class); 
    notificationIntent.putExtra(TaskReceiver.NOTIFICATION_ID, 0); 
    notificationIntent.putExtra(TaskReceiver.NOTIFICATION, notification); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext().getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, hour); 
    calendar.set(Calendar.MINUTE, minute); 
    calendar.set(Calendar.SECOND, 0); 
    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
} 

private Notification getNotification(String content) { 
    Notification.Builder builder = new Notification.Builder(getContext()); 
    builder.setContentTitle("Text Title"); 
    builder.setContentText("Some Text"); 
    builder.setSmallIcon(R.mipmap.ic_launcher); 
    builder.setAutoCancel(true); 
    builder.setVibrate(new long[]{1000, 1000, 1000}); 

    Intent intent = new Intent(getContext(), MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 1, intent, Intent.FLAG_ACTIVITY_NEW_TASK); 
    builder.setContentIntent(pendingIntent); 
    return builder.build(); 
} 

,並調用它像活動:

Cursor c = dbManager.fetch(); 
    if(c != null){ 
     while (c.moveToNext()){ 
      int hour = c.getInt(c.getColumnIndex(DatabaseHelper.CLOCK_HOUR)); 
      int minute = c.getInt(c.getColumnIndex(DatabaseHelper.CLOCK_MINUTE)); 

      scheduleNotification(getNotification("Test"), hour, minute); 
     } 
    }else { 
     Toast.makeText(getContext(), "Cursor is empty", Toast.LENGTH_SHORT).show(); 
    } 

但它不工作,誰能幫助我?

+0

試試這種方式https://stackoverflow.com/questions/44204387/alarmmanager-setinexactrepeating-setwindow-setrepeating-methods-do-not-fire-al/44205413#44205413 –

+0

@AniruddhParihar它不爲我工作,因爲當我讀數據庫中的數據,並將它們設置的通知,通知不顯示。我不知道爲什麼。 – rexo

+0

「不工作」是什麼意思?你看到錯誤信息,你有沒有嘗試過任何調試?如果是,結果如何? – PPartisan

回答

0

問題解決了,如PPartisan如上所述,問題是的PendingIntent。 所以我改變scheduleNotification方法,現在它`做工精細: scheduleNotificaion方法:

public void scheduleNotification(int hour , int minute){ 
    Random random = new Random(); 
    int randomNumber = random.nextInt(500) + 20; 

    Calendar calendar = Calendar.getInstance(); 
    int curHour = calendar.get(Calendar.HOUR_OF_DAY); 
    int curMinute = calendar.get(Calendar.MINUTE); 

    int subHour = hour - curHour; 
    int subMinute = minute - curMinute; 

    if(subHour < 0){ 
     Toast.makeText(this, "Alarm hour Passed...", Toast.LENGTH_SHORT).show(); 
    } else if(subMinute < 0){ 
     Toast.makeText(this, "Alarm minute passed...", Toast.LENGTH_SHORT).show(); 
    } else { 
     Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), randomNumber, intent, Intent.FLAG_ACTIVITY_NEW_TASK); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + subHour * 60 * 60 * 1000 + subMinute * 60 * 1000, pendingIntent); 
    } 
} 

和onCreate方法:

if (listView.getAdapter().getCount() != 0){ 
     emptyText.setVisibility(View.GONE); 
     cursor.moveToFirst(); 
     int hour = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.HOUR)); 
     int minute = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.MINUTE)); 
     scheduleNotification(hour, minute); 
     while (cursor.moveToNext()){ 
      int hourNext = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.HOUR)); 
      int minuteNext = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.MINUTE)); 
      scheduleNotification(hourNext, minuteNext); 
     } 
    } else { 
     //what ever 
    } 

這只是簡單的改變讓自己瞭解的方式.. 感謝PPartisan ...

+0

很高興我的解決方案有所幫助,但僅供參考,目前用於生成唯一ID的系統並不能保證您的ID實際上是唯一的。使用存儲在SQLiteDatabase的'_id'列中的值將非常健壯。或者,您可以創建一個使用「AtomicInteger」的中央計數器(請參閱「View#generateViewId」的源代碼或關於如何執行此操作的想法)......儘管這樣做不會在應用程序重新啓動後生存。 – PPartisan

+0

@PPartisan是的,確切地說。我只是用它來進行測試,並將其固定在最終代碼中。 – rexo