91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android 設置鬧鐘及通知示例

發布時間:2020-10-03 14:36:01 來源:腳本之家 閱讀:204 作者:夜的千章 欄目:移動開發

簡單說一下這次demo內容,首先做一個設置一次性鬧鐘,先得到alarmManager,打開一個時間對話框,在里面設置鬧鐘的時間,時間一到發送廣播,然后廣播接受者接到跳轉到新的activity播放音樂。接著是一個反復鬧鐘,最后是一個簡單的通知,具體代碼如下:

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 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 setAlarm(View view){

    //獲取當前系統的時間
    Calendar calendar=Calendar.getInstance();
    int hour=calendar.get(Calendar.HOUR_OF_DAY);
    int minute=calendar.get(Calendar.MINUTE);


    //彈出時間對話框
    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);


        Intent intent=new Intent();
        intent.setAction("com.zking.g150820_android28_alarm_notification.RING");

        //將來時態的跳轉 ang eng ing ong
        PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);

        //設置鬧鐘
        alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);

        //時間一到,發送廣播(鬧鐘響了)
        //廣播接受者中(跳轉Activity)
        // 跳轉Activity,在這個Activity中播放音樂
      }
    },hour,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);


    //彈出時間對話框
    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);


        Intent intent=new Intent();
        intent.setAction("com.zking.g150820_android28_alarm_notification.RING");

        //將來時態的跳轉 ang eng ing ong
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);

        //設置鬧鐘
//        alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);

        //設置周期鬧鐘
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);

        //時間一到,發送廣播(鬧鐘響了)
        //廣播接受者中(跳轉Activity)
        // 跳轉Activity,在這個Activity中播放音樂

      }
    },hour,minute,true);

    timePickerDialog.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("今日大事件");
    builder.setContentText("騎士擊敗勇士奪冠");
    builder.setDefaults(NotificationCompat.DEFAULT_ALL);

    builder.setAutoCancel(true);

    builder.setSmallIcon(android.R.drawable.ic_media_play);

    builder.setContentIntent(PendingIntent.getActivity(this,0x102,new Intent(this,RingActivity.class),0));

    Notification notification=builder.build();

    //發送通知
    notificationManager.notify(0x101,notification);

  }

}

布局文件(xml):

<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="ghq.zking.com.ghq_android_28.MainActivity">

  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="設置鬧鐘(一次)"
    android:onClick="setAlarm"
    />
  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="設置鬧鐘(周期)"
    android:onClick="setAlarmCycle"
    />

  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="取消周期"
    android:onClick="cancelCycle"
    />

  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:text="發送通知"
    android:onClick="sendNotification"
    />


</LinearLayout>

廣播接受者代碼:

public class RingReceived extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    if ("ghq.zking.com.ghq_android_28_alarm_notification.RING".equals(intent.getAction())){
      //跳轉到Activity
      Intent intent1=new Intent(context,RingActivity.class);
      //給Intent設置標志位
      intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent1);
    }
  }
}

鬧鐘播放activity代碼:

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

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

长丰县| 集安市| 武定县| 龙陵县| 兴隆县| 乌审旗| 游戏| 四会市| 古丈县| 永修县| 手机| 莱阳市| 府谷县| 扬州市| 麟游县| 木兰县| 潍坊市| 沅陵县| 祥云县| 开原市| 射阳县| 巨野县| 南部县| 错那县| 临沧市| 忻城县| 灌阳县| 汝州市| 辽中县| 克什克腾旗| 望都县| 唐海县| 建水县| 广汉市| 衡山县| 霍山县| 隆德县| 海阳市| 图木舒克市| 六盘水市| 石门县|