- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 android 前台服务,通过通知调用。在服务中,我只是每 10 秒记录一次“Tick tack”,但服务的优先级是每 X 秒在 web View 中导航,因此我也在使用新线程并在主线程中工作。
如果我将应用程序连接到 USB,日志似乎没问题,每 10 秒调用一次“tick tack”,如果手机已解锁并且我在应用程序上查看日志也是如此。
但是当我断开 USB 连接,或者我锁定设备时,就会发生这种情况:
2018-11-14 12:11:53.115 12596-12596/? I/Service: tick tack
2018-11-14 12:12:18.704 12596-12596/? I/Service: tick tack
2018-11-14 12:15:42.572 12596-12596/? I/Service: tick tack
2018-11-14 12:17:30.942 12596-12596/? I/Service: tick tack
2018-11-14 12:17:53.534 12596-12596/? I/Service: tick tack
2018-11-14 12:18:27.118 12596-12596/? I/Service: tick tack
2018-11-14 12:18:37.118 12596-12596/? I/Service: tick tack
2018-11-14 12:18:47.118 12596-12596/? I/Service: tick tack
2018-11-14 12:18:57.121 12596-12596/? I/Service: tick tack
2018-11-14 12:19:25.208 12596-12596/? I/Service: tick tack
2018-11-14 12:19:48.294 12596-12596/? I/Service: tick tack
前台服务的限制是什么?即使设备处于空闲状态,我也可以进行繁重的前台工作吗?
最佳答案
前几天我为类似的东西制作了这个库 service in background and foreground
而且它的工作绝对没有问题。
我选择使用 AlarmManager
运行它的原因是如果应用程序需要执行本地事件,AlarmManager
是一个很好的调度候选者 + 允许应用程序安排可能需要运行或重复超出其生命周期范围的任务。这允许应用程序执行某些功能,即使在应用程序进程或其所有 Android 组件已被系统清理后也是如此。
更新
调用该方法启动服务
public void call(int Value_in_seconds) {
if (Value_in_seconds == (int) Value_in_seconds) {
// Number is integer
Long time = new GregorianCalendar().getTimeInMillis() + Value_in_seconds * 1000;
// create an Intent and set the class which will execute when Alarm triggers, here
// ServiceReciever in the Intent, the onRecieve() method of this class will execute when
// alarm triggers
Intent intentAlarm = new Intent(context, ServiceReciever.class);
// create the object
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(context, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
} else {
Toast.makeText(context, context.getString(R.string.intValue), Toast.LENGTH_SHORT).show();
}
}
创建ServiceReciever
类
public class ServiceReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//call the method here
}
}
在您的 list
中
<application>
<receiver android:name="hossamscott.com.github.backgroundservice.ServiceReciever" android:process=":ff" android:exported="true" android:enabled="true">
</receiver>
<service android:name="hossamscott.com.github.backgroundservice.BackgroundTask"/>
</application>
如果你喜欢在 Thread
中运行它,那就应该是这样比你可以添加下一行
public class BackgroundTask extends Service {
private boolean isRunning;
private Context context;
private Thread backgroundThread;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
this.context = this;
this.isRunning = false;
this.backgroundThread = new Thread(myTask);
}
private Runnable myTask = new Runnable() {
public void run() {
// Do something here
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// do your logic here
}
});
stopSelf();
}
};
@Override
public void onDestroy() {
this.isRunning = false;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!this.isRunning) {
this.isRunning = true;
this.backgroundThread.start();
}
return START_STICKY;
}
}
要调用这个 class
将 ServiceReciever
编辑成这样
@Override
public void onReceive(Context context, Intent intent) {
//call the method here
Intent background = new Intent(context, BackgroundTask.class);
context.startService(background);
}
关于如果设备空闲,Android 前台服务会变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299557/
我已经在谷歌上搜索这个问题一段时间了,但我还没有找到有效的解决方案。 问题是 SSH 登录到我的服务器突然变得很慢。我可以看到身份验证需要大约 10 秒才能继续,这是我的 ssh 详细日志: Open
我正在使用 AVPlayer 在我的项目中播放在线视频。视频播放良好。现在我想减少/增加视频的 fps。以下是我正在使用的代码: self.asset = [AVAsset assetWithURL:
在 Raspberry Pi 上运行两个使用 python gpio 引脚的程序时,一个变慢。一种是磁传感器,另一种是温湿度传感器。后者是放慢速度的。它不是每 2 秒打印一次温度,而是每 5 到 10
我从 Redis 向我的应用程序提供一个 json,然后我对其进行解码和循环。 这是我从 Redis 提供的 json 的样子: [ { "titel": "test 1",
Ejabberd 版本:19.9.0 在发送 OMEMO 消息时(使用 websockets),例如
我们有相当大的代码库(150 多个项目、400000 多行 Java 代码、一些 Groovy 和 Gradle 代码、一些 Perl 代码、一些 XML、大量 JSP 等)。我设法在 Spring
我在一个网站上工作,您可以在其中创建 svg 艺术品,这意味着您可以动态添加元素、缩放、颜色并移动它们。 问题是,当你开始在他们身上施加阴影时,一切都会开始变慢。对于这个的现场演示,this是我正在开
有没有办法分析 Vim 插件? 当我打开一个大的 .py 时,我的 MacVim 变得越来越慢。我知道我可以取消选择所有插件并逐一重新选择以检查哪个插件是罪魁祸首,但是有没有更快的方法? 我的 dot
我正在构建一个JavaFX应用程序。我知道它使用反射,并且反射可能不如我在代码中构建 UI 时那么快。 所以, 如何设计我的 Controller 以使由反射引起的开销尽可能小? 带/不带 @FXML
我对 UITableViewCell 进行了子类化显示从 1 到 70 的数字。 在每个单元格中,我都在检查中奖号码并检查他们的背景。问题是,经过几次滚动后,tableview 变得非常缓慢,甚至无法
如果我想group_by 和filter 那些在数据集中有任何NA 或factor 值的,我想在 dplyr 中使用 any 函数,但发现它对 NAs 或 factor 运行缓慢(但不是为了寻找任何数
我有一个问题。在我的解决方案中,我需要将数千个数据插入数据库。我正在使用批处理准备语句在一个请求中插入多行。在我调用插入几次之后, hibernate 变得更慢了。 我猜它会在我提交后检查数据库是否有
我从 json url 获取数据,但是当我想加载图像时,速度非常慢! class NewsTableViewController: UITableViewController { var id
我有一个相当简单的托管 Realm 对象 RealmAlertItem由一些字符串和 float 组成。我有一个函数 showAlertNotification()随后被调用(从网络外部触发)并且它依
请参阅下面的表格结构。 CREATE TABLE `oarc` ( `ID` bigint(20) NOT NULL AUTO_INCREMENT, `zID` int(11) NOT N
IntelliJ 慢得像爬行。键之间没有 1-2 个延迟几乎无法打字。我已经更新了堆大小。我在我的 Macbook Pro 上运行大约 2GB RAM。自从它一直在放缓。我已经增加了堆大小,但无济于事
我的 Web 应用程序遇到了性能问题。发现瓶颈是db。应用程序在具有 4 个 CPU 和 2GB RAM 的 LAMP 服务器 (VPS) 上运行。 将新记录插入数据库(包含大约 100.000 条记
我有关于自定义 DispatchQueue 的问题。 我创建了一个队列,并将其用作captureOutput:方法的队列。这是一个代码片段: //At the file header private
我是一名移动 QA。现在我们有一个关于网络响应和 UI 渲染之间的竞争条件的问题。我们猜测如果 UI 渲染比网络响应慢,那么它就会崩溃。 我们已经尝试通过使用 Charles 的本地 map 功能来加
我在 firefox 中遇到了一些奇怪的行为,我正在构建一个单页作品集,作为一名平面设计师,编码一直很困难。我想平滑地控制导航,然后向所有元素添加缩放(最初设计为 1920x1080 全屏)。讲师扔了
我是一名优秀的程序员,十分优秀!