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如下:
<?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>


Android重复闹钟(每天)的实现,布布扣,bubuko.com

Android重复闹钟(每天)的实现

上一篇:史上最简单的个人移动APP开发入门--jQuery Mobile版跨平台APP开发


下一篇:通过openURL的方式启动其它App