让手机振动时,屏幕关闭 [英] Allow phone to vibrate when screen turns off
问题描述
我要寻找一种方式,让我的程序,使手机震动后屏幕已经超时关闭。我已经做了很多的研究,并没有发现一些作品。我已经看过了电源管理器类,更具体的WakeLock机制。从很多帖子的声音,我需要使用WakeLock类的PARTIAL_WAKE_LOCK变量。
PARTIAL_WAKE_LOCK - 唤醒锁,保证了CPU运行
。
不过,我不能把它当屏幕关闭振动手机。我知道我使用正确WakeLock,因为我可以得到SCREEN_DIM_WAKE_LOCK工作。是PARTIAL_WAKE_LOCK什么,我找?
@覆盖
公共无效的onCreate(){
super.onCreate();
//注册接收器,手柄屏幕上,屏幕关闭逻辑
IntentFilter的过滤器=新的IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver的mReceiver =新ScreenReceiver();
registerReceiver(mReceiver,过滤器);
}
@覆盖
公共无效ONSTART(意向意图,诠释startId){
布尔screenOn = intent.getBooleanExtra(screen_state,假);
如果(!screenOn){
//获取振动器的情况下,从当前上下文
振动器V =(震动)getSystemService(Context.VIBRATOR_SERVICE);
//这个例子将导致手机震动SOS的莫尔斯code
//在莫尔斯code,S=点 - 点 - 点,O=破折号,破折号,破折号
//有停顿,以独立的点/破折号,字母和单词
//下面的数字重新present毫秒长度
INT点= 200; //的莫尔斯code以毫秒为单位点长度
INT破折号= 500; //在毫秒莫尔斯code冲刺长
INT short_gap = 200; //峡长点/破折号之间
INT medium_gap = 500; //间隙长度字母之间
INT long_gap = 1000; //间隙长度词的
长[]模式= {
0,//立即开始
点,short_gap,圆点,short_gap,圆点,//小号
medium_gap,
破折号,short_gap,破折号,short_gap,破折号,//Ø
medium_gap,
点,short_gap,圆点,short_gap,圆点,//小号
long_gap
};
//只有执行该模式的一个时间(-1表示不重复)
v.vibrate(图案,-1);
} 其他 {
//你的code
}
}
请注意û必须添加使用,允许行到你的Manifest.xml文件,块之外。
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android包=...>
<使用-权限的Android:名称=android.permission.VIBRATE/>
请注意:您还必须在真正的手机测试这个code,仿真器将无法viberate
I am looking for a way to allow my program to make the phone vibrate after the screen has turned off from timing out. I have done lots of research, and have not found something that works. I have looked at the PowerManager class and more specifically the WakeLock mechanism. From the sound of many posts, I would need to use the PARTIAL_WAKE_LOCK variable of the WakeLock class.
PARTIAL_WAKE_LOCK - Wake lock that ensures that the CPU is running.
However, I cannot get it to vibrate the phone when the screen turns off. I know I am using the WakeLock correctly because I can get SCREEN_DIM_WAKE_LOCK to work. Is PARTIAL_WAKE_LOCK what I am looking for?
@Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// This example will cause the phone to vibrate "SOS" in Morse Code
// In Morse Code, "s" = "dot-dot-dot", "o" = "dash-dash-dash"
// There are pauses to separate dots/dashes, letters, and words
// The following numbers represent millisecond lengths
int dot = 200; // Length of a Morse Code "dot" in milliseconds
int dash = 500; // Length of a Morse Code "dash" in milliseconds
int short_gap = 200; // Length of Gap Between dots/dashes
int medium_gap = 500; // Length of Gap Between Letters
int long_gap = 1000; // Length of Gap Between Words
long[] pattern = {
0, // Start immediately
dot, short_gap, dot, short_gap, dot, // s
medium_gap,
dash, short_gap, dash, short_gap, dash, // o
medium_gap,
dot, short_gap, dot, short_gap, dot, // s
long_gap
};
// Only perform this pattern one time (-1 means "do not repeat")
v.vibrate(pattern, -1);
} else {
// YOUR CODE
}
}
note u must Add the uses-permission line to your Manifest.xml file, outside of the block.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
<uses-permission android:name="android.permission.VIBRATE"/>
note : you must also test this code on a real phone , emulator can't viberate
这篇关于让手机振动时,屏幕关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!