1

I'd like to make an notification which start to count time when user exited android application. If user do not executed application after 1hours, It notified me to execute and If user ignoring it, It executes saved SMS messages. I found some examples on timer, but I do not know how to find application exit time. Please give me some advice with full code. I am desperately need it...

TimerTask task = new TimerTask(){
    public void run() {
        try {
            mainTime++;
            int min = mainTime / 60;
            int sec = mainTime % 60;
            String strTime = String.format("%s : %s", min, sec);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

Timer mTimer = new Timer();
mTimer.schedule(task, 0, 60000);
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("Chack your app", smsBody);                 
sendIntent.putExtra("12345678", phonenumber);               
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Danielson
  • 2,605
  • 2
  • 28
  • 51
writer
  • 11
  • 2

2 Answers2

1

Okay so what you need to do is to store the system time locally (may be using SharedPreferences) when the application exits. You can register a BroadcastReceiver which will help you trigger some action when 1hr or a certain time has passed from the locally stored time when app exited.

If you want to know how to handle programmatically when and how to exit the app , please refer this answer.

Community
  • 1
  • 1
Sash_KP
  • 5,551
  • 2
  • 25
  • 34
1

You could also try to use the Android alarm system. Once the user exit your application, you could set up an Alarm. Something like:

YourActivityOrFragment.java

@Override
protected void onStop() {
   Calendar c = Calendar.getInstance();
            c.setTimeInMillis(System.currentTimeMillis());
            c.add(Calendar.HOUR,1);
            scheduleAlarm(c.getTimeInMillis());
}

private void scheduleAlarm(long time) {
   Intent yourIntent = new Intent("Some_ID");

   PendingIntent pi = PendingIntent.getBroadcast(YourClass.this, ALARM_ID, yourIntent, PendingIntent.FLAG_CANCEL_CURRENT);

// Put some extras here, if you need so. Like:
// yourIntent.putExtra("field","value");

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

am.set(AlarmManager.RTC_WAKEUP,time,pi);
}

Now, create a BroadcastReceiver to handle those alarms.

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

private static final String LOG_TAG = AlarmReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {

    Log.d(LOG_TAG, "Alarm fired!");

    Intent it = new Intent(context, YourNotificationHandler.class);

    // Get your Extras here. And do whatever you want, if you need.

    // For what you said, there's no need to start an Activity, so let's handle that alarm as a service.

    context.startService(it);

   // But if for some reason you want to start an Activity, just do it like: 
   // context.startActivity(it);
}

}

On your AndroidManifest.xml declare your BroadcastReceiver.

<receiver android:name=".AlarmReceiver" >
    <intent-filter>
        <action android:name="Some_ID" />

        <category android:name="android.intent.category.default" />
    </intent-filter>
</receiver>

And last of all, create your service to handle your notifications, you could try something like an IntentService. On that file, you'll have a onHandleIntent(Intent intent) method. Get your Intent there, and it's Extras, and do whatever you want to do. Later, just call your Notifications. I've used a utility class on my projects to handle those, but feel free to choose how you'll do that.

Example:

public static void createService(Context context, CharSequence tickerMessage, CharSequence title,
                                     CharSequence message, int icon, int id, Intent intent, long[] pattern, Boolean autoCancel) {

    PendingIntent p = PendingIntent.getService(context, 0, intent, 0);
    Notification n;
    int apiLevel = Build.VERSION.SDK_INT;

    if (apiLevel >= 11) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setTicker(tickerMessage)
                .setContentText(message)
                .setSmallIcon(icon)
                .setContentIntent(p)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        if (pattern.length > 0) {
            builder.setVibrate(pattern);
        }

        if (autoCancel != null) {
            builder.setAutoCancel(autoCancel);
        }

        if (apiLevel >= 17) {
            // Android 4.2+
            n = builder.build();
        }
        else {
            // Android 3.x
            n = builder.getNotification();
        }
    }
    else {
        // Android 2.2+
        n = new Notification(icon, tickerMessage, System.currentTimeMillis());
        // Data
        n.setLatestEventInfo(context, title, message, p);
    }

    NotificationManager nm = (NotificationManager)
            context.getSystemService(Activity.NOTIFICATION_SERVICE);

    nm.notify(id, n);
}

You can read more about alarms here. More on Service here. BroadcastReceiver here. Notifications, here and here. And this might be an interesting read about Notification as well.

Mauker
  • 11,237
  • 7
  • 58
  • 76