Skip to content

Instantly share code, notes, and snippets.

@SelvinPL
Last active July 10, 2019 18:00
Show Gist options
  • Select an option

  • Save SelvinPL/be80f168797517291c81 to your computer and use it in GitHub Desktop.

Select an option

Save SelvinPL/be80f168797517291c81 to your computer and use it in GitHub Desktop.
package pl.selvin.wallpaperchanger;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import java.io.InputStream;
import java.net.URL;
public class Activity extends android.app.Activity {
final static String TAG = "AllInOne";
final static int CHANGE_INTERVAL = 30 * 1000; //30 sec for testing
static boolean loading = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PendingIntent pending = PendingIntent.getBroadcast(this, 666, new Intent("pl.selvin.wallpaperchanger.CHANGE_WALLPAPTER_TIMER"), PendingIntent.FLAG_CANCEL_CURRENT);
((AlarmManager) getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), CHANGE_INTERVAL, pending);
}
public static class Service extends IntentService {
public Service() {
super("wallpaperchanger-download");
}
@Override
protected void onHandleIntent(Intent intent) {
WallpaperManager wm = WallpaperManager.getInstance(this);
int height = wm.getDesiredMinimumHeight();
int width = wm.getDesiredMinimumWidth();
String url = "https://source.unsplash.com/category/nature/" + width + "x" + height + "/";
try {
InputStream input = new URL(url).openStream();
Log.v(TAG, url);
wm.setStream(input);
input.close();
} catch (Exception e) {
e.printStackTrace();
}
loading = false;
}
}
public static class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
if (!loading) {
loading = true;
context.startService(new Intent(context, Service.class));
}
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.selvin.wallpaperchanger">
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:label="WallpaperChanger">
<activity
android:name=".Activity"
android:label="WallpaperChanger">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".Activity$AlarmReceiver">
<intent-filter>
<action android:name="pl.selvin.wallpaperchanger.CHANGE_WALLPAPTER_TIMER" />
</intent-filter>
</receiver>
<service android:name=".Activity$Service" />
</application>
</manifest>
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "pl.selvin.wallpaperchanger"
minSdkVersion 5
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt')
}
}
}
dependencies {
}
Copyright Selvin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment