2015-12-04 135 views
2

我正在構建一個應用程序觸發在11:30 IST通知..我已測試它的工作,但11:30後每當我打開我的應用程序它發出通知...計劃通知觸發每當我打開應用程序

我不知道爲什麼,我覺得應該是在條件時檢查一些錯誤......

我想按時一天,第二天觸發通知只有一次......然後... :)

請先解決此問題 在此先感謝

**MainActivity.java** 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
spinner = (ProgressBar)findViewById(R.id.progressBar); 
spinner.setVisibility(View.GONE); 
Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 11); 
calendar.set(Calendar.MINUTE, 30); 
calendar.set(Calendar.SECOND, 00); 
Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent1, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); 
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
} 


**AlarmReciever.java** 

package com.alivestats.alivestats; 

import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.support.v4.app.NotificationCompat; 
public class AlarmReciever extends BroadcastReceiver{ 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    int MID = 1; 

    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent notificationIntent = new Intent(context, MainActivity.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
      notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
      context).setSmallIcon(R.drawable.background) 
      .setContentTitle("title") 
      .setContentText("description").setSound(alarmSound) 
      .setAutoCancel(true).setWhen(when) 
      .setContentIntent(pendingIntent) 
      .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}); 
    notificationManager.notify(MID, mNotifyBuilder.build()); 
    MID++; 

} 

} 
+0

您在BroadcastReceiver中註冊它時,它已被提供給manifest。 –

+0

你是如何得到11點半的時間? –

+0

我剛剛把

回答

1

經歷了這麼多茶葉和咖啡我編寫自己的答案...

long t = System.currentTimeMillis(); 
Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 11); 
calendar.set(Calendar.MINUTE, 30); 
calendar.set(Calendar.SECOND, 00); 
if (t <= calendar.getTimeInMillis()) { 
     //calendar.add(Calendar.DATE, 6); //add 6 days. 
     Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class); 
     intent1.setAction("NOTE"); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); 
     am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
     //am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
    } 

    else{ 

    } 

我剛纔比較系統時間time..if notifcation時間通知小於等於系統時間的真實reciever將work..but如果沒有,那麼什麼都不會發生......

BTW感謝幫助@tiny和@Mahesh

0
Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class); 
intent1 .setAction("ilove"); 


public void onReceive(Context context, Intent intent) { 
if(!"ilove" .equalsIgnoreCase(getAction()))log.e("test",intent.getAction()); 
} 


<receiver android:name="com.android.app.AlaramReceiver" android:exported="false"> 
<intent-filter> 
<action android:name="ilove" /> 
</intent-filter> 
</receiver> 
+0

MainActivity或AlarmReciever中的onRecieve()函數放在哪裏?@ tiny –

+0

這只是在你的AlarmReciever中 –

+0

你只需要在你創建PendingIntent的地方添加Action並在顯示notificatin之前檢查Action –

0

日曆日曆= Calendar.getInstan CE();

  calendar.set(Calendar.HOUR, 6); 

    int myPreferedTime=calendar.get(Calendar.HOUR); 


    Calendar rightNow = Calendar.getInstance(); 

    int currentHour = rightNow.get(Calendar.HOUR_OF_DAY); 
    Log.i("current time","is"+currentHour); 


    int difference=myPreferedTime-currentHour; 

    Log.i("Differencevalue","is"+difference); 

    switch(difference) 
    { 
     case 1: 

      rightNow.add(Calendar.HOUR_OF_DAY, 1); 
      break; 

     case 2: 

      rightNow.add(Calendar.HOUR_OF_DAY, 2); 
      break; 

     case 3: 

      rightNow.add(Calendar.HOUR_OF_DAY, 3); 
      break; 

     case 4: 

      rightNow.add(Calendar.HOUR_OF_DAY, 4); 
      break; 

     case 5: 

      rightNow.add(Calendar.HOUR_OF_DAY, 5); 
      break; 
     case 6: 

      rightNow.add(Calendar.HOUR_OF_DAY, 0); 
      break; 

     case -1: 
      rightNow.add(Calendar.HOUR_OF_DAY,23); 
      break; 

     case -2: 
      rightNow.add(Calendar.HOUR_OF_DAY,22); 
      break; 

     case -3: 
      rightNow.add(Calendar.HOUR_OF_DAY,21); 
      break; 
     case -4: 
      rightNow.add(Calendar.HOUR_OF_DAY,20); 
      break; 
     case -5: 
      rightNow.add(Calendar.HOUR_OF_DAY,19); 
      break; 

     case -6: 
      rightNow.add(Calendar.HOUR_OF_DAY,18); 
      break; 
     case -7: 
      rightNow.add(Calendar.HOUR_OF_DAY,17); 
      break; 
     case -8: 
      rightNow.add(Calendar.HOUR_OF_DAY,16); 
      break; 
     case -9: 
      rightNow.add(Calendar.HOUR_OF_DAY,15); 
      break; 
     case -10: 
      rightNow.add(Calendar.HOUR_OF_DAY,14); 
      break; 
     case -11: 
      rightNow.add(Calendar.HOUR_OF_DAY,13); 
      break; 
     case -12: 
      rightNow.add(Calendar.HOUR_OF_DAY,12); 
      break; 
     case -13: 
      rightNow.add(Calendar.HOUR_OF_DAY,11); 
      break; 
     case -14: 
      rightNow.add(Calendar.HOUR_OF_DAY,10); 
      break; 
     case -15: 
      rightNow.add(Calendar.HOUR_OF_DAY,9); 
      break; 

     case -16: 
      Log.i("am here","bcoz time is 11 clock"); 
      rightNow.add(Calendar.HOUR_OF_DAY, 8); 
      break; 

     case -17: 
      rightNow.add(Calendar.HOUR_OF_DAY,7); 
      break; 
     case -18: 
      rightNow.add(Calendar.HOUR_OF_DAY,6); 
      break; 
     default: 
      break; 


    } 

     Intent intent1 = new Intent(MainActivity.this, MyReceiver.class); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, 0); 
     Log.i("am sending", "yes"); 
     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
     Log.i("time holding with me", "calendar stores time like" + rightNow.getTimeInMillis()); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis(), 
       AlarmManager.INTERVAL_DAY, pendingIntent); 
+0

我工作了一個星期,以克服計劃通知觸發器的問題 - 每次我 - 開放應用程序和提供這種解決方案,如果任何人有任何疑問,請l等我知道 –

+0

請提供更多信息給你的答案。僅僅添加一個腳本是不夠的。 – Hristo

+0

在上面的腳本中,我正在進行數學計算,主要是在當前小時和我的預計小時之間做出區分,並根據差異值決定爲應用程序添加小時,以便它會在每天早上6點之間( AM) - (7:00) –

相關問題