2012-12-30 47 views
0

上沒有調用的鬧鐘所以我試圖從這個網站實現重複功能 - http://www.java2s.com/Code/Android/Core-Class/Exampleofschedulingoneshotandrepeatingalarms.htm - 進入我的Android應用程序,但由於某些原因,它沒有調用該類,這裏是代碼摘錄。Android

public class MainActivity extends Activity implements SensorEventListener { 

// Schedule repeating alarm 

      Intent intent = new Intent(MainActivity.this, RepeatingAlarm.class); 
      PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, 
       intent, 0); 

      // We want the alarm to go off 30 seconds from now. 
      long firstTime = SystemClock.elapsedRealtime(); 
      firstTime += 15 * 1000; 

      // Schedule the alarm! 
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
      am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 
       15 * 1000, sender); 

      // Tell the user about what we did. 
      Toast.makeText(
        getApplicationContext(), 
        "Scheduled", Toast.LENGTH_LONG) 
        .show(); 

} 

//Schedule alarm for data upload 
class RepeatingAlarm extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Toast.makeText(
        context, 
        "ALARMED!", Toast.LENGTH_LONG) 
        .show(); 
     } 
} 

如果有幫助的完整代碼是在這裏:https://gist.github.com/4410665

+1

接收器是否在清單中聲明? –

+0

那肯定就是這樣,因爲這跟谷歌示例中的代碼基本相同。我猜這是會修復它嗎? – eWizardII

+1

如果這是你的接收器的位置是的(所以如果你有一個實際的子包叫做接收器,那裏有你的RepeatingAlarm類)。 –

回答

0

的解決方案是採取:

//Schedule alarm for data upload 
class RepeatingAlarm extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Toast.makeText(
        context, 
        "ALARMED!", Toast.LENGTH_LONG) 
        .show(); 
     } 
} 

並保存爲RepeatingAlarm.java基本上它自己的類。然後進入mainfest文件並添加這種效果

<receiver android:name="RepeatingAlarm"></receiver>