Android鬧鐘設定

來源:互聯網
上載者:User
package com.king.android.media;

import java.util.Calendar;

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

import com.king.android.R;

/**

 * 描述:鬧鐘事件
 * 作者:Andy.Liu
 * 時間: 2012-7-23  上午08:03:24
 **/
public class AlarmActivity extends Activity implements OnClickListener {
    
    Calendar mCalendar;
    TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mCalendar = Calendar.getInstance();
        
        mTextView = (TextView) findViewById(R.id.tv_info);
        mTextView.setVisibility(View.VISIBLE);
        
        Button btnSetAlarm = (Button) findViewById(R.id.btn_first);
        btnSetAlarm.setText("設定鬧鐘");
        btnSetAlarm.setVisibility(View.VISIBLE);
        btnSetAlarm.setOnClickListener(this);
        
        btnSetAlarm = (Button) findViewById(R.id.btn_second);
        btnSetAlarm.setText("取消鬧鐘");
        btnSetAlarm.setVisibility(View.VISIBLE);
        btnSetAlarm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.btn_first:
            setAlarm();
            break;
        case R.id.btn_second:
            setCancel();
            break;
        }
    }
    
    private void setAlarm(){
        mCalendar.setTimeInMillis(System.currentTimeMillis());
        int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
        int miu = mCalendar.get(Calendar.MINUTE);
        new TimePickerDialog(AlarmActivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mCalendar.setTimeInMillis(System.currentTimeMillis());
                mCalendar.set(Calendar.MINUTE, minute);
                mCalendar.set(Calendar.SECOND, 0);
                mCalendar.set(Calendar.MILLISECOND, 0);
                
                Intent intent = new Intent(AlarmActivity.this,AlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                //設定鬧鐘
                am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent);
                //設定周期鬧鐘
                am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10*10000, 24*60*60*1000, pendingIntent);
                
                String tmps = "設定鬧鐘為"+format(hourOfDay)+":"+format(minute);
                mTextView.setText(tmps);
            }
        }, hour, miu, true).show();
        }
    
    
    /**
     * 
     *TODO:取消鬧鐘
     *Author:Andy.Liu
     *Create Time:2012-7-23 上午08:18:09
     *TAG:
     *Return:void
     */
    private void setCancel(){
        Intent intent = new Intent(AlarmActivity.this,AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        //取消鬧鐘
        am.cancel(pendingIntent);
        mTextView.setText(" 鬧鐘已經取消");
    }
    
    /**
     * 
     *TODO:格式化字串
     *Author:Andy.Liu
     *Create Time:2012-7-23 上午08:16:01
     *TAG:@param x
     *Return:void
     */
    private String format(int x){
        String s = x+"";
        if(s.length()==1){
            s="0"+s;
        }
        return s;
    }
    

}

 

package com.king.android.media;

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

/**

 * 描述:鬧鐘廣播
 * 作者:Andy.Liu
 * 時間: 2012-7-23  上午08:00:38
 **/
public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "你設定的時間到了", Toast.LENGTH_LONG).show();
    }

}       

   <receiver android:name=".media.AlarmReceiver" android:process=":remote"/>    注意註冊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.