闹铃设置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011324501/article/details/50878009

1、首先创建一个广播提示时间到了

package com.example.alarm;

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

public class AlarmReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent arg1) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "你设置的闹钟时间到了", Toast.LENGTH_LONG).show();
	}
}

2、注册广播

<receiver android:name=".AlarmReceiver" android:process=":remote" />
3、写布局文件

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="设置闹铃"/>
    
    <Button 
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="取消闹铃"/>

</LinearLayout>
4、写主代码

package com.example.alarm;

import java.util.Calendar;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {

	private Button determine;
	private Button cancel;
	private TextView text;
	Calendar calendar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		initUI();
	}
	//初始化ui
	private void initUI() {
		// TODO Auto-generated method stub
		 calendar = Calendar.getInstance();
		 text = (TextView)findViewById(R.id.text);
		 determine = (Button)findViewById(R.id.button1);
		 determine.setOnClickListener(new determineListener());
		 cancel = (Button)findViewById(R.id.button2);
		 cancel.setOnClickListener(new cancelListener());	 
	}
	//设置闹铃
	public class determineListener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			calendar.setTimeInMillis(System.currentTimeMillis());
			int hour = calendar.get(Calendar.HOUR_OF_DAY);
			int minute = calendar.get(Calendar.MINUTE);
			new TimePickerDialog(MainActivity.this,new TimePickerDialog.OnTimeSetListener() {
				
				@Override
				public void onTimeSet(TimePicker view, int hourofDay, int minute) {
					// TODO Auto-generated method stub
					//设置日历的时间和当前时间同步
					calendar.setTimeInMillis(System.currentTimeMillis());
					//设置小时和分钟
					 calendar.set(Calendar.HOUR_OF_DAY, hourofDay); 
                     calendar.set(Calendar.MINUTE, minute); 
                     //将秒合毫秒设置为0
                     calendar.set(Calendar.SECOND, 0); 
                     calendar.set(Calendar.MILLISECOND, 0); 
                     //建立Intent和PendingIntent来调用闹钟管理器
                     Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); 
                     PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); 
                     //获取闹钟管理器
                     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
                     //设置闹钟
                     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
                     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000),  
                             (24 * 60 * 60 * 1000), pendingIntent); 
                     String tmps = "设置闹钟时间为" + format(hourofDay) + ":" +format(minute); 
                     text.setText(tmps);                    
				}
			}, hour, minute, true).show();
		}
		
	}
	/**
	 * 
	 * @param time
	 * @return
	 */
	 private String format(int time){ 
	        String str = "" + time; 
	        if(str.length() == 1){ 
	            str = "0" + str; 
	        } 
	        return str; 
	    } 
	//取消闹铃
	public class cancelListener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			//建立Intent和PendingIntent来调用闹钟管理器
			Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); 
            PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); 
            //获取闹钟管理器
            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
            //取消闹钟设置
            alarmManager.cancel(pendingIntent); 
            text.setText("闹铃已取消!"); 
		}
		
	}

}


展开阅读全文

没有更多推荐了,返回首页