java


Android AlaramManager of android not triggering at defined time


I want to trigger alarm with my song at specified time, below is what I've done :
This is in the java file :
public class MainActivity extends Activity implements View.OnClickListener
/** Called when the user clicks the Send button */
public void startMusic(View view) {
Integer hr = timepickerStart.getCurrentHour();
Integer min = timepickerStart.getCurrentMinute();
// Start service using AlarmManager
if(selectedFile != null && selectedFile.length() > 0)
{
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
final Intent intent = new Intent(this,MusicPlayActivity.class);
intent.putExtra(EXTRA_MESSAGE, selectedFile.getPath());
PendingIntent pendingIndent = PendingIntent.getBroadcast(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Calendar firingCal = Calendar.getInstance();
System.out.println("Before Present time in mili" + firingCal.getTimeInMillis());
firingCal.add(Calendar.HOUR, hr);
firingCal.add(Calendar.MINUTE, min);
firingCal.add(Calendar.SECOND, 0);
long intendedTime = firingCal.getTimeInMillis();
System.out.println("After setting Present time in mili" + firingCal.getTimeInMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime, AlarmManager.INTERVAL_DAY, pendingIndent);
Toast.makeText(this, "Alarm set from today", Toast.LENGTH_LONG).show();
} else
{
Toast.makeText(this, "Please select music File", Toast.LENGTH_LONG).show();
}
}
Now in manifest file
<activity android:name="com.example.test.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The issue is it starts on its own will.
Exmaple :
If the mobile time is 9:30 AM 2/12/2013 (DD/MM/YYYY), and I select the Trigger time at 10:00 AM on the same day. This will not trigger at 10:00 AM at all, it will trigger the next day that is 3/12/2013 some different time that too not at 10:00 AM
I am not able to judge the timing properly.
First of all why are using wrapper class(Integer) to store hour and min simply use int variable
Set value to calender not add to calender
firingCal.set(Calendar.HOUR, hr);//better use Calander.HOUR_OF_DAY bcoz hour range is 0 to 11 HOUR_OF_DAY 0 23 bcoz of this also your alarm is triggering next day
firingCal.set(Calendar.MINUTE, min);
firingCal.set(Calendar.SECOND, 0);
I have made some simple alarm manager demo,which is working fine in my case,try and if it works fine for you than implement same in your code
Here is my MainActivity
Here is MyReceiver Class
Here is AlarmActivity class
And here are layout xml files
activity_main.xml
alarmview.xml
In AlarmActivity at line number 76 this line mMediaPlayer.setVolume(0,0); is responsible for muting alarm sound,if you don't want to mute it than simply comment that line.

Related Links

Android Emulator won't boot or start. Nothing is popping up
savedInstanceState causing a crash while screen rotation
Java 2D Array, find containing elements
wait for user input in swing
What is the difference between java.util.concurrent.SynchronousQueue and java.util.concurrent.Exchanger?
convert decimal as well as character to hex and stored it into unsigned array and send it to bluetooth
Single click dont provides me Output
Playing HDMI input on an android media player
Missing or invalid type argument for pointer action - Selenium
How to stop the handler here when the button will be clicked again?
How to use WebRTC and Java WebSockets to achive realtime video sharing
Play Framework 2 + Java - Many HTTP Requests Upon One Web Page Request
Spark failed to delete temp directory
While requesting access token reddit api is returning 429 error
Getting error Failed to load image while working with android vision api for scanning qr code
Converting output of ASN1Dump.dumpAsString to a class in java

Categories

HOME
log4j
view
sd-card
google-oauth
rubygems
enterprise-library-5
azure-media-services
awesome-wm
google-translate
here-api
php-7.1
n-gram
hapi
highlight.js
fatal-error
angular-ui
reactcsstransitiongroup
flux
reverse-proxy
paging
css-animations
hammerspoon
dxf
numerical-methods
facebook-access-token
rst2pdf
neo4j-spatial
windowbuilder
lightswitch-2013
microsoft-chart-controls
phonegap
scorm2004
noraui
typed.js
webtest
mozilla
language-concepts
plsql-psp
espeak
neuroscience
libvpx
code-search-engine
c11
twitch
cookiecutter-django
awt
email-templates
node-gyp
botbuilder
estimote
flashair
avconv
xml-documentation
jxcore
font-size
google-perftools
dtexec
akka-cluster
pearson
mako
titanium-android
orthogonal
flash-cs5
make-install
sdf
rvest
oauth2client
goose
ptrace
system.management
phpcas
execute
metaclass
mono-embedding
web-controls
python-green
clicktag
angularjs-ng-click
titanium-modules
code-access-security
terminfo
responsive-slides
starcluster
fluentautomation
yorick
balanced-payments
spring-io
shellexecute
random-seed
html-editor
free-variable
reddot
cufon
libc++
zpt
mod-auth
blackberry-playbook
table-footer
sql-server-profiler
imac
xfbml
telerik-scheduler
fluent-interface
forums
asp.net-profiles
firefox-5
zend-tool
ti-dsp

Resources

Encrypt Message