Good day friends!
There is a Service
, it starts at boot (well, it seems to be started), creates a task at startup (in fact, several, but they have the same essence). The task does something and falls asleep this way for a while:
TimeUnit.SECONDS.sleep(180);
It seems like everything would be fine, but, for example, for the first time after the launch, the task worked for the first time in almost an hour! Well, it's like that? I did before this through a timer, like this:
TT = new myTimerTask(); T2 = new Timer(); if (TT != null && T2 != null) T2.scheduleAtFixedRate(TT, 1000, 1000*60); //-- раз в минуту
It worked the same way as it got: then 20 times per second, then after 10 minutes. Here I redid it into tasks - a similar garbage. That interval is observed, then how horrible. What could be the reasons?
The Service
itself during hibernation of the phone hangs like Foreground
, and when the body wakes up, it leaves the Foreground
(the customer’s constantly hanging application icon annoys the customer because of the Foreground
, and I Foreground
perverted).
Now I look at the logs - the task worked only when creating the Service
, 25 minutes passed - there were no more launches (after 5 minutes they should be). Entries in the log of the achievement of onDestroy()
Service
'but not. Or is he just silently nailed OS?
Start Service
every time you turn off the screen or what?
====== Addition, (almost) the entire Service code =============
public class LockscreenService extends Service implements SensorEventListener, OnClickListener { private BroadcastReceiver mReceiver; private Thread T = null; WindowManager windowManager; TextView alertText; boolean removed; boolean iam_set_bg; static boolean iam_started = false; int NOTIFY_ID = 0; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Config.log("LSS Created"); if (Config.st == null) Config.st = new StorageInterface(this); else Config.st.link(this); iam_set_bg=false; } // Register for Lockscreen event intents @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("LSS", "Service started 1"); if (!iam_started) { iam_started = true; if (T==null) T = new Thread( new splasher() ); //-- поток показывающий вспышку if (T!=null) T.start(); Log.d("LSS", "Threads started."); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(LockscreenIntentReceiver.BROADCAST_ACTION_NOTE); filter.addAction(LockscreenIntentReceiver.BROADCAST_ACTION_ADV); filter.addAction(Intent.ACTION_WALLPAPER_CHANGED); mReceiver = new LockscreenIntentReceiver(); registerReceiver(mReceiver, filter); SensorManager mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); Sensor LightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); if(LightSensor != null) { // Log.d("LSS", "Sensor.TYPE_LIGHT Available"); mySensorManager.registerListener( this, LightSensor, SensorManager.SENSOR_DELAY_NORMAL); } else { // Log.d("LSS", "Sensor.TYPE_LIGHT NOT Available"); Config.light = 0; } windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); alertText = new TextView(this); alertText.setOnClickListener(this); alertText.setBackgroundColor( Color.parseColor("#666666") ); alertText.setTextColor( Color.parseColor("#ffffff")); alertText.setPadding(20, 20, 20, 20); alertText.setTextSize(22); removed = true; Log.d("LSS", "Inintialized..."); } else { Log.d("LSS", "Get CMD"); if (intent.hasExtra("act")) { int act = intent.getIntExtra("act", 0); if (act == 1) { Config.log("# startForeground"); startForeground(); } if (act == 2) { Config.log("# stopForeground"); this.stopForeground( true ); } if (act == 3) this.hint("Виберите НЕТ для отключения штатной блокировки"); if (act == 4) { if (!removed) windowManager.removeView(alertText); removed = true; } } } return START_STICKY; } // Run service in foreground so it is less likely to be killed by system private void startForeground() { Notification notification = new NotificationCompat.Builder(this) .setContentTitle(getResources().getString(R.string.app_name)) .setTicker(getResources().getString(R.string.app_name)) .setContentText("Running") .setSmallIcon(R.drawable.ic_launcher) .setContentIntent(null) .setOngoing(true) .build(); startForeground(9999, notification); } // Unregister receiver @Override public void onDestroy() { super.onDestroy(); Config.log("LSS Destroyed"); if (T != null) { Thread dummy = T; T = null; dummy.interrupt(); } unregisterReceiver(mReceiver); } public class splasher implements Runnable { @Override public void run() { Log.d("LSS TASK", "RUN started"); while(true) { try { TimeUnit.SECONDS.sleep(180); Log.d("LSS TASK", "RUN show : Config.screen_off=" + Config.screen_off); Config.log("Flash: light=" + onfig.light + " activ=" + Config.curr_active[30] + " set=" + Config.curr_sets[30]); if (Config.locked == 1 && Config.screen_off == 1 && Config.inring == 0 && Config.incall == 0 && Config.light > 20) { Config.adv = 1; Intent intent = new Intent(LockscreenIntentReceiver.BROADCAST_ACTION_ADV); intent.putExtra("act", 5); sendBroadcast(intent); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //--------------- /splasher -------- //---- light sensor--- @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { if (sensor.getType() == Sensor.TYPE_LIGHT) { // TODO } } @Override public void onSensorChanged(SensorEvent event) { if(event.sensor.getType() == Sensor.TYPE_LIGHT) { if (event.values[0] <= 0) return; Config.light = event.values[0]; } } public void hint( String str ) { alertText.setText( str ); alertText.setOnClickListener(this); WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL; // params.x = 0; // params.y = 100; windowManager.addView(alertText, params); removed = false; } @Override public void onClick(View arg0) { // TODO Auto-generated method stub if (!removed) windowManager.removeView(alertText); removed = true; } }
If it helps ...
Service
twitches every time the screen is turned off and on. Those. regularly has the ability to restart, but the logs have no traces of restarting, i.e. he means alive and well. But the task start logs - that is, no. And it happens that there are lighting change logs, but there is no task launch that should occur at the same time ... I don’t misunderstand something.