2014-04-18 59 views
-1

我正在創建android鬧鐘aap,在添加貪睡按鈕時報警我收到錯誤,我無法理解任何人可以幫助我?我爲Android鬧鐘創建貪睡但我無法辨認?

buttonSnooze = (Button) findViewById(R.id.btn_snooze); 
     buttonSnooze.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 

        Intent intent = new Intent(this, this.getClass()); 
        PendingIntent pendingIntent = 
         PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

        long currentTimeMillis = System.currentTimeMillis(); 
        long nextUpdateTimeMillis = currentTimeMillis + 5 * DateUtils.MINUTE_IN_MILLIS; 
        Time nextUpdateTime = new Time(); 
        nextUpdateTime.set(nextUpdateTimeMillis); 

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
        alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent); 
      } 
      }); 

和誤差是如下 的方法的getService(上下文,INT,意圖,INT)中的類型的PendingIntent不適用於參數(新View.OnClickListener(){},INT,意圖, INT)

回答

0

嗨,你在setOnClickListener

使用onTouch你需要做的是:

嘗試編寫代碼如下

Button buttonSnooze = (Button) findViewById(R.id.btn_snooze); 
    snooze.setOnTouchListener(new OnTouchListener() { 
     public boolean onTouch(View arg0, MotionEvent arg1) { 
      mMediaPlayer.stop(); 
      finish(); 
      return true; 
     } 
}; 

然後在那裏你撥打了報警,更新時間

Intent intent = new Intent(this, this.getClass()); 
PendingIntent pendingIntent = 
    PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

long currentTimeMillis = System.currentTimeMillis(); 
long nextUpdateTimeMillis = currentTimeMillis + 5 * DateUtils.MINUTE_IN_MILLIS; 
Time nextUpdateTime = new Time(); 
nextUpdateTime.set(nextUpdateTimeMillis); 

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent); 
+0

感謝你的答案之前。但我已經編輯了上面的代碼,請檢查並回答 –

+0

在pendingIntent中.getService方法中出現錯誤,您可以通過其他方式解決它。 –