Android編程使用AlarmManager設(shè)置鬧鐘的方法
本文實例講述了Android編程使用AlarmManager設(shè)置鬧鐘的方法。分享給大家供大家參考,具體如下:
package com.Aina.Android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* com.Aina.Android
* Pro_AlarmManager
* @author Aina.huang E-mail: 674023920@qq.com
* @version 創(chuàng)建時間:2010 Jul 8, 2010 3:03:19 PM
* 類說明
*/
public class AlamrReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "鬧鐘時間到", Toast.LENGTH_LONG).show();
}
}
package com.Aina.Android;
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.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class Test extends Activity {
/** Called when the activity is first created. */
private TextView tv = null;
private Button btn_set = null;
private Button btn_cel = null;
private Calendar c = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.TextView);
btn_set = (Button) this.findViewById(R.id.Button01);
btn_cel = (Button) this.findViewById(R.id.Button02);
c = Calendar.getInstance();
btn_set.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
c.setTimeInMillis(System.currentTimeMillis());
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
new TimePickerDialog(Test.this,new TimePickerDialog.OnTimeSetListener(){
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// TODO Auto-generated method stub
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(Test.this,AlamrReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(Test.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);//設(shè)置鬧鐘
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), (10*1000), pi);//重復(fù)設(shè)置
tv.setText("設(shè)置的鬧鐘時間為:"+hourOfDay+":"+minute);
}
},hour,minute,true).show();
}
});
btn_cel.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Test.this,AlamrReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(Test.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.cancel(pi);
tv.setText("鬧鐘取消");
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:id="@+id/TextView"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:text="設(shè)置鬧鐘" android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="取消鬧鐘" android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Aina.Android"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Test"
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=".AlamrReceiver" android:process=":remote"></receiver>
</application>
</manifest>
PS:關(guān)于AndroidManifest.xml文件相關(guān)屬性功能可參考本站在線工具:
Android Manifest功能與權(quán)限描述大全:
http://tools.jb51.net/table/AndroidManifest
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android通過AlarmManager類實現(xiàn)簡單鬧鐘功能
- Android 使用AlarmManager和NotificationManager來實現(xiàn)鬧鐘和通知欄
- Android手機鬧鐘服務(wù)AlarmManagerk開發(fā)案例
- 簡單實現(xiàn)Android鬧鐘程序 附源碼
- Android編程實現(xiàn)鬧鐘的方法詳解
- Android實現(xiàn)簡易鬧鐘功能
- 簡單實現(xiàn)Android鬧鐘功能
- Android鬧鐘設(shè)置的解決方案
- Android鬧鐘機制實現(xiàn)定時任務(wù)功能
- Android使用AlarmManager設(shè)置鬧鐘功能
相關(guān)文章
探討Android 的屏幕滾動操作不如 iPhone 流暢順滑的原因
雖然很多Android手機的配置都比iPhone要高,比如大多數(shù)Andorid手機的內(nèi)存都有1GB,而iPhone 4S只有512MB內(nèi)存,但用過iPhone的人都知道Android手機在使用的時候總感覺沒有那么順滑,究竟為什么會出現(xiàn)這種現(xiàn)象呢?2014-07-07
Android獲取設(shè)備隱私 忽略6.0權(quán)限管理
這篇文章主要介紹了Android獲取設(shè)備隱私,忽略6.0權(quán)限管理,感興趣的小伙伴們可以參考一下2016-01-01
android的消息處理機制(圖文+源碼分析)—Looper/Handler/Message
這篇文章寫的非常好,深入淺出;android的消息處理機制(圖+源碼分析)—Looper,Handler,Message是一位大三學(xué)生自己剖析的心得,感興趣的朋友可以了解下哦,希望對你有所幫助2013-01-01
AndroidSDK Support自帶夜間、日間模式切換詳解
這篇文章主要為大家詳細(xì)介紹了AndroidSDK Support自帶夜間、日間模式切換,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android通過HttpURLConnection和HttpClient接口實現(xiàn)網(wǎng)絡(luò)編程
這篇文章主要介紹了Android通過HttpURLConnection和HttpClient接口實現(xiàn)網(wǎng)絡(luò)編程的相關(guān)資料,需要的朋友可以參考下2015-02-02
android自動生成dimens適配文件的圖文教程詳解(無需Java工具類)
這篇文章主要介紹了android自動生成dimens適配文件,無需Java工具類,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

