android闹铃简单实现
1.闹铃activity
packagecom.example.myalarm; importjava.util.Calendar; importjava.util.Date; importjava.util.Locale; importandroid.media.RingtoneManager; importandroid.net.Uri; importandroid.os.Bundle; importandroid.provider.MediaStore; importandroid.app.Activity; importandroid.app.AlarmManager; importandroid.app.PendingIntent; importandroid.app.TimePickerDialog; importandroid.app.TimePickerDialog.OnTimeSetListener; importandroid.content.Intent; importandroid.database.Cursor; importandroid.util.Log; importandroid.view.Menu; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.TimePicker; publicclassAlarmActivityextendsActivity{ privatestaticfinalStringTAG="AlarmActivity"; AlarmManageralarmManager; Calendarcalendar=Calendar.getInstance(Locale.CHINESE); ButtonsetTime; ButtonsetRing; ButtonsetOver; UriringUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); @Override protectedvoidonCreate(BundlesavedInstanceState){ 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(); } privatevoidsetTimeAndRing(){ setTime.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewview){ setTime(); } }); setRing.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewview){ setRingtone(); } }); setOver.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewview){ setAlarm(calendar); } }); } //启动闹玲,设置闹玲 privatevoidsetAlarm(Calendarcalendar){ Intentintent=newIntent(); intent.setClass(this,AlarmBroadcastReceiver.class); intent.putExtra("msg","Getup!Getup!"); intent.putExtra("ringURI",ringUri.toString()); Log.d(TAG,ringUri.toString()); PendingIntentpendingIntent=PendingIntent.getBroadcast(this,0,intent,0); alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent); } //设置时间 privatevoidsetTime(){ Datedate=newDate(); calendar.setTime(date); inthour=calendar.get(Calendar.HOUR); intminute=calendar.get(Calendar.MINUTE); newTimePickerDialog(this,newOnTimeSetListener(){ @Override publicvoidonTimeSet(TimePickertimePicker,inthour,intminute){ calendar.set(Calendar.HOUR,hour); calendar.set(Calendar.MINUTE,minute); } },hour,minute,true).show(); } //设置闹玲铃声 privatevoidsetRingtone(){ Intentintent=newIntent(); intent.setAction(RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT,false); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,"设置闹玲铃声"); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_ALL); UripickedUri=RingtoneManager.getActualDefaultRingtoneUri(this,RingtoneManager.TYPE_ALARM); if(pickedUri!=null){ intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,pickedUri); ringUri=pickedUri; } startActivityForResult(intent,1); } @Override protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){ super.onActivityResult(requestCode,resultCode,data); if(resultCode!=RESULT_OK){ return; } switch(requestCode){ case1: //获取选中的铃声的URI UripickedURI=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; } } privatevoidgetName(inttype){ UripickedUri=RingtoneManager.getActualDefaultRingtoneUri(this,type); Log.i(TAG,pickedUri.toString()); Cursorcursor=this.getContentResolver().query(pickedUri,newString[]{MediaStore.Audio.Media.TITLE},null,null,null); if(cursor!=null){ if(cursor.moveToFirst()){ Stringring_name=cursor.getString(0); Log.i(TAG,ring_name); String[]c=cursor.getColumnNames(); for(Stringstring:c){ Log.i(TAG,string); } } cursor.close(); } } @Override publicbooleanonCreateOptionsMenu(Menumenu){ getMenuInflater().inflate(R.menu.alarm,menu); returntrue; } }
2,布局文件
<RelativeLayoutxmlns: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" > <Buttonandroid:id="@+id/setTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置时间"/> <Buttonandroid:id="@+id/setRing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/setTime" android:text="设置铃声"/> <Buttonandroid:id="@+id/setOver" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/setRing" android:text="设置完成"/> </RelativeLayout>
3.广播接收闹铃信息:
packagecom.example.myalarm; importjava.io.IOException; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.media.MediaPlayer; importandroid.net.Uri; importandroid.util.Log; importandroid.widget.Toast; publicclassAlarmBroadcastReceiverextendsBroadcastReceiver{ UriringUri; @Override publicvoidonReceive(Contextcontext,Intentintent){ Stringmsg=intent.getStringExtra("msg"); Toast.makeText(context,msg,Toast.LENGTH_SHORT).show(); MediaPlayermp=newMediaPlayer(); Stringuri=intent.getStringExtra("ringURI"); if(uri!=null){ ringUri=Uri.parse(uri); Log.d("AlarmActivity",ringUri.toString()); } try{ mp.setDataSource(context,ringUri); mp.prepare(); }catch(IllegalArgumentExceptione){ e.printStackTrace(); }catch(SecurityExceptione){ e.printStackTrace(); }catch(IllegalStateExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } mp.start(); } }
4.在manifest文件中注册广播
<receiverandroid:name="com.example.myalarm.AlarmBroadcastReceiver"></receiver>
以上代码就是实现Android闹钟的全部代码了,希望大家能够喜欢。