Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Google calendar or Instagram make their apps all the time up and running even after force stop

Looks like force stop should prevent app from running and it's even disable all app's alarms. However I found that notification in Google Calendar still shown fine even after force stop, and I all the time see Instagram app running, even when I kill it, it's just restart automatically and it's once again there.

So, what is a way to make app running constantly? I'm doing app with reminders and need to show notifications in specific time, regardless how app was previously closed before.

like image 869
silent_coder Avatar asked Apr 05 '17 11:04

silent_coder


People also ask

How does Google Calendar manage time?

Set Your Working Hours in Google CalendarIn your calendar, click the gear icon “Settings menu” Scroll down to “Working Hours” Click on the day(s) of the week that you work. Set the working hours for each work day.

What is Google Calendar and how does it work?

Google Calendar takes the pain out of scheduling meetings, events, and other activities so you can focus your headspace on the things that matter. With clever but easy-to-use features, Google Calendar will help you keep track and manage your schedules better. Jimmy Rodela is a small business expert writing for The Ascent and The Motley Fool.

How to use the Google Calendar to schedule a meeting?

You can use the Google Calendar in many ways as a scheduling tool. In this Google Calendar tutorial, we’ll cover how to use the scheduling app to schedule a meeting with your clients. 1. Create an event To create an event, click the “Create” button on the upper left hand of the page or click on the online calendar.

How do I schedule an Instagram post on my calendar?

Click the Create button in the top-right corner of your screen or the + symbol on the calendar day you want to schedule. Choose your Instagram profile in the top-left of the message editor. Add your picture or video, then fill in your text. (Optional) Use the built-in Social Message Optimizer to refine your text. Schedule your post time.

How to make your Instagram strategy easier to execute?

You’ll also get a free template to help you follow the process. Make your Instagram strategy easier to execute with a detailed content calendar that outlines the who, what, where, and when of your posts. Before you read how to plan your Instagram content calendar, take this template with you.


Video Answer


3 Answers

If you start a service in Application Class than your service will be always running even though if a user terminates or force stop from task manager, it will run again.

To create service specifically in Android studio Right click on app from Project Explorer and then New > Service > Service

Create a service:

public class ServiceName extends Service {

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
      // do your jobs here
      return super.onStartCommand(intent, flags, startId);
        }
    } 

Now create an Application Class and start Service in Application Class

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        startService(new Intent(this, ServiceName.class));
    }
}  
like image 152
Sagar Damani Avatar answered Oct 17 '22 14:10

Sagar Damani


I see two options there.

First one is to handle any exception via Thread.setDefaultUncaughtExceptionHandler() that is shipping with Java, it's not from Android SDK.

To be concise, you make your custom Application class implement UncaughtExceptionHandler and register it as a listener to any exception in the application. As soon as some crash happens this callback will be fired from where you can delay a job to happen in near future (e.g. spawn AlarmManager to launch the app again in 50ms).


The second option is that you can launch your components in different processes. From docs:

android:process

The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The element's process attribute can set a different default for all components. But component can override the default with its own process attribute, allowing you to spread your application across multiple processes.

A crash that happens on different process won't make the process that hosts your UI to crash.

like image 24
azizbekian Avatar answered Oct 17 '22 14:10

azizbekian


Well for starters they have a job scheduler system that schedules jobs to executed, these jobs can be stopped , started or resumed. They implement a mechanism to detect crash, think java-script where if a application crashes it can be restarted (nodemon or forever) , in android there are services, which can be started or resume, There is this special service behavior to restart crashed services.

START_REDELIVER_INTENT- tells the system to restart the service after the crash and also redeliver the intents that were present at the time of crash.

The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the JobScheduler class. This API allows to batch jobs when the device has more resources available. In general this API can be used to schedule everything that is not time critical for the user.

You can also mix Alarms, broadcast receivers and threading coupled with reactive programming to perform the job.Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.

You can attach observables to certain tasks performing specific operations, You can implement asynchronous i/o, computational operations, or even “infinite” streams of data by designing your own Observable.

like image 1
Remario Avatar answered Oct 17 '22 15:10

Remario