在后台运行Android应用程序时振动消失



我创建了一个应用程序,当用户将手机倾斜到某个方向时,它会发出振动警报。它在运行应用程序时工作正常,但是,当关闭窗口在后台运行应用程序时,振动停止工作。我如何在后台启用它?

vibrator.vibrate(VibrationEffect.createWaveform(new long[] {0, 1000}, -1));

我还有

<uses-permission android:name="android.permission.VIBRATE"/>

有几个选项。其中一个正在使用前台服务。

前台服务是在后台运行的服务,优先级高于后台服务,这意味着当应用程序关闭时,它不太可能被系统杀死。

这是一个示例代码片段:
public class VibrationForegroundService extends Service {
private Vibrator vibrator;
@Override
public void onCreate() {
super.onCreate();
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(1, createNotification());
vibrator.vibrate(VibrationEffect.createWaveform(new long[] {0, 1000}, -1));
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
vibrator.cancel();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private Notification createNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default")
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Vibration Service")
.setContentText("Vibration is running")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOngoing(true);
return builder.build();
}
}

除了@MarcM答案外,添加"startMyOwnForeground"它应该可以在Android 9+上运行

在你的片段中:

getActivity().startService(new Intent(getActivity(), VibrationService.class));

VibrationService.java

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.os.VibrationEffect;
import android.os.Vibrator;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

public class VibrationdService extends Service {
private Vibrator vibrator;
@Override
public void onCreate() {
super.onCreate();
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_vibration)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
vibrator.vibrate(VibrationEffect.createWaveform(new long[] {0, 1000}, -1));
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
vibrator.cancel();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private Notification createNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default")
.setSmallIcon(R.drawable.ic_vibration)
.setContentTitle("Vibration Service")
.setContentText("Vibration is running")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOngoing(true);
return builder.build();
}
}