• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

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

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);
 
  • Like
Reactions: flyhigh427
Upvote 0
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]
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones