今天研究了下安卓闹钟,效果分别是响一次和一直不停的响的闹钟
接下来就直接给大家代码,而两种代码都是写在一个项目里面的,所以呢直接就上代码给大家看了
这是main.xml里面的代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.liuqian.android_27alarm.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="setAlarm" android:text="设置闹钟一次" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="setAlarmCycle" android:text="设置闹钟周期" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="cancelCycle" android:text="取消周期" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="sendNotification" android:text="发送通知" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="senddefinitionNotification" android:text="发送自定义通知" /> </LinearLayout>
接下来的是MainActivity中的的代码
package com.liuqian.android_27alarm; import android.app.AlarmManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.app.NotificationCompat; import android.view.View; import android.widget.RemoteViews; import android.widget.TimePicker; import java.util.Calendar; public class MainActivity extends AppCompatActivity { private AlarmManager alarmManager; private PendingIntent pendingIntent; private NotificationManager notificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取闹钟管理者 alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); } //设置闹钟一次 public void setAlarm(View view){ //获取系统当前时间 Calendar calendar=Calendar.getInstance(); int hour=calendar.get(Calendar.HOUR_OF_DAY); int minute=calendar.get(Calendar.MINUTE); //弹出时间对话框 TimePickerDialog builder=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { //时间到 跳转至activity 在activity中播放音乐 Calendar calendar1=Calendar.getInstance(); calendar1.set(Calendar.HOUR_OF_DAY,hourOfDay); calendar1.set(Calendar.MINUTE,minute); //跳转 Intent intent=new Intent(); intent.setAction("com.liuqian.android_27alarm.RING"); //PendingIntent PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x1010,intent,0); //闹钟管理者 参数: 唤醒屏幕, alarmManager.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),pendingIntent); } },hour,minute,true); builder.show(); } //设置闹钟 周期 public void setAlarmCycle(View view){ //获取系统当前时间 Calendar calendar=Calendar.getInstance(); int hour=calendar.get(Calendar.HOUR_OF_DAY); int minute=calendar.get(Calendar.MINUTE); //弹出时间对话框 TimePickerDialog builder=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { //时间到 跳转至activity 在activity中播放音乐 Calendar calendar1=Calendar.getInstance(); calendar1.set(Calendar.HOUR_OF_DAY,hourOfDay); calendar1.set(Calendar.MINUTE,minute); //跳转 Intent intent=new Intent(); intent.setAction("com.liuqian.android_27alarm.RING"); //PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x1010,intent,0); //闹钟管理者 参数: 唤醒屏幕, //设置周期闹钟 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),5000, pendingIntent); } },hour,minute,true); builder.show(); } //取消周期 public void cancelCycle(View view){ alarmManager.cancel(pendingIntent); } //发送通知 public void sendNotification(View view){ //实例化通知管理器 NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //实例化通知 NotificationCompat.Builder builder=new NotificationCompat.Builder(this); //设置值 builder.setContentTitle("Happy every day"); builder.setContentText("今天又是美好的一天"); builder.setDefaults(NotificationCompat.DEFAULT_ALL); builder.setAutoCancel(true); builder.setSmallIcon(android.R.drawable.ic_media_rew); builder.setContentIntent(PendingIntent.getActivity(this,0x102,new Intent(this,RingActivity.class),0)); //发送通知 Notification notification=builder.build(); notificationManager.notify(0x101,notification); } //发送通知 public void senddefinitionNotification(View view){ //实例化通知管理器 NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //实例化通知 NotificationCompat.Builder builder=new NotificationCompat.Builder(this); //设置值 builder.setContentTitle("Happy every day"); builder.setContentText("今天又是美好的一天"); builder.setDefaults(NotificationCompat.DEFAULT_ALL); builder.setAutoCancel(true); builder.setSmallIcon(android.R.drawable.ic_media_rew); builder.setContentIntent(PendingIntent.getActivity(this,0x102,new Intent(this,RingActivity.class),0)); RemoteViews remoteViews=new RemoteViews(this.getPackageName(),R.layout.activity_ring); //发送通知 Notification notification=builder.build(); notification.contentView=remoteViews; notification.bigContentView=remoteViews; notificationManager.notify(0x101,notification); } }
中有几个方法,与xml相对应
接下来是main_ring.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_ring" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.liuqian.android_27alarm.RingActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="哦多可,你再不起床你就要丢掉这一天最美好的早晨了哦" android:textColor="#aabbcc" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="stop" android:text="停止"/> </LinearLayout><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_ring" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.liuqian.android_27alarm.RingActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="哦多可,你再不起床你就要丢掉这一天最美好的早晨了哦" android:textColor="#aabbcc" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="stop" android:text="停止"/> </LinearLayout>
接下来是RingActivity的代码
package com.liuqian.android_27alarm; import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class RingActivity extends AppCompatActivity { private MediaPlayer mediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ring); mediaPlayer = MediaPlayer.create(this, R.raw.love); mediaPlayer.start(); } //停止点击事件 public void stop(View view){ mediaPlayer.stop(); finish(); } }
而最后也是最需要注意到就是接收广播
package com.liuqian.android_27alarm; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; /** * Created by Administrator on 2017/02/14. */ public class RingRecevied extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if("com.liuqian.android_27alarm.RING".equals(intent.getAction())){ //跳转到activity 播放闹钟 Intent intent1=new Intent(context,RingActivity.class); //给INtentm设置一个标志位 intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent1); } } }
也就是跳转页面到RingActivty,然后在manifest文件里面配置就可以了
<receiver android:name=".RingRecevied"> <intent-filter> <action android:name="com.liuqian.android_27alarm.RING"></action> </intent-filter> </receiver>
直接运行代码就可以了。
今天的分享也就到此结束了,希望可以给大家带来一些帮助,如有错误,请指正,本人虚心接受,谢谢!!