如何解决在Android 10上关闭屏幕后,如何每隔X秒执行一次简单的任务?
这只是对问题的一种测试,设备仅每10秒钟振动一次。
它在android 9或更低版本上正常运行,但在android 10上则不能正常运行。
我有一个简单的MainActivity,可在其中启动前台服务:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(android.os.Build.VERSION.SDK_INT >= 26)
startForegroundService(new Intent(MainActivity.this,ForegroundService.class));
else
startService(new Intent(MainActivity.this,ForegroundService.class));
}
public void close(View view) {
stopService(new Intent(MainActivity.this,ForegroundService.class));
finish();
}
}
public class ForegroundService extends Service {
private static Vibrator vibrator;
private static notificationmanager notificationmanager;
private static String channelId;
private static int NOTIFICATION;
private static Timer timer;
public ForegroundService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
super.onStartCommand(intent,flags,startId);
return START_STICKY;
}
@Override
public void onCreate() {
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
notificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
NOTIFICATION = getString(R.string.app_name).hashCode();
if(Build.VERSION.SDK_INT>=26) {
channelId = getApplicationContext().getPackageName();
String channelName = getString(R.string.app_name);
NotificationChannel notificationChannel = new NotificationChannel(channelId,channelName,notificationmanager.IMPORTANCE_LOW);
notificationChannel.setSound(null,null);
notificationChannel.enableLights(false);
notificationChannel.enableVibration(false);
notificationmanager.createNotificationChannel(notificationChannel);
}
startForeground(NOTIFICATION,createNotification(this));
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Log.e("Service","Timer woke up");
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createOneshot(1000,VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(1000);
}
}
},1000,10000);
}
@Override
public void onDestroy() {
stopForeground(true);
timer.cancel();
}
private static Notification createNotification(Context context) {
Notification.Builder builder;
if(Build.VERSION.SDK_INT>=26) {
builder = new Notification.Builder(context,channelId);
}
else {
builder = new Notification.Builder(context);
builder.setPriority(Notification.PRIORITY_LOW);
}
Intent notificationIntent = new Intent(context,MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(context,notificationIntent,0);
builder = builder
.setContentTitle(context.getString(R.string.app_name))
.setContentText("Test")
.setContentIntent(contentIntent)
.setSmallIcon(android.R.drawable.btn_star);
return builder.build();
}
}
当然我在清单中也具有以下权限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.VIBRATE"/>
正如我所说的,它在android 9或更低版本上可以正常工作,但是在android 10上,只要屏幕熄灭并且没有连接电缆,它就会开始延迟每项任务。 它开始等待42秒,58秒,117秒甚至几分钟后才开始等待下一次振动,而不是恰好是10秒,而是相当随机的。 然后,只要我通过电缆将设备连接到电源,并且屏幕仍处于关闭状态,它就会再次正确等待10秒钟。
我可以在手机上找到的任何节能类型(通用或专用于此应用)都被拒绝了。
在声明的行为更改中,我什至找不到任何相关信息: https://developer.android.com/about/versions/10/behavior-changes-all
是否有可能再做一次? 谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。