您好,登录后才能下订单哦!
本文实例讲述了Android编程实现闹钟的方法。分享给大家供大家参考,具体如下:
在Android中,有一个闹钟的类,AlarmManager
类,我们可以通过其来对系统内的通知进行操作!
本文用到了Service
和Broadcast
两个后台管理,在处理时,曾出现过很多问题,但经过半天的努力,终于解决了!
首先是Main.xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/mText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/setTimeButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/setTimeButton" /> <Button android:id="@+id/cancelButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cancelButton" /> </LinearLayout>
MainActivity.Java:
package com.chiefcto.Alarm; import java.text.Format; 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.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class MainActivity extends Activity { //Properties private Button msetButton; private Button mcancelButton; private AlermReceiver uIReceiver; private TextView mTextView; //就用了Java的日历 private Calendar calendar; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); calendar = Calendar.getInstance(); mTextView = (TextView)this.findViewById(R.id.mText); msetButton = (Button)this.findViewById(R.id.setTimeButton); mcancelButton = (Button)findViewById(R.id.cancelButton); msetButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub calendar.setTimeInMillis(System.currentTimeMillis()); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { calendar.setTimeInMillis(System.currentTimeMillis()); //set(f, value) changes field f to value. calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Intent intent = new Intent(MainActivity.this, AlermReceiver.class); intent.putExtra("music", true); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); AlarmManager am; //获取系统进程 am = (AlarmManager)getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); //设置周期!! am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(10*1000), (24*60*60*1000), pendingIntent); String tmps = "设置闹钟时间为:"+format(hourOfDay)+":"+format(minute); mTextView.setText(tmps); } },hour,minute,true).show(); } }); mcancelButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, AlermReceiver.class); intent.putExtra("music", true); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); AlarmManager am; //获取系统进程 am = (AlarmManager)getSystemService(ALARM_SERVICE); //cancel am.cancel(pendingIntent); mTextView.setText("取消了!"); } }); } private String format(int x) { String s = ""+x; if(s.length() == 1) s = "0"+s; return s; } }
AlarmService.java
package com.chiefcto.Alarm; import java.io.IOException; import android.app.Service; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.RingtoneManager; import android.net.Uri; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; public class AlarmService extends Service { // MediaPlayer实例 private MediaPlayer player; // IBinder实例 @Override public IBinder onBind(Intent intent) { playMusic(); return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); playMusic(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if (player != null) { player.stop(); player.release(); } } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); if (intent != null) { Bundle bundle = intent.getExtras(); if (bundle != null) { if(bundle.getBoolean("music")) playMusic(); else stopMusic(); } } } public void playMusic() { if(player == null) { Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); try { player = new MediaPlayer(); player.setDataSource(this, uri); final AudioManager audioManager = (AudioManager)this .getSystemService(Context.AUDIO_SERVICE); if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { player.setAudioStreamType(AudioManager.STREAM_ALARM); player.setLooping(true); player.prepare(); } } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } if(!player.isPlaying()) { player.start(); } } public void stopMusic() { if (player != null) { player.stop(); try { // 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数 player.prepare(); } catch (IOException ex) { ex.printStackTrace(); } } } }
下面就是本文的关键性代码,了解Activity到Broadcast的机制,这小应用就很好实现了~
AlermReceiver.java
package com.chiefcto.Alarm; import java.io.IOException; import java.util.Calendar; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class AlermReceiver extends BroadcastReceiver { private MediaPlayer mMediaPlayer; Context context; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); int minute = calendar.get(Calendar.MINUTE); CharSequence text = String.valueOf(minute); Toast.makeText(context, text, Toast.LENGTH_LONG).show(); this.context = context; Bundle bundle = intent.getExtras(); Intent serviceIntent = new Intent("chief_musicService"); serviceIntent.putExtras(bundle); if(bundle != null) { Log.i("CTO", String.valueOf(bundle.getBoolean("music"))); if(bundle.getBoolean("music")) context.startService(serviceIntent); else context.stopService(serviceIntent); } //在这里是播放不了的!! //playAlarmRing(); } private void playAlarmRing() { Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); try { mMediaPlayer = new MediaPlayer(); mMediaPlayer.setDataSource(context, uri); final AudioManager audioManager = (AudioManager) context .getSystemService(Context.AUDIO_SERVICE); if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mMediaPlayer.setLooping(true); mMediaPlayer.prepare(); } } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mMediaPlayer.start(); } private void StopAlarmRing() { mMediaPlayer.stop(); } }
要注意的是Mainifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.chiefcto.Alarm" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".AlarmService"> <intent-filter> <action android:name="chief_musicService"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service> <receiver android:name=".AlermReceiver" android:process=":remote"/> </application> <uses-permission android:name="android.permission.MOUT_UNMOUNT_FILESYSTEMS"/> </manifest>
注:在模拟器中是运行不了的~
PS:关于AndroidManifest.xml文件相关属性功能可参考本站在线工具:
Android Manifest功能与权限描述大全:
http://tools.jb51.net/table/AndroidManifest
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。