Delete all Stock Alarm clock alarms in Android - 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.

Related

Android Lollipop know if app as Usage Stats access

Since Android Lollipop, we have now an API for accessing apps usage stats. However, your app must be granted those permissions by the user.
I know that for redirecting the user to those settings using Settings.ACTION_USAGE_ACCESS_SETTINGS.
Now, my question is how do you know the user has granted you those permissions so that you can stop redirecting him to the settings.
Thanks!
you can simply query usagestats with daily interval and end time the current time and if nothing is returned this means the user hasn’t granted permissions
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean doIHavePermission(){
final UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
final List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, 0, System.currentTimeMillis());
return !queryUsageStats.isEmpty();
}
Daily interval with start date 0 and end date the current time must at least return todays usage.So it will be empty only if permissions are not granted.
Check this answer:
Tomik's answer
If you hurry, here's the solution ;)
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static boolean usageAccessGranted(Context context) {
AppOpsManager appOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
android.os.Process.myUid(), context.getPackageName());
return mode == AppOpsManager.MODE_ALLOWED;
}
I stumbled on the same problem. On Samsung S5 Lollipop usage stats did not work with the following code:
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
However usage stats actually exist. With the following code one can open the security settings:
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
intent.setComponent(new ComponentName("com.android.settings","com.android.settings.Settings$SecuritySettingsActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
An the scroll to the bottom and there is usage stats. I also inspeced logs and by pressing usage stats, you are directed to SubActivity which contains UsageStats fragment. I tried the following code:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.SubSettings"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
But got security exception. The problem is that they didnt mark SubActivity as exported, so as far as I know its not possible to directly start SubActivity (usage stats window). The only solution is to take user to Securiy settings and them tell him to manually go to usage stats view and enable app.
If someone finds better solution it would be great!
See ActivityNotFoundException in Lollipop when trying to launch activity with intent android.settings.USAGE_ACCESS_SETTINGS
for a better way, since with method we cannot discern whether there are simple no stats for the time interval

Is there a way to add reminders to a new calendar event using Intents?

I need to support Android 2.1 and up. I know that CalendarContract isn't available in Android 2.1, so I've done the following workaround.
Intent intent = new Intent(Intent.ACTION_EDIT)
.setType("vnd.android.cursor.item/event")
.putExtra("beginTime", beginTime.getTimeInMillis())
.putExtra("title", title)
.putExtra("description", description)
.putExtra("eventLocation", location)
.putExtra("allDay", allDay)
.putExtra(Intent.EXTRA_EMAIL, email );
if(!allDay) {
intent.putExtra("endTime", endTime.getTimeInMillis());
}
startActivity(intent);
This works very well so far. I've tested on 2.1 through 4.1.
I'd like to add reminders, too, but I can't find any documentation on how to do it using Intents. Does anyone have an example? I want to avoid adding more permissions to my manifest for writing to the calendar, so if you have a suggestion that requires that, I won't be able to use it.
If you check the stock android Calendar source code, reminders cannot be added using intent.
Instead of this calendar has a setting to set the default reminder. But some OEMs could have implemented this. So even if you find it, it will not work on all phones.

Running task periodicaly(once a day/once a week)

I want to run some task (i.e. get my web site news page) periodically (once a week/ a day), even if my application is closed. Is it possible?
Yes it is, you need to look at the AlarmManager to setup a reoccurring "Alarm". This is better for battery life on the device, as unlike a service it does not run constantly in the background. The Alarm triggers a broadcast receiver which will execute your custom code.
As a final note - there are enum values for the timing of the Alarm including daily, half daily and many more although you can just set an actual value.
A good example can be found in the follow SO post:
Alarm Manager Example
Update
Newer features have been added to Android. If you are reading this then I would advise you now look into GcmNetworkManager. This optimises battery life and works pre-lollipop. For Lollipop onwards you can use JobScheduler. I would advise using these classes over the AlarmManager.
I think the best fit is GcmNetworkManager. Basically it has everything you need from AlarmManager plus persistence, so job can proceed executing after reboot.
Example:
PeriodicTask task = new PeriodicTask.Builder()
.setService(MyTaskService.class)
.setTag(TASK_TAG_PERIODIC)
.setPeriod(5L)
.build();
mGcmNetworkManager.schedule(task);
As an alternative I'm comparing the current week:
Calendar cal = Calendar.getInstance();
int currentWeekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
SharedPreferences sharedPreferences= this.getSharedPreferences("appInfo", 0);
int weekOfYear = sharedPreferences.getInt("weekOfYear", 0);
if(weekOfYear != currentWeekOfYear){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("weekOfYear", currentWeekOfYear);
editor.commit();
// Your once a week code here
}
I'm not advocating this is better than the Alarm solution. I'm just showing a different approach.

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())

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

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()).

Categories

Resources