I have MainActivity which has 5 buttons each of them has intent to service with server. Main has alarmmanager for repeat the connection with the server(service) and i have receiver class so when i close the app or restart the device the service still works so here is my problems.
- when I click on any button I activate the service in the service there is httpurlconnection to connect with my server so let say i want the wallpaper to change every 10 min ((it changes in 8 min then in 4 min and sometimes in 10 min ???)).
my code is right but this problems appears to me today (yestarday worked great)
I'm sending Post-Request. My service is about sending Post-request to server and use the response to change the wallpaper inside the service.
private final static int a = 1000*60*5; //5 min
private final static int b = 1000 * 60 * 10; // 10 min
private final static int c = 1000 * 60 * 30; // 30 min
private final static int d = 1000 * 60 * 120; // 2 hours
private final static int e = 1000 * 60 * 360; // 6 hours
private final static int f = 1000*60*1440; // 1 day
preference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1: {
intent = new Intent(MainActivity.this, WallService.class);
pintent = PendingIntent.getService(MainActivity.this, 0, intent, 0);
preference.edit().putInt("timeInterval", a).apply();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), a, pintent);
startService(intent);
}
case R.id.btn2: {
intent = new Intent(MainActivity.this, WallService.class);
pintent = PendingIntent.getService(MainActivity.this, 0, intent, 0);
preference.edit().putInt("timeInterval", b).apply();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), b, pintent);
startService(intent);
}
case R.id.btn3: {
intent = new Intent(MainActivity.this, WallService.class);
pintent = PendingIntent.getService(MainActivity.this, 0, intent, 0);
preference.edit().putInt("timeInterval", c).apply();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), c, pintent);
startService(intent);
}
case R.id.btn4: {
intent = new Intent(MainActivity.this, WallService.class);
pintent = PendingIntent.getService(MainActivity.this, 0, intent, 0);
preference.edit().putInt("timeInterval", d).apply();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), d, pintent);
startService(intent);
}
case R.id.btn5: {
intent = new Intent(MainActivity.this, WallService.class);
pintent = PendingIntent.getService(MainActivity.this, 0, intent, 0);
preference.edit().putInt("timeInterval", e).apply();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), e, pintent);
startService(intent);
}
case R.id.btn6: {
intent = new Intent(MainActivity.this, WallService.class);
pintent = PendingIntent.getService(MainActivity.this, 0, intent, 0);
preference.edit().putInt("timeInterval", f).apply();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), f, pintent);
startService(intent);
}
}
}
Manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<receiver android:name=".BootCompletedIntentReceiver" android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".WallService" android:enabled="true"
android:exported="true"></service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>