不逼逼,直接上代码
我这里是用广播来实现闹钟效果的
import android.app.AlarmManager;
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.view.View;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private AlarmManager alarmManager;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取闹钟的管理者
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
}
//开启一次性闹钟的onClick事件
public void steatone(View view){
//获取系统当前时间
Calendar calendar=Calendar.getInstance();
int h=calendar.get(Calendar.HOUR_OF_DAY);
int m=calendar.get(Calendar.MINUTE);
//弹出对话框
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);
//确定好选择的时间
//设置闹钟
//04.当之间一到,将执行的响应
Intent intent=new Intent();
intent.setAction("唯一性名字");
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
}
},h,m,true);
timePickerDialog.show();
}
//开启周期性闹钟的onClick事件
public void steattwo(View view){
//获取系统当前时间
Calendar calendar=Calendar.getInstance();
int h=calendar.get(Calendar.HOUR_OF_DAY);
int m=calendar.get(Calendar.MINUTE);
//弹出对话框
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);
//确定好选择的时间
//设置闹钟
//04.当之间一到,将执行的响应
Intent intent=new Intent();
intent.setAction("唯一性名字");
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
}
},h,m,true);
timePickerDialog.show();
}
//关闭所有闹钟闹钟的onClick事件
public void stop(View view){
alarmManager.cancel(pendingIntent);
}
}
广播接受者
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by YaozzzzMABY on 2017/2/22.
*/
public class RingReceived extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if("唯一性名字".equals(intent.getAction())){
Log.i("test","收到广播了");
//跳转到Activity
Intent intent1=new Intent(context,TempActivity.class);
//给Intent设置标志位Flag
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Intent的组成部分:ComponementName extra Action Data Categlay(分组) Flag
context.startActivity(intent1);
}
}
}
如若闹钟激活则跳转到
import android.support.v7.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
public class TempActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_temp);
//播放音乐
mediaPlayer = MediaPlayer.create(this, R.raw.ss1);
mediaPlayer.start();
}
//关闭按钮的事件
public void close(View view){
mediaPlayer.stop();
finish();
}
}