Android: Service ktory przeskakuje, kiedy komorka jest wygaszona

0

Wedlug ponizszego kodu powinna byc dodawana nowa linijka co 10 sekund w "TextView". W linijce jest informacja kiedy dostala dodana np:
////////
1549
1559
1509
1519
1529
////////
Chodzi to poprawnie w genymotion i na komorke (jako aplikacja na ekranie lub aplikacja w tle).

Jednak kiedy automatycznie/recznie komorka jest wygaszona, wynik jest taki np:
////////
1549
1559
1509
1519
1519 <- ?
1519 <- ?
1519 <- ?
1519 <- ?
1519 <- ?
1519 <- ?
1519 <- ?
1539 <- ?
1539 <- ?
1539 <- ?
1539 <- ?
1539 <- ?
1529
1539
////////

Plynnosc dodawania tych linijek, w trakcie wygaszenia komorki, mozna jakos wymusic?

AndroidManifest.xml:

<service
    android:name=".MyService"
    android:exported="false" />

activity_main.xml:

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private String textViewValue = "Co 10 sekund:";

    private SimpleDateFormat simpleDateFormat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(MyService.MY_ACTION);
        registerReceiver(broadcastReceiver, intentFilter);

        simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

        startMyService();
    }

    private void startMyService() {
        Intent intent = new Intent(this, MyService.class);
        long start = System.currentTimeMillis();
        intent.putExtra("start", String.valueOf(start));
        intent.putExtra("odswierzanie", String.valueOf(5 * 1000));
        intent.putExtra("przeskok", String.valueOf(10 * 1000));
        startService(intent);
    }

    private void stopMyService() {
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            textViewValue = textViewValue + "\n" + simpleDateFormat.format(new Date(System.currentTimeMillis()));
            textView.setText(textViewValue);
        }
    };

}

MyService.java:

public class MyService extends Service {

    final static String MY_ACTION = "MY_ACTION";
    private Intent intent;

    private long start;
    private long odswierzanie;
    private long przeskok;

    private Timer timer;
    private TimerTask timerTask;
    private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            long czas = System.currentTimeMillis();
            if (czas > start) {
                start = start + przeskok;
                intent = new Intent();
                intent.setAction(MY_ACTION);
                intent.putExtra("test", true);
                sendBroadcast(intent);
            }
        }
    }

    @Override
    public IBinder onBind(Intent arg0) { return null; }

    @Override
    public void onCreate() {
        super.onCreate();
        timer = new Timer();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String startString = intent.getStringExtra("start");
        start = Long.valueOf(startString).longValue();
        String odswierzanieString = intent.getStringExtra("odswierzanie");
        odswierzanie = Long.valueOf(odswierzanieString).longValue();
        String przeskokString = intent.getStringExtra("przeskok");
        przeskok = Long.valueOf(przeskokString).longValue();
        if (timerTask != null) {
            timerTask.cancel();
            timer.purge();
        }
        timerTask = new MyTimerTask();
        timer.scheduleAtFixedRate(timerTask, 0, odswierzanie);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        if (timerTask != null) {
            timerTask.cancel();
            timer.purge();
        }
        super.onDestroy();
    }

}
0

Spróbuj czas pobierać w MyService i przekazywać do aktywności z UI, a nie pobierać ten czas w aktywności z UI.

0

Po wygaszeniu ekranu android generalnie nie działa, oszczędza baterię. Jeżeli potrzebujesz pracować w tym czasie, musisz złapać PowerManager.PARTIAL_WAKE_LOCK. Niby deprecated, ale w moim przypadku nie znalazłem nic, czym dałoby się to zastąpić. Jakbyś jeszcze potrzebował wybudzić androida, to też to niedawno przerabiałem: wakeUpDevice.

1 użytkowników online, w tym zalogowanych: 0, gości: 1