Android之简易的闹钟和通知

1.首先闹钟分两种,一种是一次性的,还有一种就是周期性的。

2.简易的通知。

package com.thm.thm_android_alarm_notification;

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;
  private NotificationManager notificationManager;

  @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 view, int hourOfDay, int minute) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY,hourOfDay);
        c.set(Calendar.MINUTE,minute);

        Intent intent = new Intent();
        intent.setAction("com.thm.thm_android_alarm_notification.RING");

        //将来时态的转跳
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x110,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 view, int hourOfDay, int minute) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY,hourOfDay);
        c.set(Calendar.MINUTE,minute);

        Intent intent = new Intent();
        intent.setAction("com.thm.thm_android_alarm_notification.RING");

        //将来时态的转跳
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x110,intent,0);

        //设置周期闹钟
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),6000, pendingIntent);

        //时间一到就发送广播
        // 广播接受者中(跳转到Activity)
        // 跳转Activity,在这个Activity中播放音乐

      }
    },hour,minute,true);
    timePickerDialog.show();
  }

  //取消周期闹钟
  public  void cancelCycle(View view){
    alarmManager.cancel(pendingIntent);
  }

  //发送通知
  public void sendNotification(View view){
    //实例化通知管理器
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    //实例化一个通知
    NotificationCompat.Builder builder = new  NotificationCompat.Builder(this);
    builder.setContentTitle("今天情人节!");
    builder.setContentText("愿天下有情人都是失散多年的亲兄妹!!");
    builder.setDefaults(NotificationCompat.DEFAULT_ALL);//使用默认的震动
    builder.setSmallIcon(android.R.drawable.ic_media_play);//系统自带的图标
    //点击后自动取消
    builder.setAutoCancel(true);

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


    Notification notification = builder.build();
    //发送通知
    notificationManager.notify(0x120,notification);
  }
}

3.闹钟是通过广播发送的,所有别忘了配置广播

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

4.闹钟跳转音乐播放页面

package com.thm.thm_android_alarm_notification;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

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.a);
    mediaPlayer.start();
  }
  public void stop(View view){
    mediaPlayer.stop();
    finish();
  }
}
package com.thm.thm_android_alarm_notification;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by THM on 2017/2/14.
 */

public class RingReceived extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
    if("com.thm.thm_android_alarm_notification.RING".equals(intent.getAction())){
      Log.i("test","闹钟响了!");
      //跳转到Activity
      Intent intent1 = new Intent(context,RingActivity.class);
      //广播跳Activity要给Intent设置标志位
      intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent1);

    }
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值