2014-04-08 102 views
0

我想根據他的要求從timepicker接收用戶的輸入,然後我想在此時顯示敬酒信息。我嘗試以下,但它不工作..如何在特定時間設置鬧鐘以顯示敬酒

for (int i = 0; i < mNameList.size(); i++) { 
        Log.i("mName is ", "" 
          + mNameList.get(i).getText().toString()); 
        Log.i("Time is ", "" + timeList.get(i).getText().toString()); 
        String[] time = timeList.get(i).getText().toString() 
          .split(":"); 
        int hour = Integer.parseInt(time[0]); 
        int min = Integer.parseInt(time[1]); 
        Log.i("Hour", "" + hour); 
        Log.i("Min", "" + min); 

        Intent intent = new Intent(MainActivity.this, 
          AlarmReciever.class); 
        PendingIntent alarmIntent = PendingIntent.getBroadcast(
          MainActivity.this, 0, intent, 
          PendingIntent.FLAG_UPDATE_CURRENT); 
        AlarmManager aManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

        Calendar firingcal = Calendar.getInstance(); 
        Calendar currentcal = Calendar.getInstance(); 

        firingcal.set(Calendar.HOUR, hour); 
        firingcal.set(Calendar.MINUTE, min); 

        long intendedTime = firingcal.getTimeInMillis(); 
        long currentTime = currentcal.getTimeInMillis(); 
        if (intendedTime >= currentTime) { 
         aManager.setRepeating(AlarmManager.RTC_WAKEUP, 
           intendedTime, AlarmManager.INTERVAL_DAY, 
           alarmIntent); 
        } else { 
         firingcal.add(Calendar.DAY_OF_MONTH, 1); 
         intendedTime = firingcal.getTimeInMillis(); 

         aManager.setRepeating(AlarmManager.RTC_WAKEUP, 
           intendedTime, AlarmManager.INTERVAL_DAY, 
           alarmIntent); 
        } 
       } 

和我reciever是: -

public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "Hey its Your turn", Toast.LENGTH_LONG).show(); 

    } 

,我在manifest.so提到了我的接收器名稱如何在特定的時間顯示敬酒。

+0

嘗試調試它,並檢查intendedtime和currentTime的價值觀 –

+0

intendedtime和CURRENTTIME是確定它們是正確的。 – Harish

回答

0

經過一番google搜索我得到了我的question.that的解決方案是爲follows..referring thisthis

for (int i = 0; i < mNameList.size(); i++) { 
        Log.i("mName is ", "" 
          + mNameList.get(i).getText().toString()); 
        Log.i("Time is ", "" + timeList.get(i).getText().toString()); 
        String[] time = timeList.get(i).getText().toString() 
          .split(":"); 
        int hour = Integer.parseInt(time[0]); 
        int min = Integer.parseInt(time[1]); 
        Log.i("Hour", "" + hour); 
        Log.i("Min", "" + min); 

        Calendar firingcal = Calendar.getInstance(); 
        Calendar currentcal = Calendar.getInstance(); 

        firingcal.set(Calendar.MONTH, 3); 
        firingcal.set(Calendar.YEAR, 2014); 
        firingcal.set(Calendar.DAY_OF_MONTH, 9); 
        firingcal.set(Calendar.HOUR_OF_DAY, hour); 
        firingcal.set(Calendar.MINUTE, min); 
        firingcal.set(Calendar.SECOND, 00); 

        if (firingcal.compareTo(currentcal) <= 0) { 
         Toast.makeText(MainActivity.this, "Invalid date/time", 
           Toast.LENGTH_LONG).show(); 

        } else { 
         Log.i("Alarm Time in else is ", 
           "" + firingcal.getTime()); 
         scheduleAlarm(firingcal); 
        } 

       } 
      } 


public void scheduleAlarm(Calendar alarmTime) { 

     Toast.makeText(MainActivity.this, 
       "Alarm time is set at" + alarmTime.getTime(), Toast.LENGTH_LONG) 
       .show(); 
     Intent intent = new Intent(MainActivity.this, AlarmReciever.class); 
     final int _id = (int) System.currentTimeMillis(); 
     PendingIntent pIntent = PendingIntent.getBroadcast(MainActivity.this, 
       _id, intent, 0); 
     AlarmManager aManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     aManager.set(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), 
       pIntent); 

    } 
0

在您的清單補充一點:

<receiver android:name="Your.Package.Name.AlarmReciever" /> 

,創造AlarmReciever類:

public class AlarmReciever extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     MainActivity.check(); 
    } 
} 

然後在主要活動中補充一點:

public static AlarmManager am; 
public static PendingIntent sender; 
try { 

      Intent intent1 = new Intent(thisActivity, AlarmReciever.class); 
      sender = PendingIntent.getBroadcast(thisActivity, 2, intent1 ,0); 
      am = (AlarmManager) getSystemService(ALARM_SERVICE); 
      long l = new Date().getTime(); 
      if (l < new Date().getTime()) { 
       l += 100; 
      } 
      am.setRepeating(AlarmManager.RTC_WAKEUP, l, 100, sender); 
     } catch (Exception e) { 
     } 

最後一件事添加check()方法MainActivity :

public static void check() { 
//Compare time and show Toast at particular time 
} 
+0

我需要給我的鬧鐘時間,什麼是 long l = new Date()。getTime(); if(l Harish

1

讓你的鬧鐘一樣:

Intent intent = new Intent(); 
     intent.setClass(_Context, OnAlarmReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(_Context, 234324243, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager alarmManager = (AlarmManager) _Context.getSystemService(Activity.ALARM_SERVICE); 

     Calendar cal = Calendar.getInstance(); 
     cal.add(Calendar.HOUR_OF_DAY, 4); 
     cal.add(Calendar.MINUTE, 30); 
     cal.add(Calendar.SECOND, 00); 

     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
       cal.getTimeInMillis(), 
       AlarmManager.INTERVAL_DAY, 
       pendingIntent); 

現在設置您的接收器像menifest文件:

<receiver android:name="com.televero.receiver.OnAlarmReceiver" > 
     </receiver> 

希望這個作品。

+0

同樣在這裏你顯示了什麼,我寫在上面的問題是什麼區別和你的信息我在清單中提到我的接收機名稱。 – Harish

+0

首先使用setInexactRepeating代替setRepeating,請參閱Android Developers文檔。其次檢查你的鬧鐘時間,如果它設置正確,最後檢查你的接收器是否被叫。 –

+0

不變,它不顯示任何吐司。 – Harish

相關問題