我创建了一个应用程序,当用户向某个方向倾斜手机时会发出振动警报。运行应用程序时效果很好,但是,当关闭窗口在后台运行应用程序时,振动停止工作。我如何在后台启用它?
vibrator.vibrate(VibrationEffect.createWaveform(new long[] {0, 1000}, -1));
我也有
<uses-permission android:name="android.permission.VIBRATE"/>
wgx48brx1#
有几种选择,其中之一是使用前台服务.前台服务是在后台运行的服务,其优先级高于后台服务,这意味着它在应用关闭时被系统终止的可能性较低。这是一个示例代码片段:
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(); } }
1u4esq0p2#
除了@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(); } }
2条答案
按热度按时间wgx48brx1#
有几种选择,其中之一是使用前台服务.
前台服务是在后台运行的服务,其优先级高于后台服务,这意味着它在应用关闭时被系统终止的可能性较低。
这是一个示例代码片段:
1u4esq0p2#
除了@MarcM答案外,添加“startMyOwnForeground”,它应该适用于Android 9+
在你的片段中:
VibrationService.java