0

example: user closed the app, and the app already set a date (28/04/2019) and time (8:00AM) when to send a notification to the user.

I tried to work with these code examples Send a notification when the app is closed https://guides.codepath.com/android/Starting-Background-Services#using-with-alarmmanager-for-periodic-tasks

what I already achieve is the notification which is working when inside the application.

If you can, try to explain it at an example, because beginners (like me) can easier learn it this way or can you guide me the step by step process to achieve what I need.

Mahdi Moqadasi
  • 2,029
  • 4
  • 26
  • 52
Ken Verganio
  • 532
  • 8
  • 21
  • `JobScheduler` would be better. You can also use `Background Service` and track time at every specified time interval. – Ajay Mehta Apr 27 '19 at 06:45

2 Answers2

0

I wrote a sample for you and i tested it, i hope this help you.

I think you have to change AlarmManager.RTC to AlarmManager.RTC_WAKEUP in your app .

Notice 1 : if your phone shutdown or restart you have to reset your alarm(s) again when the phone bootup, so you have to save your alarm(s) in shared preferences or etc and reset when phone bootup.

Notice 2: Also you need a broadcast receiver for know when the phone booted up.

Notice 3: AlarmManager works in all Android versions perfectly.

MainActivity

public class MainActivity extends AppCompatActivity {

    AlarmManager alarmManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alarmManager =(AlarmManager) getSystemService(ALARM_SERVICE);

        findViewById(R.id.btnSetAlarm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 4);
                calendar.set(Calendar.MINUTE, 33);
                calendar.set(Calendar.SECOND, 10);
                calendar.set(Calendar.AM_PM, Calendar.PM);

                Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 243619,intent,0 );
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);

                Toast.makeText(MainActivity.this, "Alarm set", Toast.LENGTH_LONG).show();
            }
        });
    }
}

AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
            if (alarmUri==null)
                alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            RingtoneManager.getRingtone(context, alarmUri).play();
            Toast.makeText(context, "Wake up!!! Wake up!!!", Toast.LENGTH_LONG).show();
        }catch (Exception ex){
            Log.d("AlarmReceiver", ex.getMessage());
        }
    }
}

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alarmmanager">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name=".AlarmReceiver"
            android:enabled="true"
            android:exported="true"></receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Feel free to ask any questions.

Peyman
  • 101
  • 1
  • 6
0

In your task, there are three approaches we can use

  1. Alarm Manager
  2. JobScheduler
  3. FireaseJobDispatcher

Alarm manager - It's an old and classic way to trigger particular tasks. But since it was created and introduced in KitKat it's not reliable in android OREO and PIE (I have tried it).

JobScheduler - This is an API for scheduling various types of jobs against the framework that will be executed in your application's own process. It works well in all android versions (Tested in O and P works for me!).

FireaseJobDispatcher - It was a deal until JobScheduler came now it's depreciated. Some others are also available like WorkManager, Evernote's AndroidJob and all (google it). But, JobScheduler here is a deal breaker I would recommend you to use JobScheduler.

  • 1
    Only Alarm Manager can be used for this task ...JobScheduler and FireaseJobDispatcher are preferred for a deferrable task only..Op wants to execute the at an exact time. – m3g4tr0n Apr 27 '19 at 05:33
  • Little bit of tweaking with this will work `JobInfo jobInfo = new JobInfo.Builder(123, name) .setPeriodic(TimeUnit.HOURS.toMillis(1)) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setPersisted(true) .build();`. – Mrityunjay Kumar Apr 28 '19 at 05:07