我正在为 android 制作壁纸应用程序,但我在 Muzei 支持方面遇到问题,希望这里的人可以帮助我了解我所缺少的内容。
我有一个 JSON 文件,我的应用程序使用它来获取壁纸 URL 并从中显示图片。它工作正常,并且得到正确的图像。但是,如果我用更多条目和更多图像更新 JSON,那么 Muzei 扩展仍然使用旧数据库。我在想也许它缓存了数据库,只是出于任何原因都没有更新它。让它使用更新的 JSON 文件的唯一方法是清除我的应用程序的数据并在 Muzei 中设置不同的扩展名,然后使用我的扩展名重置 Muzei。这对用户来说不是很友好。
可能只是失明,但我们将不胜感激。
ArtSource.java:
package com.main.walls.muzei;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import com.google.android.apps.muzei.api.Artwork;
import com.google.android.apps.muzei.api.RemoteMuzeiArtSource;
import com.google.android.apps.muzei.api.UserCommand;
import com.main.walls.utilities.Preferences;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Random;
import walls.wallpaper.R;
public class ArtSource extends RemoteMuzeiArtSource {
private WallsDatabase wdb;
private ArrayList<WallpaperInfo> wallslist;
private Preferences mPrefs;
private static final String ARTSOURCE_NAME = "Walls";
private static final String JSON_URL = "http://pastebin.com/raw.php?i=VWTzhJ0N";
private static final String MARKET_URL = "https://play.google.com/store/apps/details?id=";
private static final int COMMAND_ID_SHARE = 1337;
public ArtSource() {
super(ARTSOURCE_NAME);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String command = intent.getExtras().getString("service");
if (command != null) {
try {
onTryUpdate(UPDATE_REASON_USER_NEXT);
} catch (RetryException e) {
Log.d("MuzeiArtSource", Log.getStackTraceString(e));
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
wdb = new WallsDatabase(getApplicationContext());
wallslist = new ArrayList<>();
mPrefs = new Preferences(ArtSource.this);
ArrayList<UserCommand> commands = new ArrayList<>();
commands.add(new UserCommand(BUILTIN_COMMAND_ID_NEXT_ARTWORK));
commands.add(new UserCommand(COMMAND_ID_SHARE, getString(R.string.justshare)));
setUserCommands(commands);
}
@Override
public void onCustomCommand(int id) {
super.onCustomCommand(id);
if (id == COMMAND_ID_SHARE) {
Artwork currentArtwork = getCurrentArtwork();
Intent shareWall = new Intent(Intent.ACTION_SEND);
shareWall.setType("text/plain");
String authorName = currentArtwork.getByline();
String storeUrl = MARKET_URL + getResources().getString(R.string.package_name);
String iconPackName = getString(R.string.app_name);
shareWall.putExtra(Intent.EXTRA_TEXT,
getString(R.string.partone) + authorName +
getString(R.string.parttwo) + iconPackName +
getString(R.string.partthree) + storeUrl);
shareWall = Intent.createChooser(shareWall, getString(R.string.share_title));
shareWall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(shareWall);
}
}
@Override
protected void onTryUpdate(int reason) throws RetryException {
if (mPrefs.isFeaturesEnabled()) {
if (wallslist.size() == 0)
getWallpapersFromUrl(JSON_URL);
int i = getRandomInt();
String token = wallslist.get(i).getWallURL();
publishArtwork(new Artwork.Builder()
.byline(wallslist.get(i).getWallAuthor())
.imageUri(Uri.parse(wallslist.get(i).getWallURL()))
.token(token)
.viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(wallslist.get(i).getWallURL())))
.build());
scheduleUpdate(System.currentTimeMillis() + mPrefs.getRotateTime());
}
}
private int getRandomInt() {
return new Random().nextInt(wallslist.size());
}
private void getWallpapersFromUrl(String url) {
wallslist.clear();
wallslist = wdb.getAllWalls();
if (wallslist.size() == 0) {
try {
HttpClient cl = new DefaultHttpClient();
HttpResponse response = cl.execute(new HttpGet(url));
if (response.getStatusLine().getStatusCode() == 200) {
final String data = EntityUtils.toString(response.getEntity());
JSONObject jsonobject = new JSONObject(data);
final JSONArray jsonarray = jsonobject.getJSONArray("wallpapers");
wallslist.clear();
wdb.deleteAllWallpapers();
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
WallpaperInfo jsondata = new WallpaperInfo(
jsonobject.getString("author"),
jsonobject.getString("url")
);
wdb.addWallpaper(jsondata);
wallslist.add(jsondata);
}
}
} catch (Exception e) {
Log.d("Wallpapers", Log.getStackTraceString(e));
}
}
}
}
不确定我是否需要这里的任何其他代码,logcat 什么也没说,就好像它正常工作一样。如果您需要查看更多代码,请告诉我。
谢谢你的帮助。