1. Download our Official Android App: Forums for Android!

Apps if my app needs to sound an alarm ,will it stay on and stop other system stuff up

Discussion in 'Android Development' started by flyhigh427, Jul 16, 2013.

  1. flyhigh427

    flyhigh427 Member
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    53
    Joined:
    Jan 10, 2013

    Jan 10, 2013
    53
    1
    16
    im making another app and i need it to sound an alarm at some point.
    my question is will the app stay running all the time and will if affect the system or not?

    how do minmize the use of resources or is that best or possible?

    thank all
     

    Advertisement

  2. Armuro

    Armuro Member
    Rank:
    None
    Points:
    36
    Posts:
    30
    Joined:
    Jul 15, 2013

    Jul 15, 2013
    30
    8
    36
    Ph.D. Candidate
    Michigan
    The AlarmManager that you're going to use won't consume much resources. All you have to do is to create a broadcast receiver and register it in the Manifest file. E.g.


    <receiver android:name="com.example.YourAlarmReceiver">
    <intent-filter>
    <action android:name="com.example.yourAlarmActionName" />
    </intent-filter>
    </receiver>

    And in your code, find some place to create the alarm (the example shows trigger an alarm after 1 minute:

    PendingIntent pi = PendingIntent.getBroadcast(YourContext, 0,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService( Context.ALARM_SERVICE );
    am.set(AlarmManager.RTC_WAKEUP, new Date().getTime + 60 * 1000, pi);
     
    flyhigh427 likes this.
  3. flyhigh427

    flyhigh427 Member
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    53
    Joined:
    Jan 10, 2013

    Jan 10, 2013
    53
    1
    16
    hey and thank you
    i was thinking i would use it to start an
    Activity every morning to check input from my app
    everyday
    i guess i can start an Activity in the app or will i have to start the whole app?
     
  4. flyhigh427

    flyhigh427 Member
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    53
    Joined:
    Jan 10, 2013

    Jan 10, 2013
    53
    1
    16
    worked like a charm
    thanks
     
  5. flyhigh427

    flyhigh427 Member
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    53
    Joined:
    Jan 10, 2013

    Jan 10, 2013
    53
    1
    16
    i call this activity from the main activity and the reboot activity..
    it works but i was wondering does it look alright or is there a better way?
    thanks









    Code (Text):
    1. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
    2. [LEFT]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] alarmsetter_activity [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Activity{
    3. [/SIZE][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/LEFT]
    4. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]protected[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onCreate(Bundle savedInstanceState) {
    5. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].onCreate(savedInstanceState);
    6. Intent myIntent = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Intent(getBaseContext(),
    7. myreceiver.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    8. myIntent.setAction([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"com.try1.pratice1.ACTION"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    9.  
    10. PendingIntent pendingIntent
    11. = PendingIntent.[I]getBroadcast[/I](getBaseContext(),
    12. 0, myIntent, 0);
    13.  
    14. AlarmManager alarmManager
    15. = (AlarmManager)getSystemService([/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ALARM_SERVICE[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    16. Calendar calendar = Calendar.[I]getInstance[/I]();
    17. [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//calendar.setTimeInMillis(System.currentTimeMillis()); [/LEFT]
    18. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//calendar.set(Calendar.DAY_OF_MONTH, 22);[/LEFT]
    19. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]calendar.set(Calendar.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]HOUR_OF_DAY[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],7);
    20. calendar.set(Calendar.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]MINUTE[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 0);
    21. calendar.set(Calendar.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]AM_PM[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], Calendar.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]AM[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    22.  
    23. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (calendar.getTimeInMillis()<System.[I]currentTimeMillis[/I]()) {
    24. calendar.add(Calendar.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]DAY_OF_YEAR[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 1);
    25. }
    26.  
    27.  
    28. alarmManager.setRepeating(AlarmManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RTC_WAKEUP[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],calendar.getTimeInMillis(),
    29. AlarmManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]INTERVAL_DAY[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], pendingIntent);
    30.  
    31.  
    32. finish();
    33. }
    34.  
    35.  
    36.  
    37.  
    38.  
    39.  
    40. }
    41.  
    42. &#12288;[/LEFT]
    43. [/SIZE]
     
  6. Armuro

    Armuro Member
    Rank:
    None
    Points:
    36
    Posts:
    30
    Joined:
    Jul 15, 2013

    Jul 15, 2013
    30
    8
    36
    Ph.D. Candidate
    Michigan
    Basically your method looks fine to me. Two more comments:

    You can use cal.roll(Calendar.DAY_OF_MONTH, 1) instead of the add() method.

    If you use HOUR_OF_DAY, you're using the 24HR format, so you don't need to set AM/PM.
     
    flyhigh427 likes this.

Share This Page

Loading...