0

How can I call a method while my application is closed on Android?

I've tried: Onpause(), OnDestroy(), and OnStop(). With no luck.

What I want is receiving something from the database and do my reaction based upon when the application is closed.

Jason
  • 2,278
  • 2
  • 17
  • 25

3 Answers3

0

I think you can use broadcast or service to do it.

mdtuyen
  • 4,470
  • 5
  • 28
  • 50
0

You could create a base activity (BaseActivity class) from which all your activities would have to be derived. Then inside BaseActivity.onCreate - you would increase some SharedPreferences counter, and inside BaseActivity.onDestroy you would decrease it. Now when that counter is equal to zero, you might assume your application is closed - but the process might still run in the background.

What I want is receiving something from the database

now I assume you already know how to "receive something from database". This could be background Service, where you could check SharedPreferences and do your processing.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • thank you for reply : yes Actually I have been receiving thing from the database, but I will try your idea for BaseActivity Thank you. – Yazeed Al-faqeeh Nov 23 '15 at 15:31
0

What is the trigger?

Part 1 : Intent - By example, you can set an alarm, that launch an Intent.

Part 2 : Broadcast Receiver - You must create a Broadcast Receiver (which reacts to an intent).

Part 3 : Service - The Broadcast Intent will launch a service that will execute when the application is not open.

compte14031879
  • 1,531
  • 14
  • 27
  • thank you for reply : Please can you give me more information about the Broadcast ? – Yazeed Al-faqeeh Nov 23 '15 at 15:58
  • An example : http://www.techotopia.com/index.php/Android_Broadcast_Intents_and_Broadcast_Receivers. Official documentation : http://developer.android.com/reference/android/content/BroadcastReceiver.html – compte14031879 Nov 23 '15 at 16:01
  • Intent and Broadcast Receiver are the basics of android, you can find a lot of documentation about it on => Google – compte14031879 Nov 23 '15 at 16:02
  • Thank you : Well I understand now, but Broadcast wont's happened to the activation of such receive a text message or change the settings in the phone. but in my case just i'll change value in my database any idea ? – Yazeed Al-faqeeh Nov 23 '15 at 16:13