0

i have an app and it's important for me to detect time changes. so far i have receiver with proper intent filters like this:

<receiver
  android:name=".MyReceiver"
  android:enabled="true"
  android:exported="true">
  <intent-filter>
    <action android:name="android.intent.action.TIME_SET" />
    <action android:name="android.intent.action.ACTION_TIME_CHANGED" />
    <action android:name="android.intent.action.DATE_CHANGED" />
  </intent-filter>
</receiver>

and this is my receiver :

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences.Editor spe =PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).edit();
        spe.putLong("UFCount", 1));
        spe.putLong("FCount", 1));
        spe.commit();
        Toast.makeText(context.getApplicationContext(), "Your job is being done!!", Toast.LENGTH_LONG).show();
    }
}

this receiver only works when the app is open\in-the-background . how can i make this receiver to work even if the app is closed?

Krishna
  • 343
  • 5
  • 22
hadi
  • 1,104
  • 8
  • 23
  • You can check this link [http://stackoverflow.com/a/16824692/4049612](http://stackoverflow.com/a/16824692/4049612) – Krishna May 06 '16 at 06:40
  • @Krishna i don't feel good about having a service running all the time . i don't think its a optimal way of doing this – hadi May 06 '16 at 06:44

1 Answers1

0

Since the application is closed, there is no UI (i.e. no Activity) to display the Toast. At this point, there should be an exception in Logcat. You might also add a logcat trace before displaying the Toast, like Log.d("MyReceiver", "in onCreate()"); and you should see it even when the app is closed.

Instead of displaying a Toast, you could for instance send a local notification, which is allowed because it does not require an Activity from your app.

Lolo
  • 4,277
  • 2
  • 25
  • 24
  • I did use the Log but it doesn't work. i searched for the Log and it doesn't show up hence the receiver didn't triggered – hadi May 06 '16 at 07:18
  • If you're looking at logcat from Android Studio, can you try and disable the logcat filters and see what you get? AS has sometimes trouble getting traces from closed applications – Lolo May 06 '16 at 07:25
  • yes i did that too. still nothing shows up. i wonder if `android.intent.action.TIME_SET` action has the flag to open closed app component ?! – hadi May 06 '16 at 07:30