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()非常easy出错.导致早八点
 *   的闹钟无效.主要原因是由于该方法的第四个參数引起的.
 *   解决的方法:为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例如以下:
<?xml version="1.0" encoding="utf-8"?

> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cc.cc" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="cc.cc.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="cc.cc.AlarmBroadcastReceiver"> <intent-filter > <action android:name="testalarm0"/> <action android:name="testalarm1"/> </intent-filter> </receiver> </application> </manifest>


main.xml例如以下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>


posted @ 2017-07-07 10:20  claireyuancy  阅读(622)  评论(0编辑  收藏  举报