How does one launch android version dependent alarm clock? [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Intent to launch the clock application on android
I have a widget that displays the time and if one taps on it, it launches the com.android.alarmclock/.AlarmClock activity with an PendingIntent. This works great before-Froyo, but with Froyo, I have to launch the com.android.deskclock/.AlarmClock. So I want to put in code that checks for the class existence and launch the appropriate activity/intent. Here is what I tried, but it does not work.
Intent alarmIntent = new Intent();
try {
if (Class.forName("com.android.deskclock.AlarmClock") != null) {
Log.i(TAG, "setting deskclock alarm -- must be Froyo!");
alarmIntent.setClassName("com.android.deskclock",
"com.android.deskclock.AlarmClock");
}
} catch (ClassNotFoundException e) {
Log.i(TAG, "setting alarmclock alarm -- must be Eclair!");
alarmIntent.setClassName("com.android.alarmclock",
"com.android.alarmclock.AlarmClock");
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, REQUEST_UPDATE_TIME_NOW,
alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.text_timeofday, pendingIntent);
It always thinks it is "Eclair" and therefore fails on Froyo. Is this the best approach, or should I check the application-level? I prefer to work with the class existence.

if (Class.forName("com.android.deskclock.AlarmClock") != null)
That won't work, because that class is not in your project. At most, it might be in some other project on the device.
There is no documented supported Intent for launching an alarm clock in the Android SDK. Your approach of hard-wiring in package and class names is fragile, as you're discovering. It won't work on some devices, if they do not have that application (e.g., replaced by one from the device manufacturer). Plus, as you've seen, it may change in future versions of Android. I am having a rough enough time trying to convince device manufacturers not to break the SDK; having third-party developers do it weakens my case.
That being said, the general way to see if something will respond to an Intent is to use PackageManager (e.g., queryIntentActivities()).

Related

Xiaomi Android 11 SecurityException Too many PendingIntent created

Our application has been receiving this new kind of crash since the beginning of the year.
Firebase shows it repeats only on Xiaomi with Android 11. It repeats in different parts of the application. And it seems unrelated to the application code. So I assume it is related to Xiaomi Android 11 update with broken code or new restrictions without clear explanation.
The obvious idea is pending intent creation spam, but I can't find any evidence that this method is frequently called, only several times.
Did anyone come out with a hack for that?
Suppressing throwable is not a good option since the functionality won't work.
Code snippet:
val intent = Intent(context, SomeClass::class.java).apply {
this.action = action
this.putExtra(EXTRA_IDENTITY, identity)
}
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
Stacktrace:
Fatal Exception: java.lang.SecurityException: Too many PendingIntent created for uid 10273, aborting Key{broadcastIntent pkg=<package> intent=act=<action name> cmp=<class> flags=0x0 u=0} requestCode=0
at android.os.Parcel.createExceptionOrNull(Parcel.java:2376)
at android.os.Parcel.createException(Parcel.java:2360)
at android.os.Parcel.readException(Parcel.java:2343)
at android.os.Parcel.readException(Parcel.java:2285)
at android.app.IActivityManager$Stub$Proxy.getIntentSenderWithFeature(IActivityManager.java:6898)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:578)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:561)
...

Periodic Work manager not showing notification on android pie when not charging or screen off

I have an app that should show a notification every 2 hours and should stop if user has already acted upon the notif. Since background services are history now, I thought of using WorkManager ("android.arch.work:work-runtime:1.0.0-beta01") for the same.
My problem is that although the work manager is successfully showing the notifications when app is running, but it won't show notification consistently in the following cases(I reduced the time span from 2 hours to 2 minutes to check the consistency):
when app is killed from the background.
device is in screen off.
state device is in unplugged state(i.e not charging).
By consistency , i mean that the notifications show at least once in the given time span. for 2 minutes time span, the freq of notifications went from once every 4 minutes to completely not show any notification at all. for 2 hours timespan( the timespan that i actually want), its been 4 hours and i haven't got a single notification. Here is the Code i am using for calling WorkManger:
public class CurrentStreakActivity extends AppCompatActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
...
setDailyNotifier();
...
}
private void setDailyNotifier() {
Constraints.Builder constraintsBuilder = new Constraints.Builder();
constraintsBuilder.setRequiresBatteryNotLow(false);
constraintsBuilder.setRequiredNetworkType(NetworkType.NOT_REQUIRED);
constraintsBuilder.setRequiresCharging(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
constraintsBuilder.setRequiresDeviceIdle(false);
}
Constraints constraints =constraintsBuilder.build();
PeriodicWorkRequest.Builder builder = new PeriodicWorkRequest
.Builder(PeriodicNotifyWorker.class, 2, TimeUnit.HOURS);
builder.setConstraints(constraints);
WorkRequest request = builder.build();
WorkManager.getInstance().enqueue(request);
}
....
}
Here is the worker class(i can post showNotif(..) and setNotificationChannel(...) too if they might be erroronous):
public class PeriodicNotifyWorker extends Worker {
private static final String TAG = "PeriodicNotifyWorker";
public PeriodicNotifyWorker(#NonNull Context context, #NonNull WorkerParameters workerParams) {
super(context, workerParams);
Log.e(TAG, "PeriodicNotifyWorker: constructor called" );
}
#NonNull
#Override
public Result doWork() {
// Log.e(TAG, "doWork: called" );
SharedPreferences sp =
getApplicationContext().getSharedPreferences(Statics.SP_FILENAME, Context.MODE_PRIVATE);
String lastcheckin = sp.getString(Statics.LAST_CHECKIN_DATE_str, Statics.getToday());
// Log.e(TAG, "doWork: checking shared preferences for last checkin:"+lastcheckin );
if (Statics.compareDateStrings(lastcheckin, Statics.getToday()) == -1) {
Log.e(TAG, "doWork: last checkin is smaller than today's date, so calling creating notification" );
return createNotificationWithButtons(sp);
}
else {
Log.e(TAG, "doWork: last checkin is bigger than today's date, so no need for notif" );
return Result.success();
}
}
private Result createNotificationWithButtons(SharedPreferences sp) {
NotificationManager manager =
(NotificationManager) getApplicationContext().getSystemService((NOTIFICATION_SERVICE));
String channel_ID = "100DaysOfCode_ID";
if (manager != null) {
setNotificationChannel(manager,channel_ID);
showNotif(manager, channel_ID, sp);
return Result.success();
}
else {
return Result.failure();
}
I am using a xiaomi miA2 androidOne device with Android Pie(SDK 28). There are a few other things that are troubling me:
What can i possibly do to know if my WorkManager is running? Other that just wait for 2 hours and hope for a notification. I actually tried something like that, keeping my phone connected to pc and checking android studio's logcat every now and then. It DOES run all the logs when the worker is actually called, but i don't think that's a correct way to test it, or is it?
In the above Code, the setDailyNotifier() is called from the onCreate() every time the app is opened. Isn't it Wrong? shouldn't there be some unique id for every WorkRequest and a check function like WorkManger.isRequestRunning(request.getID) which could let us check if a worker is already on the given task??If this was a case of AsyncTask, then boy we would have a mess.
I have also checked #commonsware's answer here about wakelock when screen is off, but i remember that work manager does use alarm manager in the inside when available. So what am I missing here?
Few comments:
WorkManager has a minimum periodic interval of 15minutes and does not guarantee to execute your task at a precise time. You can read more about this on this blog.
All the usual background limitation you've on newer Android releases are still relevant when you use WorkManager to schedule your tasks. WorkManager guarantees that the task are executed even if the app is killed or the device is restated, but it cannot guarantee the exact execution.
There's one note about the tasks being rescheduled when your app is killed. Some OEM have done modification to the OS and the Launcher app that prevents WorkManager to be able to accomplish these functionality.
Here's the issuetracker discussion:
Yes, it's true even when the phone is a Chinese phone.
The only issue that we have come across is the case where some Chinese OEMs treat swipe to dismiss from Recents as a force stop. When that happens, WorkManager will reschedule all pending jobs, next time the app starts up. Given that this is a CDD violation, there is not much more that WorkManager can do given its a client library.
To add to this, if a device manufacturer has decided to modify stock Android to force-stop the app, WorkManager will stop working (as will JobScheduler, alarms, broadcast receivers, etc.). There is no way to work around this. Some device manufacturers do this, unfortunately, so in those cases WorkManager will stop working until the next time the app is launched.
As of now , i have this app installed for last 8 days and i can confirm that the code is correct and app is working fine. as said by pfmaggi , the minimum time interval for work manager to schedule the work is 15 minutes, so there is a less chance that the WorkManager would have worked as expected in my testing conditions( of 2 minutes ) . Here are some of my other observations:
Like I said in the question that i was unable to recieve a notification for 4 hours even though i have passed the repeat interval as 2 hours. This was because of Flex Time. I passed in the flex time of 15 minutes and now it shows notifications between correct time interval. so i will be marking pfmaggi's answer as correct.
The problem of repeated work request can be solved by replacing WorkManager.getInstance().enqueue(request) with WorkManager.getInstance().enqueueUniqueWork(request,..)
I was still unable to find a way to test the work manager in the way i have described.

How to detect process foreground for Android O

On our application there's a service that is normally started during Application.OnCreate (directly calling context.startService) and also later on via AlarmManager (refactor is in progress to migrate some of its work to JobScheduler).
Our application also have a BroadcastReceiver that gets launched with its direct intent.
Given the new limitations in Android Oreo (https://developer.android.com/about/versions/oreo/android-8.0-changes.html) we're having an issue as follows:
app/process is in background/dead
BroadcastReceiver gets fired by the OS
Application.onCreate() executes before the BroadcastReceiver
Application.onCreate() code tries to run the Service
this leads to crash with "IllegalStateException: Not allowed to start service Intent".
I'm aware of the new recommended ways of launching a Service as answered by CommonsWare here https://stackoverflow.com/a/44505719/906362, but for this specific case, I simply want to have if(process in foreground) { startService }. I'm currently using the following method and it seems to work:
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isProcessInForeground_V21(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.AppTask> tasks = am.getAppTasks();
return tasks.size() > 0;
}
But I can't find the exact checks Android Oreo is doing (I got as far as here https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/ContextImpl.java on the startServiceCommon method, but from there requireForeground flag seems to go to some native implementation)
So my question:
For the specific purpose of Android Oreo new limitations, how to check if my process is foreground before calling startService?
To continue your investigation: (TL;DR: see between horizontal lines at the bottom)
Disclaimer, I don't know too much about Android, I just like digging in the source code.
Note: you can also navigate the code in Android Studio if you jump to file instead of class:
or searching for text in Project and Libraries.
IActivityManager is defined by AIDL, that's why there are no sources for it:
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/app/IActivityManager.aidl#145
Based on how AIDL needs to be implemented I found that ActivityManagerService extends IActivityManager.Stub (God bless Google indexing).
Note I also found this, which might be an interesting read if you're really interested how things work internally.
https://programmer.group/android-9.0-source-app-startup-process.html
ActivityManagerService sources reveal that in Oreo startService is forwarded to ActiveServices which is located in the same package.
Assuming we're looking for an exception like this:
java.lang.IllegalStateException: Not allowed to start service Intent {...}: app is in background uid UidRecord{af72e61 u0a229 CAC bg:+3m52s273ms idle procs:1 seq(0,0,0)}
we have to continue down the rabbit hole: requireForeground gets assigned to fgRequired parameter and the message is here. The condition to allow this depends on the start mode returned by ActivityManagerService.getAppStartModeLocked(packageTargetSdk = 26 or greater, disabledOnly = false, forcedStandby = false).
There are 4 start modes:
APP_START_MODE_NORMAL (needs to be different than this, i.e. !=)
APP_START_MODE_DELAYED (this is ok, i.e. return null)
APP_START_MODE_DELAYED_RIGID
APP_START_MODE_DISABLED
Ephemeral apps will immediately return APP_START_MODE_DISABLED, but assuming this is a normal app, we end up in appServicesRestrictedInBackgroundLocked.
Note: this is where some of the whitelist mentioned in https://stackoverflow.com/a/46445436/253468 is decided.
Since all branches but last return APP_START_MODE_NORMAL, this redirects to appRestrictedInBackgroundLocked where we find our most likely suspect:
int appRestrictedInBackgroundLocked(int uid, String packageName, int packageTargetSdk) {
// Apps that target O+ are always subject to background check
if (packageTargetSdk >= Build.VERSION_CODES.O) {
return ActivityManager.APP_START_MODE_DELAYED_RIGID;
}
So the reason for denial is simply targeting O. I think the final answer to your question of how the OS decides if your app is foreground or background is this condition in getAppStartModeLocked
UidRecord uidRec = mActiveUids.get(uid);
if (uidRec == null || alwaysRestrict || uidRec.idle) {
My guess is that a missing record means it's not running (but then how is it starting a service?!), and idle means it's backgrounded. Notice that in my exception message the UidRecord is saying that it's idle and has been backgrounded for 3m52s.
I peeked into your getAppTasks and it's based on TaskRecord.effectiveUid, so I'm guessing that's quite close to listing UidRecords for your app.
Not sure if this helps, but I'll post it anyway, so if anyone wants to investigate more, they have more info.

Delete all Stock Alarm clock alarms in Android

Is there a way to delete all stock alarm clock alarms in Android?
I am not talking about alarmservice intent, I am talking about the actual alarm clock. (Frustratingly both have the same name)
Here is the code I used to set the alarm :
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
int hour = 6;
int min = 0 ;
i.putExtra(AlarmClock.EXTRA_HOUR, hour);
i.putExtra(AlarmClock.EXTRA_MINUTES, min);
So, is there a way to remove/delete the alarm, through code?
(PS, I'd really appreciate suggestions as to how to improve my questions, rather than receiving a downvote for no reason at all. I'm finding it really hard to understand the Stackoverflow community, no offence)
After a lot of research, I figured out there is no way to do so with one click of a button. (Android security issues). However, there is one way- open the AppInfo page for the Stock Alarm clock on every phone. This is the code I used:
Intent i = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + packageName));
startActivity(i);
Where package name is the package name of the default clock apps.
Here is a list that I could find online:
String clockImpls[] = {
"com.android.deskclock.ALARM_ALERT",
"com.google.android.deskclock",
"com.android.deskclock.ALARM_DISMISS",
"com.htc.android.worldclock.WorldClockTabControl",
"com.sonyericsson.organizer.Organizer_WorldClock",
"com.htc.android.worldclock.AlarmAlert",
"com.htc.android.worldclock.TimerAlert",
"com.sec.android.app.clockpackage.ClockPackage",
"com.motorola.blur.alarmclock.AlarmClock",
"com.android.deskclock.ALARM_DONE",
"com.android.deskclock.ALARM_SNOOZE",
"com.android.deskclock.DeskClock",
"com.android.deskclock.AlarmClock",
"com.motorola.blur.alarmclock.AlarmAlert",
"com.motorola.blur.alarmclock.AlarmClock",
"com.motorola.blur.alarmclock.AlarmTimerAlert",
"com.android.alarmclock.AlarmClock",
"com.android.deskclock.DeskClock",
"com.sec.android.app.clockpackage.alarm.AlarmAlert",
"com.android.alarmclock.ALARM_ALERT",
"com.sec.android.app.clockpackage.ClockPackage",
"com.samsung.sec.android.clockpackage.alarm.ALARM_ALERT",
"com.htc.android.worldclock.ALARM_ALERT",
"com.sonyericsson.alarm.ALARM_ALERT",
"zte.com.cn.alarmclock.ALARM_ALERT",
"com.motorola.blur.alarmclock.ALARM_ALERT",
"com.mobitobi.android.gentlealarm.ALARM_INFO",
"com.urbandroid.sleep.alarmclock.ALARM_ALERT",
"com.splunchy.android.alarmclock.ALARM_ALERT"
};
PS:
Sadly, I haven't yet figured out a way to get questions answered from the community at StackOverflow. I'd really love people to help me out, in pointing where I'm going wrong.

Trouble with PowerManager.goToSleep()

I'm trying to put a device into sleep mode for a certain amount of time, say x, by calling..
powerManager.goToSleep(xNumberOfMilliseconds);
However, the api never seems to work consistently, and never for any amount of time greater than 1000 milliseconds. I'm stumped. I have the appropriate permissions, my application has its sharedUserId set to "android.uid.system" in the manifest, and the application is signed with the same key the firmware itself is signed with (platform key).
It is a pretty simple API call, so I don't really know what on earth is going wrong. I've been able to get this problem on both a device running android 2.3 and a device running android 3.2.
Any ideas?
I have done this but it works at random on several android 4.0.x plaforms.
powerManager.goToSleep(SystemClock.uptimeMillis() + timeMs)
Did anyone managed to use the method the way he has intended to?
Edit:
It seems the right answer was what figure in the code below:
public void sleepFor(long time, Context context) {
//Create a new PendingIntent, to wake-up at the specified time, and add it to the AlarmManager
Intent intent = new Intent(context, this.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent wakeupIntent = PendingIntent.getActivity(context, CODE_WAKE_UP_DEVICE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// CODE_WAKE_UP_DEVICE is a dummy request code.
AlarmManager am = getAlarmManager();
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + time, wakeupIntent);
powerService.goToSleep(SystemClock.uptimeMillis() + 1);
}
ContentResolver cr= getContentResolver();
android.provider.Settings.System.putInt(cr,android.provider.Settings.System.SCREEN_OFF_TIMEOUT
,1000);
According to the documentation, current system time would be more appropriate parameter for goToSleep() than the desired sleep duration:
powerManager.goToSleep(SystemClock.uptimeMillis())

Categories

Resources