Android: open activity when alarm clock is done or dismmised - android

Every alarm clock that the user set through the phone stock clock has an option of opening another application when the alarm is dismissed or done (I'm not sure if this feature is added in Marshmallow but I have it and I run android M).
The default for each alarm is "none" but you're able to pick the mail, weather, music applications etc...
I would like to add my application to this list so it'll open directly when the alarm is done.
What settings are needed for my application to show up at this list, and how can I set it as the default app for specific alarm (What extra should be specefied)
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm");
i.putExtra(AlarmClock.EXTRA_HOUR, 10);
i.putExtra(AlarmClock.EXTRA_MINUTES, 30);
startActivity(i);

So it seems that there's no option to add your app to the desired list which allow the user to pick an application to open immediately after the alarm has been dismissed or done.
My other solution is to listen to that specific event of dismissing or done through broadcast receiver. This might be a bit tricky because the event has multiple names according to the device.
I have LG phone so my event is com.lge.clock.alarmalert.stop, but you'll have to figure out whats the event for each device. I've seen some similar topics as this one.
Here's my manifest declaration for the receiver:
<receiver android:name=".Receiver.AlarmBroadcastReceiver">
<intent-filter>
<action android:name="com.android.deskclock.ALARM_DISMISS" />
<action android:name="com.android.deskclock.ALARM_DONE" />
<action android:name="com.lge.clock.alarmalert.stop" />
</intent-filter>
</receiver>
The deskclock is the default stock clock running on some devices, as I mentioned, I had to find the action for the LG phones and added it aswell.
The Receiver:
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.android.deskclock.ALARM_DISMISS") || action.equals("com.android.deskclock.ALARM_DONE") || action.equals("com.lge.clock.alarmalert.stop"))
{
////Do Something
}
}

Related

Intent filter when Android Auto started

Does there exist an intent filter which indicates when Android Auto starts? am building an app that start background thread, and I want connect to a device using Bluetooth to get remote control of head unit from custom hardware.
You can use ACTION_ENTER_CAR_MODE in a broadcast receiver to listen for when Android Auto starts and connects. Just keep in mind that ACTION_ENTER_CAR_MODE is not exclusive to Android Auto, it just means the OS is in car mode, which may or may not involve Android Auto.
Also, to satisfy the Android O requirements, you’ll need to make an explicit registration of the receiver by registering it in the activity. As a result of registering it in the activity it will not receive the broadcast on the very first connection to Auto, but only after the activity has been created and then on each connection afterwards.
<receiver
android:name=".CarModeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.app.action.ENTER_CAR_MODE"/>
<action android:name="android.app.action.EXIT_CAR_MODE"/>
</intent-filter>
</receiver>
Then in the implementation of the receiver...
public class CarModeReceiver extends BroadcastReceiver {
#Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
Log.d("CarModeReceiver", "Entered Car Mode");
} else if (UiModeManager.ACTION_EXIT_CAR_MODE.equals(action)) {
Log.d("CarModeReceiver", "Exited Car Mode");
}
}
}
It's also worth noting that from the documentation linked above...
In addition, the user may manually switch the system to car mode without physically being in a dock. While in car mode -- whether by manual action from the user or being physically placed in a dock -- a notification is displayed allowing the user to exit dock mode. Thus the dock mode represented here may be different than the current state of the underlying dock event broadcast.

Oreo: Alarm is not fired when Application is not running

I have relatively simple setup that should trigger an alarm at certain time of the day and show a notification to user. here is relative code,
Setting the alarm
long inTime = /*expirationTime*/ Calendar.getInstance().getTimeInMillis() + 10000;
Intent startIntent = new Intent("parking.event");
startIntent.setClass(getBaseContext(), ParkingExpirationWarmingBroadcast.class);
PendingIntent startPendingIntent = PendingIntent.getBroadcast(this, 99, startIntent, 0);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP,
inTime,
startPendingIntent);
BroadcastReceiver registered
<receiver
android:name=".modules.parking.ParkingExpirationWarmingBroadcast"
android:enabled="true">
<intent-filter>
<action android:name="parking.event" />
</intent-filter>
</receiver>
Broadcast Receiver
class ParkingExpirationWarmingBroadcast : BroadcastReceiver() {
#SuppressLint("NewApi")
override fun onReceive(context: Context, intent: Intent) {
}
}
The receiver is only getting triggered if app is in background. as soon as i swipe the app from multitasking, the notification is cleared and no new Alarms are triggered. I checked this setup on Android 7.0 and BroadcastReceiver is triggered regardless of app running or not.
I am aware regarding restrictions over implicit broadcasts in Android Oreo but i don't believe the intent that i have mentioned above is considered implicit.
Can anyone point out what i am doing wrong?
This is a general behavior of any Android's version. If you force-quit an application, then its Alarms and PendingIntents are deleted as well.
You can find the same answer here and here.
Force closing an app destroys its components . This is what force stop does. It's not a bug, it is very much a feature. Follow the following thread , it has been discussed by android framework engineers .
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/anUoem0qrxU

Android: is android.intent.action.DATE_CHANGED triggered at device reboot?

I made this broadcast receiver
public class DateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("DATE RECEIVER", "OK");
}
}
registered in manifest
<receiver
android:name=".DateReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
It works when device is on but if I turn it off and wait for midnight to pass, then I don't get any intent at reboot. How should I get it?
You won't get this broadcast because date has NOT changed (unless i.e. there's time update after the boot). It is different but has not changed in the way that justifies this broadcast. This may be confusing but in fact it does not matter what time stamp is when device starts. As device was started it does not know if that was because of restart or it was off for 5 weeks. Broadcast will be send if time is artificially changed i.e. due to network time sync, manual time change via preferences, timezone change. Normal ticking does not count. Initial time stamp does not matter.
If you need to know date on boot, you should listen to BOOT_COMPLETED.
You also need to remove android:exported="false" or set it to true as otherwise it is not reachable.

How to detect when a new alarm is set in Android

I would like to detect when a new alarm is set in Android default alarm application (I am using a Samsung device).
I tried: filter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
but it didn't work.
PS: My goal is not to detect whenever an alarm is ringing, but to use a broadcast receiver to monitor when a new alarm is created.
This is the code I am using:
IntentFilter filter = new IntentFilter("android.app.action.NEXT_ALARM_CLOCK_CHANGED");
filter.addAction("com.samsung.sec.android.clockpackage.alarm.SET_ALARM");
registerReceiver(mReceiver, filter);
From API Level-21 and above you can use below solution in your AndroidManifest.xml for the registered BroadcastReceiver.
<receiver
android:name=".YourAlarmReceiverClass"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.app.action.NEXT_ALARM_CLOCK_CHANGED">
</action>
</intent-filter>
</receiver>
ACTION_NEXT_ALARM_CLOCK_CHANGED is sent after the value returned by getNextAlarmClock() has changed, and your BroadcastReceiver's onReceive() will be called each time when user creates a new Alarm using Alarm clock application.
The intent AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED will be only triggered when the next alarm to ring is changed, not when any alarm is changed. As long as you are not allowed to get a list of all active alarms in your device... probably you won't be allowed either to get an intent when one alarm is added. Ther isn't any intent which does what you ask for.

Android. How to keep an application running after the system is going to kill it. Or how not to let the system to kill it

Android. How to keep an application running after the system is going to kill it?
Or how not to let the system to kill it?
I need to support the application in the foreground. That is, open the app every 30 seconds, regardless of whether it is minimized or closed.
While the application is running (it is displayed in the list of cached) method is called OnReceive. If the application is unloaded from memory, it no longer triggers Activity
use AlarmManager, BroadcastReceiver
Activity1.OnCreate
...
Intent intent = new Intent(this, typeof(MyAppReciever));
PendingIntent sender = PendingIntent.GetBroadcast(this, 0, intent,PendingIntentFlags.UpdateCurrent);
AlarmManager am = (AlarmManager)GetSystemService(AlarmService);
am.SetRepeating(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime()+30000,30000,sender);
BroadcastReceiver
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionPowerConnected })]
public class MyAppReciever : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent intent1 = new Intent(context, typeof(Activity1));
intent1.AddFlags(ActivityFlags.NewTask);
context.StartActivity(intent1);
}
}
manifest
....
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name="AndroidApplicationTest.MyAppReciever">
<intent-filter>
<action android:name="android.intent.action.POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
UPD #1
Let's simplify the example. For example , I want the application to be opened periodically ( shows the current Activity). Procedure is as follows: run the application by double clicking on the icon , then minimize by home-button. The application appears in the "working apps" (in the cache ) . Begin to actively use the phone , my application is gradually replaced and disappears from the "working apps" Say an hour after the first run , I want the application to reopen the (initial Activity). I hope that AlarmManager rise OnReceive event in which spelled Activity open the main application.
what am I doing wrong?
Problem with your Pending Intent. Use same action and category to match your Receiver intent filter to wake up your Receiver.
Ultimately you cannot stop android from killing your activity. Any memory crisis comes up, there is no guaranty that your application will be running.Least priority given to a services running in background for not get killed. But with your Service.start_sticky your tell android that woke run background service and memory becomes available. So the service is only one android component that can you keep running for your application that is also with no guaranty.

Categories

Resources