-2

I can make an Android timer by creating a time thread(AsyncTask).

However, when the fragment is stopped/or destroyed and restarted/re-created,

time thread is still running but it will not be able to change the screen UI dynamically and fragment page is reset.

If you can make a timer by creating an asyncTask thread, the problem is that the time is not updated after user leave the screen and come back.

How can you create an Android app that works out of the way in any way?

Knecht
  • 1
  • 2

2 Answers2

0

First, you have to create a broadcast receiver that will fired when alarm time changes and phone boots.

AndroidManifest.xml

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <receiver android:name="AlarmBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        </intent-filter>
    </receiver>
    <receiver android:name="AlertBroadcastReceiver"/>

This broadcast receiver will fire a service for alarm time calculation and will handle actions when an alarm time completes.

public class AlarmBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent serviceIntent = new Intent(context, AlarmService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            context.startForegroundService(serviceIntent);
        else
            context.startService(serviceIntent);
    }
}

In the service class. You should calculate alarm time in milliseconds and set it with Alarm Manager Class. Also you should set a PendingIntent for start an action when alarm time comes.

Intent startIntent = new Intent(getBaseContext(), AlertBroadcastReceiver.class);
startIntent.putExtra("alarm", alarmContent);
startIntent.putExtra("type", "start");

PendingIntent pendingIntentStart = PendingIntent.getBroadcast(getBaseContext(), 0, startIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManagerStart = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    alarmManagerStart.set(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
}
else if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT  && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    alarmManagerStart.setExact(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if(getAlarmTime(alarmArrayNd, "start").getTimeInMillis() >= 900000)
        alarmManagerStart.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
    else
        alarmManagerStart.setExact(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
}

In this example, the method getAlarmtime() is a method for calculating alarm time in milliseconds that i created also extras in intent is arbitrary, you can put whatever you want it in.

Yusuf Çağlar
  • 320
  • 2
  • 13
0

thankyou very much i solve this problem by not destroying fragment object. i think getting alarmmanger event make a lot of overhead. so this is not best approach but i edited my source by not destroying fragment object.

Knecht
  • 1
  • 2