【问题标题】:keep vibrator running in sleep mode保持振动器在睡眠模式下运行
【发布时间】: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我应该用什么?振动器不适用于唤醒锁定..
  • 我不是说它的方法是错误的,但你的代码是关闭的

标签: java android


【解决方案1】:

首先创建基于例如警报服务的服务调度程序。某事。像这样。

public class ScheduledLocalisationExecutor {

    private Context context;
    private AlarmManager alarmManager;
    private Intent broadcastIntent;
    private PendingIntent pendingIntent;
    private DbxStart dbxStart;


    public ScheduledLocalisationExecutor(Context appContext) {
        context = appContext;
        dbxStart = new DbxStart();
    }

    public void setUpScheduledService(long updateTime) {
        if (dbxStart.getOpenedDatastore() == null) {
            Log.e("DROPBOX", "Dropbox account is not linked...");
            return;
        }
        Log.w("scheduled factory","updating Service!");
        broadcastIntent = new Intent(context, LocalisationUpdatesReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(context, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + updateTime, pendingIntent);
    }

}

现在在 android manifest 中注册你的广播接收器。

 <receiver android:name=".receivers.LocalisationUpdatesReceiver">
        </receiver>

并创建您的广播接收器。

public class LocalisationUpdatesReceiver extends 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);
            }
        }
}

按照这个计划,你就会成功!

【讨论】:

  • BroadcastReceiver.onReceive() 不会执行这么长的操作。您将需要一项服务才能运行它。见我上面的回答。
  • 但是“多少时间”它振动没有问题,但是如何唤醒应用程序,即使屏幕出现问题,也可以将其 ping 通,请仔细阅读上面的问题[第一篇]。
  • 我知道这一点,但vibrate() 操作不应该在BroadcastReceiver.onReceive() 中执行。请检查BroadcastReceiver 的文档,特别是onReceive() 的文档,它说除非接收器注册为在不同的线程上运行,否则它将在主线程上运行,这将使其成为在大约之后被杀死的候选者10 秒。文档还提到需要将其关联到 Service 以使操作正确执行。
  • 好的,我知道接收器线程很适合在很短的时间后被杀死。但是为什么我们不能在 onReceive 中执行振动的事情呢?如果振动短于 10 秒或值较小,则决定广播至死。但是,当然,如果您的振动时间比死亡时间长,您应该实施后台操作。但据我所知:D 最常见应用程序中的振动时间短于 10 秒,并且可以再次触发广播:D 但如果女性想获得某种有用的应用程序:D 在服务中运行它是有意义的:D跨度>
  • 我强调了服务的使用,因为 OP 提供的振动模式是一个连续循环,交替 3 秒振动和 4:57 等待。 OP 可能会用短 3 秒长的振动模式安排警报来取代它,但谁知道呢。
【解决方案2】:

BroadcastReceiver.onReceive() 不是为长时间操作而设计的。你应该在其他地方打电话给vibrate()(在Service或更好的IntentService,就此而言)。

此外,振动模式运行时间过长。如果我是你,我会安排 vibrate() 通过 Android 的调度机制(称为 AlarmManager)每 4:57 运行一个短模式。这个想法是,编写一个调用startService(yourservice)BroadcastReceiver.onReceive() 并安排每次运行BroadcastReceiver(或者更好,因为你需要一个部分唤醒锁来规避睡眠模式,编写一个调用startWakefulService()WakefulBroadcastReceiver.onReceive()) .不要忘记在服务结束时调用YourWakefulBroadcastReceiver.completeWakefulIntent() 来释放唤醒锁。

如果您仍然希望您的服务执行运行那么长时间的振动模式,想法是一样的。

【讨论】:

    【解决方案3】:

    只是想添加一些信息,您可以使用 Immersion Corp 的免费触觉效果库为用户提供独特的触觉效果。这将帮助用户了解这是来自您的应用,而不是常规通知等(无需 3 秒长的嗡嗡声)。

    该库包含 120 多种经过微调的触觉效果,您可以从 here 下载它,并提供链接库并将其命名为 over here 的快速入门指南。

    【讨论】:

      猜你喜欢
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 2012-11-14
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      相关资源
      最近更新 更多