溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android實(shí)現(xiàn)鬧鐘功能小Dome

發(fā)布時(shí)間:2020-10-07 17:11:22 來(lái)源:腳本之家 閱讀:386 作者:Risk_平安 欄目:移動(dòng)開(kāi)發(fā)

鬧鐘鬧不醒的可以自己去調(diào)整下,這個(gè)最是最基本的

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) {
    //獲取當(dāng)前系統(tǒng)時(shí)間
    Calendar calendar = Calendar.getInstance();
    int huor = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);


    //1 彈出時(shí)間對(duì)話(huà)框
    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確定好選擇的時(shí)間
        //3設(shè)置鬧鐘
        //4當(dāng)時(shí)間一到,將執(zhí)行的響應(yīng)
        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) {
    //獲取當(dāng)前系統(tǒng)時(shí)間
    Calendar calendar=Calendar.getInstance();
    int hour=calendar.get(Calendar.HOUR_OF_DAY);
    int minute=calendar.get(Calendar.MINUTE);

    //01.彈出時(shí)間對(duì)話(huà)框
    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.確定好選擇的時(shí)間
        //03.設(shè)置鬧鐘

        //04.當(dāng)之間一到,將執(zhí)行的響應(yīng)
        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);

    //播放音樂(lè)
    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","收到廣播了");

      //跳轉(zhuǎn)到Activity
      Intent intent1=new Intent(context,RingActivity.class);
      //給Intent設(shè)置標(biāo)志位Flag
      intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      //Intent的組成部分:ComponementName extra Action Data Categlay(分組) Flag
      context.startActivity(intent1);
    }
  }
}

activity_main

<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.zking.risk_android_alarn_notification.MainActivity">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="設(shè)置鬧鐘(一次)"
    android:onClick="setAlarmOne"
    />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="設(shè)置鬧鐘(周期)"
    android:onClick="setAlarmCycle"
    />
<Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="取消周期鬧鐘"
  android:onClick="cancelAlarmCycle"
  />

</LinearLayout>

actiity_ring

<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.zking.risk_android_alarn_notification.RingActivity"
  android:weightSum="1">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="親愛(ài)的,該起床了"
    android:textSize="30sp"
    />

  <TimePicker
    android:layout_width="324dp"
    android:layout_height="378dp"
    android:id="@+id/timePicker"
    android:layout_weight="0.30" />

<Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="關(guān)閉"
  android:onClick="close"
  />


</LinearLayout>

最后的清單配置

 <activity android:name=".RingActivity"></activity>

    <receiver android:name=".RingReceived">
      <intent-filter>
        <action android:name="com.zking.risk_android_alarn_notification.RING"></action>
      </intent-filter>

    </receiver>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI