Android Alarm Simple Implementation _android

Source: Internet
Author: User
Tags locale set time

1. Alarm Activity

Copy Code code as follows:

Package com.example.myalarm;
Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.Locale;
Import Android.media.RingtoneManager;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.provider.MediaStore;
Import android.app.Activity;
Import Android.app.AlarmManager;
Import android.app.PendingIntent;
Import Android.app.TimePickerDialog;
Import Android.app.TimePickerDialog.OnTimeSetListener;
Import android.content.Intent;
Import Android.database.Cursor;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TimePicker;
public class Alarmactivity extends activity {
private static final String TAG = "alarmactivity";
Alarmmanager Alarmmanager;
Calendar calendar = calendar.getinstance (Locale.chinese);
Button settime;
Button setring;
Button Setover;
Uri Ringuri = Ringtonemanager.getdefaulturi (ringtonemanager.type_alarm);
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_alarm);
Alarmmanager = (Alarmmanager) getsystemservice (Alarm_service);
SetTime = (Button) Findviewbyid (r.id.settime);
setring = (Button) Findviewbyid (r.id.setring);
Setover = (Button) Findviewbyid (r.id.setover);
SetTime ();
Setringtone ();
Settimeandring ();
}
private void settimeandring () {
Settime.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (view view) {
SetTime ();
}
});
Setring.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (view view) {
Setringtone ();
}
});
Setover.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (view view) {
Setalarm (Calendar);
}
});
}
Start the trouble Ling, set the alarm Ling
private void Setalarm (Calendar calendar) {
Intent Intent = new Intent ();
Intent.setclass (this, alarmbroadcastreceiver.class);
Intent.putextra ("msg", "Get up! Get up! ");
Intent.putextra ("Ringuri", ringuri.tostring ());
LOG.D (TAG, ringuri.tostring ());
Pendingintent pendingintent = pendingintent.getbroadcast (this, 0, intent, 0);
Alarmmanager.set (Alarmmanager.rtc_wakeup,calendar.gettimeinmillis (), pendingintent);
}
Set time
private void SetTime () {
Date date = new Date ();
Calendar.settime (date);
int hour = Calendar.get (Calendar.hour);
int minute = Calendar.get (Calendar.minute);
New Timepickerdialog (This, new Ontimesetlistener () {
@Override
public void Ontimeset (Timepicker timepicker, int hour, int minute) {
Calendar.set (Calendar.hour,hour);
Calendar.set (Calendar.minute,minute);
}
}, hour, minute, true). Show ();
}
Set up an alarm ring
private void Setringtone () {
Intent Intent = new Intent ();
Intent.setaction (Ringtonemanager.action_ringtone_picker);
Intent.putextra (Ringtonemanager.extra_ringtone_show_default, false);
Intent.putextra (Ringtonemanager.extra_ringtone_title, "set alarm ring");
Intent.putextra (Ringtonemanager.extra_ringtone_type, Ringtonemanager.type_all);
Uri Pickeduri = Ringtonemanager.getactualdefaultringtoneuri (this,ringtonemanager.type_alarm);
if (pickeduri!=null) {
Intent.putextra (Ringtonemanager.extra_ringtone_existing_uri,pickeduri);
Ringuri = Pickeduri;
}
Startactivityforresult (Intent, 1);
}
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (RESULTCODE!=RESULT_OK) {
Return
}
Switch (requestcode) {
Case 1:
Gets the URI of the selected ringtone
Uri Pickeduri = Data.getparcelableextra (Ringtonemanager.extra_ringtone_picked_uri);
LOG.I (Tag,pickeduri.tostring ());
Ringtonemanager.setactualdefaultringtoneuri (This, ringtonemanager.type_alarm, Pickeduri);
GetName (Ringtonemanager.type_alarm);
Break
Default
Break
}
}
private void GetName (int type) {
Uri Pickeduri = Ringtonemanager.getactualdefaultringtoneuri (this, type);
LOG.I (Tag,pickeduri.tostring ());
Cursor Cursor = This.getcontentresolver (). Query (Pickeduri, new string[]{mediastore.audio.media.title}, NULL, NULL, NULL);
if (cursor!=null) {
if (Cursor.movetofirst ()) {
String ring_name = cursor.getstring (0);
LOG.I (Tag,ring_name);
String[] C = cursor.getcolumnnames ();
for (String string:c) {
LOG.I (tag,string);
}
}
Cursor.close ();
}
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.alarm, menu);
return true;
}
}

2, Layout file

Copy Code code as follows:

<relativelayout 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"
>
<button android:id= "@+id/settime"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Set Time"/>
<button android:id= "@+id/setring"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/settime"
android:text= "Set ringtones"/>
<button android:id= "@+id/setover"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/setring"
android:text= "Setup Complete"/>
</RelativeLayout>

3. Broadcast Receive alarm information:

Copy Code code as follows:

Package com.example.myalarm;
Import java.io.IOException;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.media.MediaPlayer;
Import Android.net.Uri;
Import Android.util.Log;
Import Android.widget.Toast;
public class Alarmbroadcastreceiver extends Broadcastreceiver {
Uri Ringuri;
@Override
public void OnReceive (context context, Intent Intent) {
String msg = Intent.getstringextra ("msg");
Toast.maketext (Context, MSG, toast.length_short). Show ();
MediaPlayer MP = new MediaPlayer ();
String uri = Intent.getstringextra ("Ringuri");
if (uri!=null) {
Ringuri = Uri.parse (Uri);
LOG.D ("Alarmactivity", ringuri.tostring ());
}
try {
Mp.setdatasource (context, Ringuri);
Mp.prepare ();
catch (IllegalArgumentException e) {
E.printstacktrace ();
catch (SecurityException e) {
E.printstacktrace ();
catch (IllegalStateException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
Mp.start ();
}
}

4. Registering the broadcast in the manifest file

Copy Code code as follows:

<receiver android:name= "Com.example.myalarm.AlarmBroadcastReceiver" ></receiver>

The above code is to implement the entire Android alarm clock code, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.