话不多说直接上代码一看就明白
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.g160628_android29.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置闹钟(一次)" android:onClick="setAlarmOnce" >Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置闹钟(周期)" android:onClick="setAlarmRepeat" >Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消周期闹钟" android:onClick="cancelAlarmRepeat" >Button> LinearLayout>
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="懒PIG,该出窝了!!!" android:textSize="40sp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止" android:onClick="stop" /> LinearLayout>package com.example.g160628_android29; 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.TimePicker; import java.util.Calendar; public class MainActivity extends AppCompatActivity { private AlarmManager am; private PendingIntent pendingIntent; private NotificationManager notificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取闹钟管理器 am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); //获取通知管理器 notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } public void setAlarmOnce(View view){ //获取当前系统时间 Calendar calendar=Calendar.getInstance(); int hour=calendar.get(Calendar.HOUR_OF_DAY); int minute=calendar.get(Calendar.MINUTE); //1.弹出时间对话框 TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar c=Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY,hourOfDay); c.set(Calendar.MINUTE,minute); //2.获取到时间 hourOfDay minute //3.设置闹钟 PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,new Intent("com.zking.administrator.g160628_android29_alarm_notification.RING"),0); am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent); } },hour,minute,true); timePickerDialog.show(); } public void setAlarmRepeat(View view){ //获取当前系统时间 Calendar calendar=Calendar.getInstance(); int hour=calendar.get(Calendar.HOUR_OF_DAY); int minute=calendar.get(Calendar.MINUTE); //1.弹出时间对话框 TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar c=Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY,hourOfDay); c.set(Calendar.MINUTE,minute); //2.获取到时间 hourOfDay minute //3.设置闹钟 pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x102,new Intent("com.example.g160628_android29.Hug"),0); am.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent); } },hour,minute,true); timePickerDialog.show(); } public void cancelAlarmRepeat(View view){ am.cancel(pendingIntent); }package com.example.g160628_android29; import android.media.MediaPlayer; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; /** * Created by Administrator on 2017/7/23. */ public class RingActivity extends AppCompatActivity { private MediaPlayer mediaPlayer; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activat_ring); mediaPlayer=MediaPlayer.create(this,R.raw.c); mediaPlayer.start();; } public void stop(View view){ mediaPlayer.stop(); finish(); } }package com.example.g160628_android29; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * Created by Administrator on 2017/7/23. */ public class RingReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("com.example.g160628_android29.Hug".equals(intent.getAction())){ Log.i("test","闹钟响了"); Intent intent2=new Intent(context,RingActivity.class); intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent2); } } }配置如下:xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.g160628_android29"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> <activity android:name=".RingActivity">activity> <receiver android:name=".RingReceiver" android:exported="true" android:enabled="true"> <intent-filter> <action android:name="com.example.g160628_android29.Hug">action> intent-filter> receiver> application> manifest>好的完成一个闹钟就出来了