1

So far i have tried to start a service on app-start and have Alarm Setup code in onTaskRemoved Method when app killed the onTaskRemoved invokes and the alarm should scheduled & Fired but its not happening in my case.

public void onTaskRemoved(Intent rootIntent)
{
    super.onTaskRemoved(rootIntent);

    Log.e("TAG","WE ARE HERE..");
    Intent intent = new Intent(this, uploadData.class);

    PendingIntent Pintent = PendingIntent.getBroadcast(this.getApplicationContext()
            , 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

    c.set(Calendar.HOUR_OF_DAY, 12);
    c.set(Calendar.MINUTE, 52);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.AM_PM, Calendar.PM);

    alarmManager.setRepeating(AlarmManager.RTC, c.getTimeInMillis(),AlarmManager.INTERVAL_DAY, Pintent);
    stopSelf();
}

Receiver in Mainfest

  <receiver android:name="uploadData"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"    />
        </intent-filter>
    </receiver>

Receiver onReceive code

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent i = new Intent(context, uploadData.class);

        PendingIntent Pintent = PendingIntent.getBroadcast(context
                , 234324243, i, 0);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        c.set(Calendar.HOUR_OF_DAY, 2);
        c.set(Calendar.MINUTE, 05);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.AM_PM, Calendar.AM);

        alarmManager.setRepeating(AlarmManager.RTC, c.getTimeInMillis(),AlarmManager.INTERVAL_DAY, Pintent);

    }else {
        /*FirebaseJobDispatcher base = new FirebaseJobDispatcher(new GooglePlayDriver(context));
        Log.i("ALARM", "I M RUNNING");
        Job myJob = base.newJobBuilder()
                .setRecurring(false)
                .setTag("upload-task")
                .setLifetime(Lifetime.FOREVER)
                .setTrigger(Trigger.NOW)
                .setConstraints(Constraint.ON_ANY_NETWORK)
                .setService(FireBaseJobDispatcher.class)
                .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
                .build();

        base.mustSchedule(myJob);*/

        Log.i("ALARM", "I M RUNNING");
        GcmNetworkManager gcm = GcmNetworkManager.getInstance(context);

        OneoffTask pT = new OneoffTask.Builder()
                .setPersisted(true)
                .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
                .setService(ServerUpdateJob.class)
                .setExecutionWindow(0,60)
                .setTag("DATA_SUBMISSION")
                .build();

        gcm.schedule(pT);
    } 
Kunal Awasthi
  • 310
  • 2
  • 14
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/151377/discussion-on-question-by-kunal-awasthi-set-an-alarm-before-app-is-killed). – Bhargav Rao Aug 08 '17 at 11:32

0 Answers0