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

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
 

Armuro

Newbie
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

Member
Thread starter
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?
 

flyhigh427

Member
Thread starter
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:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[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{
[/SIZE][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/LEFT]
[/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) {
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].onCreate(savedInstanceState);
Intent myIntent = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Intent(getBaseContext(), 
myreceiver.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
myIntent.setAction([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"com.try1.pratice1.ACTION"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); 

PendingIntent pendingIntent 
= PendingIntent.[I]getBroadcast[/I](getBaseContext(), 
0, myIntent, 0);

AlarmManager alarmManager 
= (AlarmManager)getSystemService([/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ALARM_SERVICE[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); 
Calendar calendar = Calendar.[I]getInstance[/I](); 
[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//calendar.setTimeInMillis(System.currentTimeMillis()); [/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//calendar.set(Calendar.DAY_OF_MONTH, 22);[/LEFT]
[/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);
calendar.set(Calendar.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]MINUTE[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 0);
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]); 

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (calendar.getTimeInMillis()<System.[I]currentTimeMillis[/I]()) { 
calendar.add(Calendar.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]DAY_OF_YEAR[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 1); 
}


alarmManager.setRepeating(AlarmManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RTC_WAKEUP[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],calendar.getTimeInMillis(),
AlarmManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]INTERVAL_DAY[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], pendingIntent);


finish();
}






}

&#12288;[/LEFT]
[/SIZE]
 

Armuro

Newbie
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.
 
Top