AlarmActivity.java Code
package
android.example.alarm;
import
java.util.Calendar;
import
android.app.Activity;
import
android.app.AlarmManager;
import
android.app.PendingIntent;
import
android.content.Intent;
import
android.os.Bundle;
import
android.os.SystemClock;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.Toast;
public class AlarmActivity extends Activity {
Toast mToast;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Watch for button clicks.
Button button =
(Button)findViewById(R.id.one_shot);
button.setOnClickListener(mOneShotListener);
button = (Button)findViewById(R.id.start_repeating);
button.setOnClickListener(mStartRepeatingListener);
button = (Button)findViewById(R.id.stop_repeating);
button.setOnClickListener(mStopRepeatingListener);
} // onCreate
private OnClickListener
mOneShotListener = new
OnClickListener() {
public void onClick(View v)
{
// When the alarm goes off, we want to
broadcast an Intent to our
// BroadcastReceiver. Here we make an Intent with an explicit class
// name to have our own receiver
(which has been published in
// AndroidManifest.xml) instantiated
and called, and then create an
// IntentSender to have the intent
executed as a broadcast.
Intent intent = new
Intent(AlarmActivity.this, OneShotAlarm.class);
PendingIntent sender =
PendingIntent.getBroadcast(AlarmActivity.this,
0, intent, 0);
// We want the alarm to go off 30
seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
// Schedule the alarm!
AlarmManager am =
(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
// Tell the user about what we did.
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(AlarmActivity.this, R.string.one_shot_scheduled,
Toast.LENGTH_LONG);
mToast.show();
}
}; // onClickListener
private OnClickListener
mStartRepeatingListener = new
OnClickListener() {
public void onClick(View v)
{
// When the alarm goes off, we want to
broadcast an Intent to our
// BroadcastReceiver. Here we make an Intent with an explicit class
// name to have our own receiver
(which has been published in
// AndroidManifest.xml) instantiated
and called, and then create an
// IntentSender to have the intent
executed as a broadcast.
// Note that unlike above, this
IntentSender is configured to
// allow itself to be sent multiple
times.
Intent intent = new
Intent(AlarmActivity.this, RepeatingAlarm.class);
PendingIntent sender =
PendingIntent.getBroadcast(AlarmActivity.this,
0, intent, 0);
// We want the alarm to go off 30
seconds from now.
long firstTime =
SystemClock.elapsedRealtime();
firstTime += 15*1000;
// Schedule the alarm!
AlarmManager am =
(AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 15*1000,
sender);
// Tell the user about what we did.
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(AlarmActivity.this, R.string.repeating_scheduled,
Toast.LENGTH_LONG);
mToast.show();
}
};
private OnClickListener
mStopRepeatingListener = new
OnClickListener() {
public void onClick(View v)
{
// Create the same intent, and thus a
matching IntentSender, for
// the one that was scheduled.
Intent intent = new Intent(AlarmActivity.this,
RepeatingAlarm.class);
PendingIntent sender =
PendingIntent.getBroadcast(AlarmActivity.this,
0, intent, 0);
// And cancel the alarm.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
// Tell the user about what we did.
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(AlarmActivity.this, R.string.repeating_unscheduled,
Toast.LENGTH_LONG);
mToast.show();
}
};
}
OneShotAlarm.java Code
package
android.example.alarm;
import
android.content.Context;
import
android.content.Intent;
import
android.content.BroadcastReceiver;
import
android.widget.Toast;
// Need the
following import to get access to the app resources, since this
// class is in
a sub-package.
/**
* This is an example of implement an {@link
BroadcastReceiver} for an alarm that
* should occur once.
* <p>
* When the alarm goes off, we show a <i>Toast</i>, a quick
message.
*/
public class OneShotAlarm extends
BroadcastReceiver
{
@Override
public void
onReceive(Context context, Intent intent)
{
Toast.makeText(context,
R.string.one_shot_received, Toast.LENGTH_SHORT).show();
}
}
RepeatingAlarm.java Code
package
android.example.alarm;
// Need the
following import to get access to the app resources, since this
// class is in
a sub-package.
import
android.content.Context;
import
android.content.Intent;
import
android.content.BroadcastReceiver;
import
android.widget.Toast;
/**
* This is an example of implement an {@link
BroadcastReceiver} for an alarm that
* should occur once.
*/
public class RepeatingAlarm extends
BroadcastReceiver
{
@Override
public void
onReceive(Context context, Intent intent)
{
Toast.makeText(context,
R.string.repeating_received, Toast.LENGTH_SHORT).show();
}
}
main.xml Code
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fcf253"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/hello"
android:textColor="#FF0000"
android:textSize="30dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
android:text="@string/alarm_controller"
android:textColor="#0000FF"
/>
<Button
android:id="@+id/one_shot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/one_shot_alarm"
>
<requestFocus />
</Button>
<Button
android:id="@+id/start_repeating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/start_repeating_alarm"
/>
<Button
android:id="@+id/stop_repeating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/stop_repeating_alarm"
/>
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#198101"
android:layout_gravity="center_horizontal"
android:text="DigitalClock"
/>
</LinearLayout>
AndroidManifest.xml code
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.example.alarm"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk android:minSdkVersion="10"
/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name=".AlarmActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<!-- Alarm Samples -->
<receiver
android:name=".OneShotAlarm"
android:process=":remote"
/>
<receiver
android:name="android.example.alarm.RepeatingAlarm"
android:process=":remote"
/>
<activity
android:name="android.example.alarm.AlarmController"
android:label="@string/activity_alarm_controller">
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.SAMPLE_CODE"
/>
</intent-filter>
</activity>
</application>
</manifest>
strings.xml Code
<?xml version="1.0"
encoding="utf-8"?>
<resources>
<string name="hello">Alarm Example</string>
<string name="app_name">Android Alarm </string>
<string name="activity_alarm_controller">App/Alarm/Alarm
Controller</string>
<string name="alarm_controller">This demonstrates how
to schedule and handle
one-shot and repeating alarms.</string>
<string name="one_shot_alarm">One Shot Alarm </string>
<string name="start_repeating_alarm">Start Repeating Alarm</string>
<string name="stop_repeating_alarm">Stop Repeating Alarm</string>
<string name="one_shot_scheduled">One-shot alarm will
Show in 30 seconds based on
the real time clock. Try changing the current time before then!</string>
<string name="one_shot_received">The one-shot Alarm
Notification !!!</string>
<string name="repeating_received">The repeating alarm
SHOW in every 15 seconds</string>
<string name="repeating_scheduled">Repeating alarm will
show in 15 seconds and
every 15 seconds after based on the
elapsed realtime clock</string>
<string name="repeating_unscheduled">Repeating alarm has
been unscheduled</string>
</resources>
Download Android Alarm Example Code
More info
Android Status Bar Notification
Android Toast Notification
-->
Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android
สอนเขียน Android สอนเขียนโปรแกรม Android
http://androidcontrol.blogspot.com/2012/01/beginning-android-training-android.html
ติดต่อ amphancm@gmail.com
ติดต่อ amphancm@gmail.com