I want to create an "alarm clock" -app which executes an alarm tone at a configurable time, also when the app is closed.
For android I found AlarmManager but it doesn't work if the app is closed.
Is this possible to achieve this functionality for iOS and Android?
You can fire a local notification at specific times to alert people do certain exercises. Every time you need to add an exercise, just add a local notification with specific time, and you can set the sound, content, isRepeat and etc.
For how to set a notification:
If you are using Xamarin.forms: you have to use dependency-service to implement the local-notifications in iOS and android project.
For example, in your xamarin.forms project:
public interface ISetLocalNotification
{
void noti(string time, string content...);
}
And implement in both iOS and Android project.
iOS: local-notifications-in-ios
Android: local-notifications-in-android
And when you want to use :
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
DependencyService.Get<ISetLocalNotification>().noti("12:00","123"...);
}
}
NOT THE SOLUTION:
There's several ways of doing this. It wasn't clear if you are using Xamarin Forms or not. But assuming you are using Xamarin forms, here's a great example with an official Xamarin video. But essentially, you are going to be running some code in the background while your app is not open. Just enable the permissions to do so, and check for time, so that the app plays the alarm when it is time.
If you are not using forms, you don't need to create the interface, and so it's much easier.
Related
I want to write a android native module which work as a background service and get current location and post to server and then integrate that module with react native.
if you just want it for the location then i suggest react-native-workers
it has access to native modules (network, geolocation, storage ...) you can aslo integrate it with react-native-queue
There are a couple of example projects / demo apps you might want to check out which implement both "Foreground Services" and "Background Services" in Android to help you get location updates while the app is closed or the screen is off. The background service example will only allow you to get updates as frequent as 1 minute, while the foreground service example will allow you to get updates as fast as 1 second (or maybe faster, I haven't tested that yet) while also displaying an "Ongoing Notification" to the user.
Background Service Example: https://github.com/comoser/rn-background-location
Foreground Service Example: https://github.com/andersryanc/ReactNative-LocationSample
There are a number of Android specific code adjustments you will need to make in either case. It's not ready yet, but in the near future I plan to update my repo's readme with a detailed set of instructions for implementing the necessary changes in your project.
I'm trying to use Android WorkManager in an application in order to schedule some background task. The requirement is to cancel the API request when the app is removed directly from app-tray.
I'm able to detect if app is getting removed from app-tray, by using Service class -> onTaskRemoved method. And here I'm making the WorkManager API cancel request.
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.d("onTaskRemoved","onTaskRemoved called");
WorkManager.getInstance().cancelAllWork();
}
This is working fine as expected. Please suggest if there is any other better alternative to achieve it.
For some android devices (for some vendors) they implemented like if any apps is removed from app tray means they will force stop the app.So the services,work manager are also stopped.
For some other android devices (for some
other vendors) they implemented like if any apps is removed from app tray means they will stop/kill only the current activity of the app & not force stop the app.So the services,work manager are not stopped.
First you need to check your target devices is comes in which category.
If you need to do this, I think you're probably using the wrong tool. WorkManager is specifically for requests that need to persist and execute at some point. Consider using an Executor or coroutine instead.
I'm developing an alarm application in which the alarm is set and then it is received by the Broadcast Receiver after that it will redirect to an activity.
I want to add a pattern lock at this activity. Once the correct combination of pattern is entered the phone will stop ringing.
How do i add this pattern lock in the activity.
Tried to implement the android-lockpattern library but doesn't know how to use it.
Simply add this pattern lock activity in front of your activity which starts after alarm is rung.
then if pattern matches then Do whatever you want to Do
refer to this project for pattern lock.
https://github.com/Joisar/LockScreenApp
In my case I caught the library which the link of demo app direct us. The library avaliable at site have problems. But the library found in link demo app link it's working. Using this library it's possible implement the guidelines site and compile without problems. See the options menu
Export source code
at the demo app to get the library.
I am writing a simple Phonegap application for Android. This program will send notification to notification bar and make the phone vibrate periodically.
I use navigator.notification.vibrate(time_period) to achieve the target. According to this article, both beep and vibration are not supported by android emulator. Hence, I was expecting that there could be entry indicating failure of it in the Catlog, but there is no such entry. The question is how to make sure that a vibration event has happened or failed (without deploying to a device).
AppHarbor looks like one of the ways to debug Phonegap application remotely. I wonder if there is other local ways to test Phonegap application as an HTML5 website in a Chrome browser (navigator.notification call is a standard call)? If yes, then it is probably possible to somehow parse the browser's console automatically to find out if the vibration event has happened.
Can you hide the vibrate() call behind an abstraction which you can replace depending on which platform you are using?
For example
var vibrateFunc = function(time_period) {
if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
console.log('vibrating for ' + time_period)
} else {
navigator.notification.vibrate(time_period)
}
}
and then have your app code call vibrateFunc() whenever it wants to vibrate.
This code will run an app automatically after booting the system, but the app will close after pressing the back button.
If the app is run normally by clicking it's icon. It will continuously run even after pressing the back button or running other apps.
public class AutoBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
My question is, how to make this auto run code to continuously run even after pressing the back button or running other apps?
You can probably start a Service here if you want your Application to run in Background. This is what Service in Android are used for - running in background and doing longtime operations.
UDPATE
You can use START_STICKY to make your Service running continuously.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
As apps run in the background anyway, I'm assuming what your really asking is how do you make apps do stuff in the background. The solution below will make your app do stuff in the background after opening the app and after the system has rebooted.
Below, I've added a link to a fully working example (in the form of an Android Studio Project).
This subject seems to be out of the scope of the Android docs, and there doesn't seem to be any one comprehensive doc on this. The information is spread across a few docs.
The following docs tell you indirectly how to do this:
https://developer.android.com/reference/android/app/Service.html
https://developer.android.com/reference/android/content/BroadcastReceiver.html
https://developer.android.com/guide/components/bound-services.html
In the interests of getting your usage requirements correct, the important part of this above doc to read carefully is: #Binder, #Messenger and the components link below:
https://developer.android.com/guide/components/aidl.html
Here is the link to a fully working example (in Android Studio format):
https://developersfound.com/BackgroundServiceDemo.zip
This project will start an Activity which binds to a service; implementing the AIDL.
This project is also useful to re-factor for the purpose of IPC across different apps.
This project is also developed to start automatically when Android restarts (provided the app has been run at least one after installation and app is not installed on SD card).
When this app/project runs after reboot, it dynamically uses a transparent view to make it look like no app has started but the service of the associated app starts cleanly.
This code is written in such a way that it's very easy to tweak to simulate a scheduled service.
This project is developed in accordance to the above docs, and is subsequently a clean solution.
There is, however, a part of this project which is not clean: I have not found a way to start a service on reboot without using an Activity. If anyone reading this post has a clean way to do this, please post a comment.
Starting an Activity is not the right approach for this behavior. Instead have your BroadcastReceiver use an intent to start a Service which can continue to run as long as possible. (See http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle)
See also Persistent service