【发布时间】:2014-01-21 13:40:06
【问题描述】:
即使在睡眠模式(屏幕锁定)之后,我也试图让振动器保持运行,但应用程序无法运行。我不知道我错过了什么..
除了Wake Lock、BroadcastReceiver还有其他解决方案吗?
(请不要预先判断,它每 4:57 分钟振动一次)
public class MainActivity extends Activity {
public BroadcastReceiver vibrateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = {0, 3000, 297000};
v.vibrate(pattern, 0);
}
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
【问题讨论】:
-
您可能想检查广播接收器的工作原理。
-
除了Broadcastreceiver我应该用什么?振动器不适用于唤醒锁定..
-
我不是说它的方法是错误的,但你的代码是关闭的