Reputation: 67
In my application im using a alarmManager to make it execute something i want every 24 hours (for example).
Its almost working perfectly but for tests purposes, i defined my interval to 5000, as you can see here:
MainActivity.java:
public void startAlarm() {
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 5000;
//86400000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
}
My AlarmReceiver.java:
public class AlarmReceiver extends BroadcastReceiver {
QuestsHelper db = new QuestsHelper(MainActivity.appContext);
@Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
Random rand = new Random();
long id = rand.nextInt(10);
newQuest = db.getRandomQuest(id+1);
MainActivity.desc.setText(newQuest.getDesc());
MainActivity.status.setText(String.valueOf(newQuest.getProgress()));
MainActivity.exp.setText(String.valueOf(newQuest.getExp()));
SharedPreferences DadosNewQuest = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = DadosNewQuest.edit();
editor.putString("desc", newQuest.getDesc());
editor.putInt("status", newQuest.getProgress());
editor.putInt("exp", newQuest.getExp());
editor.apply();
SharedPreferences shareprefs = context.getSharedPreferences("cscs", MODE_PRIVATE);
SharedPreferences.Editor editor2 = shareprefs.edit();
editor2.putLong("cscs", newQuest.getCsGoal());
editor2.commit();
showNewQuestNotification();
Log.d("AlarmReceiver", "TRIGGER");
}
private void showNewQuestNotification() {
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(appContext)
.setSmallIcon(R.drawable.lpq)
.setContentTitle("New Quest available")
.setContentText("You have a new quest to do !");
// para vibrar:
// builder.setVibrate(new long[] { 1000, 1000});
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
Intent notificationIntent = new Intent(appContext, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(appContext, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
Below is the android log-cat it seems like it is executing every one minute even though the interval is set to 5000.
Complete MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public static Context appContext;
private PendingIntent pendingIntent;
private AlarmManager manager;
private Game selectedGame;
private SessionManager session;
private static final String TAG = "teste";
private static final String TAG2 = "teste2";
public static TextView desc;
public static TextView status;
public static TextView exp;
public EditText search;
public Button button;
public Long csjogo = null; // minions farmados
public static Quest newQuest = new Quest();
String URL = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
appContext=getApplicationContext();
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences shareprefs = getSharedPreferences("cscs", MODE_PRIVATE);
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences DadosNewQuest = PreferenceManager.getDefaultSharedPreferences(this);
session = new SessionManager(getApplicationContext());
desc = (TextView) findViewById(R.id.desc);
status = (TextView) findViewById(R.id.status);
exp = (TextView) findViewById(R.id.exp);
search = (EditText) findViewById(R.id.search);
button = (Button) findViewById(R.id.button);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
if (!prefs.getBoolean("firstTime", false)) {
Log.d(TAG, "onCreate: CORREU");
startAlarm();
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}
desc.setText(DadosNewQuest.getString("desc", ""));
status.setText(String.valueOf(DadosNewQuest.getInt("status", 0)));
exp.setText(String.valueOf(DadosNewQuest.getInt("exp", 0)));
Log.d("lul", DadosNewQuest.getString("desc", ""));
Log.d("lul", String.valueOf(DadosNewQuest.getInt("status", 0)));
Log.d("lul", String.valueOf(DadosNewQuest.getInt("exp", 0)));
MainActivity.status.setText(String.valueOf(newQuest.getProgress()));
MainActivity.exp.setText(String.valueOf(newQuest.getExp()));
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "A verificar ...", Toast.LENGTH_SHORT).show();
verifyQUEST(sharedPref.getString("username", ""), shareprefs.getLong("cscs",0));
}
});
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
AsyncRiotAPI.setRegion(Region.EUW);
AsyncRiotAPI.setAPIKey("");
Log.d(TAG2, String.valueOf(shareprefs.getLong("cscs",0)));
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
public void startAlarm() {
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 5000;
//86400000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
}
public void cancelAlarm() {
if (manager != null) {
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
}
public void verifyQUEST(String nick, final long csquest) {
AsyncRiotAPI.getRecentGames(new Action<List<Game>>() {
@Override
public void perform(final List<Game> games) {
runOnUiThread(new Runnable() {
public void run() {
selectedGame = games.get(0);
csjogo = (long) selectedGame.getStats().getMinionsKilled() + selectedGame.getStats().getNeutralMinionsKilledEnemyJungle() + selectedGame.getStats().getNeutralMinionsKilledYourJungle();
Log.d(TAG, String.valueOf(csjogo));
if (csjogo >= csquest ) {
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
ViewDialog alert = new ViewDialog();
alert.showDialog(MainActivity.this, "Congratulations, quest completed !", csquest, csjogo);
addPoints(newQuest.getExp(), sharedPref.getString("username", ""));
} else {
NegativeViewDialog alert = new NegativeViewDialog();
alert.showDialog(MainActivity.this, "What a noob, you failed the quest !", csquest, csjogo);
}
}
});
}
@Override
public void handle(APIException e) {
Log.d(TAG, "ERRO, RIP");
}
}, nick );
}
private void addPoints (final int points, final String nickname) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("points", String.valueOf(points));
params.put("nickname", nickname);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.about, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_login) {
if ( session.isLoggedIn()) {
Toast.makeText(appContext, "You are already logged in !", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
} else if (id == R.id.nav_about) {
} else if (id == R.id.nav_profile) {
Intent intent = new Intent(MainActivity.this, ProfileQuest.class);
startActivity(intent);
} else if (id == R.id.nav_manage) {
Intent intent = new Intent(MainActivity.this, QuestsActivity.class);
startActivity(intent);
} else if (id == R.id.nav_board) {
} else if (id == R.id.nav_contacts) {
} else if (id == R.id.nav_logout) {
if (session.isLoggedIn()) {
session.setLogin(false);
}
Toast.makeText(appContext, "logout", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
} else if (id == R.id.nav_settings) {
Intent intent = new Intent(MainActivity.this, UserSettings.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Any toughts ?
Upvotes: 0
Views: 75
Reputation: 93561
Alarm Manager doesn't allow you to fire more than once every 20 seconds in any mode. If you need it more frequently than that, use a Handler and postDelayed rather than an alarm.
Using setRepeating, Android reserves the right to reschedule you to a more convenient time for the system to minimize battery power from timers firing. setExact is the method you need to get a faster timer, but should only be used if you absolutely need it.
Please note that on newer Android, Doze will stop any timers set by setExact except for a small window every 15 minutes or so once Doze turns on. setExactAndAllowWhileIdle will fire during Doze, but should only be used for VERY important timers such as alarm clocks and meeting reminders.
Upvotes: 1