This article describes the Android based on the broadcast event mechanism to implement a simple timing reminders function code. Share to everyone for your reference, specific as follows:
1.Android Broadcast event mechanism
The broadcast event handling of Android is similar to ordinary event handling. The difference is that the latter is triggered by a component action such as clicking a button, which uses the Sentbroadcast () method to initiate a system-level event broadcast to deliver the message by building the intent object. The reception of broadcast events is implemented by defining a class that inherits broadcast receiver, inherits the class, overwrites its OnReceive () method, and responds to the event in that method. The Android system defines a number of standard broadcast action responses that respond to system broadcast events. For example: action_time_changed (triggered when time changes). However, we can also define our own broadcast receiver receive broadcast events.
2. Realize a simple timing reminding function
Mainly consists of three parts:
1 timing-broadcast by defining activity
2) receive broadcast-receive broadcast by implementing Broadcastreceiver
3 reminders-and alerts users through notification
Now we are going to implement these three parts:
2.1 How to make a broadcast on a regular basis?
Now the phone has the function of the alarm clock, we can use the system provided by the alarm clock function, to the timing, that is, to issue a broadcast. Specifically, it can be implemented with Alarmmanager in Android development.
Alarmmanager provides a system-level hint service that allows you to schedule a service at a certain time.
The steps for using the Alarmmanager are described below:
1 Obtain Alarmmanager instance: Alarmmanager object is not directly instantiated, but is obtained by Context.getsystemservice (context.alarm_serviece) method.
2) define a pendingintent to emit the broadcast.
3 Call Alarmmanager related methods, set timer, repeat reminders and other functions.
The detailed code is as follows (Remindersetting.java):
Package com.
Reminder;
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.view.View;
Import Android.widget.Button;
/** * Trigger the broadcast event and set the alarm */public class Remindersetting extends activity {Button btnenable; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main); /* Create a button.
When you click the button, the alarm clock is enabled/btnenable= (button) Findviewbyid (r.id.btnenable); Btnenable.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {SETR
Eminder (TRUE);
}
}); /** * Set the alarm * * * @param b Whether enable the alarm clock or not/private void Setreminder (bo
Olean b) { Get the Alarmmanager instance Alarmmanager am= (Alarmmanager) Getsystemservice (Alarm_service); Create a pendingintent that would perform a broadcast pendingintent pi= pendingintent.getbroadcast (remindersetting.t
His, 0, new Intent (This,myreceiver.class), 0);
if (b) {//Just use the ' Alarm time '.
Calendar c=calendar.getinstance ();
Schedule an alarm Am.set (Alarmmanager.rtc_wakeup, C.gettimeinmillis (), pi);
} else{//Cancel current alarm am.cancel (PI);
}
}
}
2.2 Receive broadcast
Create a new class to inherit Broadcastreceiver and implement the OnReceive () method. When Broadcastreceiver receives the broadcast, it executes the OnReceive () method. So, we add code to the OnReceive () method, and when we receive the broadcast, we jump to the activity that displays the reminder message. The specific code is as follows (Myreceiver.java):
Package com. Reminder;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
/** * Receive the broadcast and start the activity that would show the alarm/public
class Myreceiver extends broadcastreceiver {
/**
* Called when the Broadcastreceiver is receiving a Intent.
* *
@Override public
void OnReceive (context context, Intent Intent) {/
* start another activity-myalarm to Display the alarm * *
intent.setflags (intent.flag_activity_new_task);
Intent.setclass (context, myalarm.class);
Context.startactivity (intent);
}
Note: After creating the broadcastreceiver, you need to register in Androidmanifest.xml:
<receiver android:name= ". Myreceiver ">
<intent-filter>
<action android:name=" com. Reminder.myreceiver "/>
</intent-filter>
</receiver>
2.3 Reminder function
To create a new activity, we use the Android notification object to alert the user in this activity. We'll add a beep, a textview to display the hint and a button to cancel the reminder.
Among them, the creation notification mainly includes:
1 obtain system-level service Notificationmanager through Context.getsystemservice (Notification_service).
2 Instantiate the notification object and set various properties that we need, such as setting the sound.
3) the Notificationmanager Notify () method is invoked to display notification
Detailed code is as follows: Myalarm.java
Package com.
Reminder;
Import android.app.Activity;
Import android.app.Notification;
Import Android.app.NotificationManager;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.provider.MediaStore.Audio;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView; /** * Display the alarm information * * Public class Myalarm extends activity {/** * A identifier for this Notifica
tion unique within your application * * public static final int notification_id=1;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.my_alarm); Create the instance of Notificationmanager final Notificationmanager nm= (Notificationmanager) Getsystemservice (NOTI
Fication_service);
Create the instance of Notification Notification n=new Notification (); /* Set the sound of the alarm. There are two way of setting the sound *//N.sound=uri.parse ("file:/Sdcard/alarm.mp3 ");
N.sound=uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI, "20");
Post a notification to being shown in the status bar Nm.notify (notification_id, N);
/* Display some information * * TextView tv= (TextView) Findviewbyid (r.id.tvnotification);
Tv.settext ("Hello, it ' s time to bla bla ...");
/* The button by which can cancel the alarm * * button btncancel= (button) Findviewbyid (R.id.btncancel); Btncancel.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View arg0) {n
M.cancel (notification_id);
Finish ();
}
});
}
}
I hope this article will help you with your Android programming.