Android Alarm clock sends broadcast to play music

Source: Internet
Author: User

Android Alarm clock sends broadcast to play music
If there are any unreasonable changes made based on relevant functions through online examples, please propose to learn from each other.
There are a total of three MainActivity. java main programs
AlarmReceiver. java broadcast Receiver
MusicService. java service play music

MainActivity. java

Package com. yqy. yqy_alarm; 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. content. sharedPreferences; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. bundle; import android. text. style. imageSpan; import android. view. View; import android. widget. button; import android. widget. textView; import android. widget. timePicker; public class MainActivity extends Activity {private TextView TV = null; private Button setTime, cancelTime; private Calendar c = null; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); String alarm = getIntent (). getS TringExtra ("alarm"); if (alarm! = Null & alarm. equals ("alarm") {stopService (new Intent ("com. yqy. yqy_alarm.MUSIC ");} TV = (TextView) findViewById (R. id. TV); setTime = (Button) findViewById (R. id. setAlarm); cancelTime = (Button) findViewById (R. id. cancelAlarm); // obtain the Calendar instance, mainly for the following obtaining time c = Calendar. getInstance (); setTime. setOnClickListener (new Button. onClickListener () {@ Override public void onClick (View arg0) {c. setTimeInMillis (System. currentTimeMillis (); int hour = c. get (Calendar. HOUR_OF_DAY); int minute = c. get (Calendar. MINUTE); new TimePickerDialog (MainActivity. this, minute, new TimePickerDialog. onTimeSetListener () {@ Override public void onTimeSet (TimePicker view, int hourOfDay, int minute) {// sets the calendar time, mainly to synchronize the calendar year, month, and day with the current c. setTimeInMillis (System. currentTimeMillis (); // set the hour minute, second and millisecond to 0 c. set (Calendar. HOUR_OF_DAY, hourOfDay); c. set (Calendar. MINUTE, minute); c. set (Calendar. SECOND, 0); c. set (Calendar. MILLISECOND, 0); int requestCode = 0; // the unique identifier of the alarm. Intent intent = new Intent (MainActivity. this, AlarmReceiver. class); intent. putExtra ("requestCode", requestCode); PendingIntent pi = PendingIntent. getBroadcast (MainActivity. this, requestCode, intent, 0); // obtain the AlarmManager instance AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE); // preset an alert am based on the current time. set (AlarmManager. RTC_WAKEUP, c. getTimeInMillis (), pi);/*** the first parameter is the alarm type; the second parameter is the delay time of the first execution, which can be delayed or executed immediately; the third parameter indicates that the repeat period is one day *, which means to set the repeat period of the alarm. * // am. setRepeating (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + (60*1000), // (24*60*60*1000), pi); // am. setRepeating (AlarmManager. RTC_WAKEUP, c. getTimeInMillis (), // 1000*60*5, pi); String msg = hourOfDay + ":" + minute; TV. setText ("currently set alarm time:" + msg) ;}, hour, minute, true ). show (); // for the five parameters in the TimePickerDialog above, see: http://blog.csdn.net/yang_hui1986527/article/details/6839342}); cancelTime. setOnClickListener (new Button. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (MainActivity. this, AlarmReceiver. class); PendingIntent pi = PendingIntent. getBroadcast (MainActivity. this, 0, intent, 0); AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE); // cancel the alarm am. cancel (pi); TV. setText ("Cancel alarm"); // cancel music stopService (new Intent ("com. yqy. yqy_alarm.MUSIC "));}});}}

AlarmReceiver
Package com. yqy. yqy_alarm; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. broadcastReceiver; import android. content. componentName; import android. content. context; import android. content. intent; import android. widget. toast; public class AlarmReceiver extends BroadcastReceiver {Intent intent; int requestCode =-1; @ Overridepublic Void onReceive (Context context, Intent arg1) {requestCode = arg1.getIntExtra ("requestCode", 0); icationicationmanager nm = (icationicationmanager) context. getSystemService (Context. NOTIFICATION_SERVICE); Notification n = new Notification (R. drawable. ic_launcher, "Hello, there! ", System. currentTimeMillis (); n. flags = Notification. FLAG_AUTO_CANCEL; intent = new Intent (context, MainActivity. class); intent. putExtra ("alarm", "alarm"); intent. setComponent (new ComponentName ("com. yqy. yqy_alarm "," com. yqy. yqy_alarm.MainActivity "); intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP | Intent. FLAG_ACTIVITY_NEW_TASK | Intent. FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); // set the start mode to PendingIntent co NtentIntent = PendingIntent. getActivity (context, 0, intent, PendingIntent. FLAG_CANCEL_CURRENT); n. setLatestEventInfo (context, "Hello, there! "," Hello, there, I'm john. ", contentIntent); nm. notify (R. string. app_name, n); Toast. makeText (context, "alarm time to", Toast. LENGTH_SHORT ). show (); intent = new Intent ("com. yqy. yqy_alarm.MUSIC "); // start the Service to play the music context. stopService (new Intent ("com. yqy. yqy_alarm.MUSIC "); context. startService (intent );}}

MusicService
package com.yqy.yqy_alarm;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;public class MusicService extends Service {      private MediaPlayer player;      @Override      public IBinder onBind(Intent intent) {          // TODO Auto-generated method stub          return null;      }            public void onStart(Intent intent, int startId)      {          super.onStart(intent, startId);          player = MediaPlayer.create(this, R.raw.beep);          player.setLooping(true);        player.start();      }      public void onDestroy()      {          super.onDestroy();          if(player != null){            player.stop();              }    }    }  



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.