2017-07-04 13 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方法を変更し、現在正常に動作してそれ `s: 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

嬉しい私の解決策が助けになりましたが、FYI、あなたの現在のユニークID生成システムは、IDが実際に一意であることを保証するものではありません。 SQLiteDatabaseの '_id'カラムに格納された値を使用することは非常に堅牢です。あるいは、 'AtomicInteger'(' View#generateViewId'のソースコードやこれを行う方法のアイデアを参照)を使用する中央カウンタを作成することができます。ただし、これはアプリの再起動にも生き残ることはできません。 – PPartisan

+0

@PPartisanはい、正確です。私はちょうどテストのためにそれを使用し、私は最終的なコードでそれを修正しました... – rexo

関連する問題