Android - Start same App via Intent and AlarmManager
-
Ahoí there,
Goal:
The overall goal is to create an alarmclock application which amongst others is supposed to have the ability to schedule an alarm and can be started by the OS (Android) at a scheduled time, if the app was closed. (e.g. with Intent and AlarmManager)Former search:
on a research I found alarmclock examples which go the approach to let an timer expire ecery 1000ms, get the systemtime, compare it to a defined value and on >= do something.
This felt unelegant to me and I was sure, Android provides mechanisms which can be utilized to do that in a more native fashion.Approach:
Defining an Android-Intent, which carrys the name of the same App, which get´s put to the AlarmManager.As a startingpoint I used the Notification example and tweaked it with what I found here under "fire the alarm at precisely 8:30 a.m" (source for completeness)
https://developer.android.com/training/scheduling/alarms#java
In my .java code in the QtProject there is a line which builds the Intent and gets the context and the name(?) of the App to start on alarm.
Intent intent = new Intent(context, AlarmNotification.class);My (current) issue:
I get the error "AlarmNotification" is an unkown symbol.
I am not experienced in Java and only understand what I see in a principle fashion.
From something I found in the internet, I derived it is because in the Java file is a package defined and that class "AlarmNotification" isn´t part of that package.
:) Of course it´s not, since it´s my App itselfHow would one solve this conceptual flaw?
best regards
FMthe .java
/**************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ package org.qtproject.example.androidnotifier; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.BitmapFactory; import android.app.NotificationChannel; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.icu.util.Calendar; public class NotificationClient { private static AlarmManager alarmMgr; private static PendingIntent alarmIntent; public static void notify(Context context) { alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmNotification.class); alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); // Set the alarm to start at 8:30 a.m. Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 12); calendar.set(Calendar.MINUTE, 38); // setRepeating() lets you specify a precise custom interval--in this case, // 20 minutes. alarmMgr.setExact( AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent); } }
-
In the end this was the first thing I solved with ChatGPT 3-5
I stated following question
assuming following situation: a qt application named "AlarmClock" is supposed to run on Android with API level 28 and via JNI can call a method "Setter" in a java class named "AlarmSetter". The goal is, that the method schedules an alarm via the AlarmManager of Andoird OS and when the alarm triggers, the application "AlarmClock" is restarted. How has the method "Setter" to be written, which has the purpose to restart the application "AlarmClock" at a given time?
and this was the answer,
import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; public class AlarmSetter { public static void Setter(Context context, long triggerTimeInMillis) { // Create an intent to restart the "AlarmClock" application Intent restartIntent = context.getPackageManager() .getLaunchIntentForPackage("com.example.alarmclock"); // Replace with the actual package name of your AlarmClock app // Create a PendingIntent for the restart intent PendingIntent restartPendingIntent = PendingIntent.getActivity( context, 0, restartIntent, PendingIntent.FLAG_CANCEL_CURRENT); // Get the AlarmManager AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); // Schedule the alarm to trigger at the specified time if (alarmManager != null) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTimeInMillis, restartPendingIntent); } } }
which I adapted to my actual used .java file (for the purpose of just trying I use a hard coded time I have to adapt)
/**************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ package org.qtproject.example.androidnotifier; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.BitmapFactory; import android.app.NotificationChannel; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.content.ComponentName; import android.icu.util.Calendar; import android.content.pm.PackageManager; public class NotificationClient { private static AlarmManager alarmMgr; private static PendingIntent alarmIntent; public static void notify(Context context) { alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); //Intent intent = new Intent(); //intent.setComponent(new ComponentName("org.qtproject.example.androidnotifier", "AlarmNotification")); Intent intent = context.getPackageManager().getLaunchIntentForPackage("org.qtproject.example.androidnotifier"); System.out.println("Testprint"); if(intent == null) System.out.println("Wrong wrong wrong wrong"); alarmIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 19); calendar.set(Calendar.MINUTE, 3); alarmMgr.setExact( AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent); /* */ } }
-
Hi @Flaming-Moe,
The code I see on https://developer.android.com/training/scheduling/alarms seems slightly different and use
AlarmReceiver.class
for the intent. May it be that ?private AlarmManager alarmMgr; private PendingIntent alarmIntent; ... alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmReceiver.class); alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); // Set the alarm to start at 8:30 a.m. Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 8); calendar.set(Calendar.MINUTE, 30); // setRepeating() lets you specify a precise custom interval--in this case, // 20 minutes. alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 20, alarmIntent);
btw this not related to Qt
-
In the end this was the first thing I solved with ChatGPT 3-5
I stated following question
assuming following situation: a qt application named "AlarmClock" is supposed to run on Android with API level 28 and via JNI can call a method "Setter" in a java class named "AlarmSetter". The goal is, that the method schedules an alarm via the AlarmManager of Andoird OS and when the alarm triggers, the application "AlarmClock" is restarted. How has the method "Setter" to be written, which has the purpose to restart the application "AlarmClock" at a given time?
and this was the answer,
import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; public class AlarmSetter { public static void Setter(Context context, long triggerTimeInMillis) { // Create an intent to restart the "AlarmClock" application Intent restartIntent = context.getPackageManager() .getLaunchIntentForPackage("com.example.alarmclock"); // Replace with the actual package name of your AlarmClock app // Create a PendingIntent for the restart intent PendingIntent restartPendingIntent = PendingIntent.getActivity( context, 0, restartIntent, PendingIntent.FLAG_CANCEL_CURRENT); // Get the AlarmManager AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); // Schedule the alarm to trigger at the specified time if (alarmManager != null) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTimeInMillis, restartPendingIntent); } } }
which I adapted to my actual used .java file (for the purpose of just trying I use a hard coded time I have to adapt)
/**************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ package org.qtproject.example.androidnotifier; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.BitmapFactory; import android.app.NotificationChannel; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.content.ComponentName; import android.icu.util.Calendar; import android.content.pm.PackageManager; public class NotificationClient { private static AlarmManager alarmMgr; private static PendingIntent alarmIntent; public static void notify(Context context) { alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); //Intent intent = new Intent(); //intent.setComponent(new ComponentName("org.qtproject.example.androidnotifier", "AlarmNotification")); Intent intent = context.getPackageManager().getLaunchIntentForPackage("org.qtproject.example.androidnotifier"); System.out.println("Testprint"); if(intent == null) System.out.println("Wrong wrong wrong wrong"); alarmIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 19); calendar.set(Calendar.MINUTE, 3); alarmMgr.setExact( AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent); /* */ } }
-
F Flaming Moe has marked this topic as solved on