2

我一直在尝试为我的三星 Galaxy 4 手表编写一个简单的振动应用程序,但遇到了问题。屏幕关闭时手表不会振动。它只会在我的应用程序打开时振动。任何想法为什么会这样?这是我的一些代码:

// MainActivity
Intent i = new Intent(buttonView.getContext(), AlarmService.class);
startService(i);
// AlarmService
Alarm alarm = new Alarm();
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    alarm.setAlarm(this);
}
// Alarm
public void setAlarm(Context context)
{
    AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    long mills = getMillsForNextThirtySeconds();
    am.setExact(AlarmManager.RTC_WAKEUP, mills, pi);
}

@Override
public void onReceive(Context context, Intent intent)
{
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "szelag:test");

    wl.acquire();
    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    VibrationEffect effect = VibrationEffect.createOneShot(1500, 255);
    
    // Watch never vibrates when it is in ambient(?) mode.
    vibrator.vibrate(effect);
    wl.release();
}

谢谢!

4

1 回答 1

0

请参阅此处的文档https://developer.android.com/reference/android/os/VibratorManager

该应用程序应该在前台发生振动。

可能来自前台服务的相关 Android Play 振动

于 2022-01-23T13:20:56.953 回答