android设置通知震动,android notification报警 震动

此篇博客介绍了AlarmNotification类,展示如何在Android应用中创建定时警报通知,通过MediaPlayer播放铃声,Vibrator振动,并触发SubSystemSelectActivity。它展示了使用NotificationManager发送定制通知的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.ict.qinyuanminemonitor.tools;

import com.ict.qinyuanminemonitor.R;

import

com.ict.qinyuanminemonitor.SubSystemSelectActivity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.app.Service;

import android.content.Context;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.Vibrator;

public class AlarmNotification {

private final int NOTIFICATION_ID = 0000;

private MediaPlayer mpMediaPlayer;

private Vibrator shake;

private Context context;

public AlarmNotification(Context context) {

super();

this.context = context;

this.mpMediaPlayer = MediaPlayer.create(context,

R.raw.classic_alarm);

this.shake = (Vibrator) context

.getSystemService(Service.VIBRATOR_SERVICE);

}

@SuppressWarnings("deprecation")

public void sendNotification(int icon, String str) {

Intent intent = new Intent(context,

SubSystemSelectActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pi = PendingIntent.getActivity(context, 0,

intent, 0);

// 创建一个Notification

Notification notify = new Notification();

// 为Notification设置图标。该图标显示在状态栏

notify.icon = icon;

// 为Notification设置发送时间

notify.when = System.currentTimeMillis();

// 设置声音

mpMediaPlayer.setLooping(false);

mpMediaPlayer.start();

// 设置默振动

long[] vibrate = new long[] {0,100,150,200};

shake.vibrate(vibrate, -1);

// 设置事件信息

notify.setLatestEventInfo(context, "警告!", str, pi);

// 获取系统的NotificationManager服务

NotificationManager notificationManager =

(NotificationManager) context

.getSystemService(context.NOTIFICATION_SERVICE);

// 发送通知

notificationManager.notify(NOTIFICATION_ID, notify);

}

public void stopNotification(){

mpMediaPlayer.stop();

shake.cancel();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值