Android 仿 QQ 锁屏状态下消息提醒(震动 + 提示音)
这里提供一下 ToolUtils.java 工具类里的震动和闹铃代码:
package com.xiao7.pump.Utils;
import android.annotation.SuppressLint;
import android.app.KeyguardManager;
import android.app.Service;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.os.Vibrator;
public class ToolUtils {
private static Vibrator vibrator;
private static PowerManager.WakeLock wakeLock;
/**
唤醒手机屏幕并解锁
@param context
*/
@SuppressLint("InvalidWakeLockTag")
public static void acquire(Context context) {
try {
//获取电源管理器对象
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
//获取 PowerManager.WakeLock 对象
wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
//点亮屏幕 30 秒
wakeLock.acquire(30 * 1000);
//灭屏(释放锁)
if (null != wakeLock) {
wakeLock.release();
}
KeyguardManager km = (KeyguardManager) context.getSystemService(Contex
t.KEYGUARD_SERVICE);
//这里参数”unLock”作为调试时 LogCat 中的 Tag
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
//解锁
kl.disableKeyguard();
} catch (Exception ex) {
}
}
/**
手机震动
@param context
@param isRepeat 是否重复震动
*/
public static void playVibrate(Context context, boolean isRepeat) {
/*
设置震动,用一个 long 的数组来表示震动状态(以毫秒为单位)
如果要设置先震动 1 秒,然后停止 0.5 秒,再震动 2 秒则可设置数组为:long[]{1000, 500, 2000}。
别忘了在 AndroidManifest 配置文件中申请震动的权限
*/
try {
vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
long[] patern = new long[]{1000, 500, 2000};
AudioAttributes audioAttributes = null;
/**
适配 android7.0 以上版本的震动
说明:如果发现 5.0 或 6.0 版本在 app 退到后台之后也无法震动,那么只需要改下方的 Build.VERSION_CODES.N 版本号即可
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM) //key
.build();
vibrator.vibrate(patern, isRepeat ? 1 : -1, audioAttributes);
}else {
vibrator.vibrate(patern, isRepeat ? 1 : -1);
}
} catch (Exception ex) {
}
}
/**
关闭震动
*/
public static void closeVibrate() {
if (vibrator != null) {
vibrator.cancel();
vibrator = null;
}
}
/**
播放系统默认提示音
@return MediaPlayer 对象
@throws Exception
*/
public static void defaultMediaPlayer(Context mContext) {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(mContext, notification);
r.play();
} catch (Exception ex) {
}
}
/**
播放系统默认来电铃声
@return MediaPlayer 对象
@throws Exception
*/
public static void defaultCallMediaPlayer(Context mContext) {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r = RingtoneManager.getRingtone(mContext, notification);
r.play();
} catch (Exception ex) {
}
}
/**
播放系统默认闹钟铃声
@return MediaPlayer 对象
@throws Exception
*/
public static void defaultAlarmMediaPlayer(Context mContext) {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone r = RingtoneManager.getRingtone(mContext, notification);
r.play();
} catch (Exception ex) {
}
}
}
三、在 MainActivity.class 活动的 onCreate()方法里注册广播实例,代码如下:
public class MainActivity extends AppCompatActivity {
//声明一个操作常量字符串
public static final String ACTION_SERVICE_NEED = "action.ServiceNeed";
//声明一个内部广播实例
public ServiceNeedBroadcastReceiver broadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
添加标志位,允许锁屏状态下显示消息,四个标志位分别是:
1.锁屏状态下显示
2.解锁
3.保持屏幕长亮(可选)
4.打开屏幕
当 Activity 启动的时候,它会解锁并亮屏显示
*/
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED //锁屏状态下显示
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD //解锁
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕长亮
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); //打开屏幕
/**
注册广播实例(在初始化的时候)
*/
IntentFilter filter = new IntentFilter();
//给意图过虑器增加一个 Action(用来区分广播来源,相当于是广播的身份证)
filter.addAction(ACTION_SERVICE_NEED);
broadcastReceiver = new ServiceNeedBroadcastReceiver();
registerReceiver(broadcastReceiver, filter);
}
四、Service 服务里给广播接口器发送指令(关于 Service 服务的用法,我就直接省略了,不懂的可以看我其他的博文),代码如下:
public class taskService extends Service {
/**
评论