我有一个带有通知的android前台服务。 在该服务中,我只是每10秒记录一次“滴答作响”,但是该服务的优先级是每X秒在Web视图中导航一次,因此Im也使用新线程并在主线程中工作。
如果我将应用程序连接到USB,则日志似乎正常,每隔10秒就会调用一次“滴答作响”,如果移动电话处于解锁状态并且我希望在应用程序上记录日志,则同样如此。
但是当我断开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
前台服务的限制是什么?即使设备处于空闲状态,我也可以做前台工作吗?
答案 0 :(得分:0)
前几天,我为这个图书馆建立了类似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
}
}
在您的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>
如果您想在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);
}