在应用被杀之前设置闹钟?

时间:2017-08-08 08:53:57

标签: android android-service alarmmanager

到目前为止,我已尝试在app-start上启动服务,并在应用程序终止onTaskRemoved调用并且应安排警报时,在onTaskRemoved方法中设置警报设置代码。被解雇但在我的情况下没有发生。

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();
}

Mainfest中的接收者

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

接收方onReceive代码

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);
    } 

0 个答案:

没有答案