我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Android实现闹钟功能小Dome

闹钟闹不醒的可以自己去调整下,这个最是最基本的

在依兰等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、网站制作 网站设计制作定制网站制作,公司网站建设,企业网站建设,成都品牌网站建设,营销型网站建设,成都外贸网站制作,依兰网站建设费用合理。

MainActivity

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);


  }

  public void setAlarmOne(View view) {
    //获取当前系统时间
    Calendar calendar = Calendar.getInstance();
    int huor = 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 i, int i1) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, i);
        c.set(Calendar.MINUTE, i1);
        //2确定好选择的时间
        //3设置闹钟
        //4当时间一到,将执行的响应
        Intent intent=new Intent();
        intent.setAction("com.zking.risk_android_alarn_notification.RING");
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
        alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), pendingIntent);
      }
    }, huor, minute, true);
    timePickerDialog.show();
  }

  public void setAlarmCycle(View view) {
    //获取当前系统时间
    Calendar calendar=Calendar.getInstance();
    int hour=calendar.get(Calendar.HOUR_OF_DAY);
    int minute=calendar.get(Calendar.MINUTE);

    //01.弹出时间对话框
    TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
      @Override
      public void onTimeSet(TimePicker timePicker, int i, int i1) {
        Calendar c=Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY,i);
        c.set(Calendar.MINUTE,i1);
        //02.确定好选择的时间
        //03.设置闹钟

        //04.当之间一到,将执行的响应
        Intent intent=new Intent();
        intent.setAction("com.zking.risk_android_alarn_notification.RING");
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x102,intent,0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);
      }
    },hour,minute,true);
    timePickerDialog.show();

  }

  public void cancelAlarmCycle(View view){
    alarmManager.cancel(pendingIntent);
  }


}

RingActivity

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.rooster);
    mediaPlayer.start();
  }

  public void close(View view) {
    mediaPlayer.stop();
    finish();
  }
}

RingReceived

public class RingReceived extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
    if("com.zking.risk_android_alarn_notification.RING".equals(intent.getAction())){
      Log.i("test","收到广播了");

      //跳转到Activity
      Intent intent1=new Intent(context,RingActivity.class);
      //给Intent设置标志位Flag
      intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      //Intent的组成部分:ComponementName extra Action Data Categlay(分组) Flag
      context.startActivity(intent1);
    }
  }
}

activity_main



  

actiity_ring


  

  

最后的清单配置

 

    
      
        
      

    

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


分享文章:Android实现闹钟功能小Dome
文章URL:http://shouzuofang.com/article/iiiiji.html

其他资讯