如果设备空闲,Android前台服务会很慢

Pab*_*rra 5 android android-service

我有一个Android前台服务,通知通知.在服务中我只是每10秒记录一次"Tick tack",但服务的优先级是每隔X秒在webview中导航,所以我使用新线程并在主线程中工作.

如果我将应用程序连接到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
Run Code Online (Sandbox Code Playgroud)

前台服务的限制是什么?即使设备闲置,我还可以进行硬前台工作吗?

Hos*_*san 1

前几天我创建了这个库,在后台和前台提供类似的服务

而且它的工作绝对正常,没有任何问题。

我选择运行它的原因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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

创建ServiceReciever 类

public class ServiceReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //call the method here

    }
}
Run Code Online (Sandbox Code Playgroud)

在你的里面manifest

<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>
Run Code Online (Sandbox Code Playgroud)

应该就是这样,如果你想运行它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;
    }

}
Run Code Online (Sandbox Code Playgroud)

并将此class编辑ServiceReciever称为这样

 @Override
    public void onReceive(Context context, Intent intent) {
        //call the method here
        Intent background = new Intent(context, BackgroundTask.class);
        context.startService(background);
    }
Run Code Online (Sandbox Code Playgroud)