Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android重復鬧鐘(每天)的實現

Android重復鬧鐘(每天)的實現

編輯:關於Android編程

MainActivity如下:

package cc.cc;

import java.util.Calendar;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
/**
 * Demo描述:
 * 重復鬧鐘的實現
 * 
 * 注意事項:
 * 1 因為我們是要實現的兩個鬧鐘每天早八點和晚八點,它們是重復的.
 *   所以在PendingIntent.getBroadcast()很容易出錯.導致早八點
 *   的鬧鐘無效.主要原因是因為該方法的第四個參數引起的.
 *   解決辦法:為BroadcastReceiver設置兩個action,在生成Intent
 *   的時候采用了不同的action,從而讓兩個鬧鐘都起作用.
 *   如果有更好的方式請留言指出,多謝.
 * 2 在calendar.set(Calendar.HOUR_OF_DAY, 8)方法的第二個參數
 *   表示小時,請按照24小時制式填寫.不用管此時設備采用什麼時間
 *   制式(12小時或24小時)
 * 
 * 參考資料:
 * 1 http://blog.csdn.net/hudashi/article/details/7060837
 * 2 http://blog.csdn.net/wdaming1986/article/details/6745655
 * 3 http://blog.csdn.net/z1074971432/article/details/17532139
 *   Thank you very much
 *
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		sendAlarmEveryday1(this);
		sendAlarmEveryday2(this);
	}
	
	private void sendAlarmEveryday1(Context context){
		AlarmManager  alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
		Calendar calendar =Calendar.getInstance(Locale.getDefault());
	    calendar.setTimeInMillis(System.currentTimeMillis());
	    calendar.set(Calendar.HOUR_OF_DAY, 8);
	    calendar.set(Calendar.MINUTE, 30);
	    calendar.set(Calendar.SECOND, 0);
	    calendar.set(Calendar.MILLISECOND, 0);

	    Intent intent = new Intent();
	    intent.setAction("testalarm0");
	    PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
	    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
	}
	
	private void sendAlarmEveryday2(Context context){
		AlarmManager  alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
		Calendar calendar =Calendar.getInstance(Locale.getDefault());
	    calendar.setTimeInMillis(System.currentTimeMillis());
	    calendar.set(Calendar.HOUR_OF_DAY, 20);
	    calendar.set(Calendar.MINUTE, 30);
	    calendar.set(Calendar.SECOND, 0);
	    calendar.set(Calendar.MILLISECOND, 0);

	    Intent intent = new Intent();
	    intent.setAction("testalarm1");
	    PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
	    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
	}

	
}

AlarmBroadcastReceiver如下:

package cc.cc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmBroadcastReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		if ("testalarm0".equals(intent.getAction())) {
			System.out.println("第一個鬧鐘");
		}
		
		if ("testalarm1".equals(intent.getAction())) {
			System.out.println("第二個鬧鐘");
		}

	}

}

AndroidManifest.xml如下:



    

    
        
            
                

                
            
        
        
        
            
                
                
            
        
        
    
    


main.xml如下:



    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved