如何设置图像采用Android PhoneGap的壁纸?
我用的PhoneGap
和 jQuery Mobile的
在一个Android应用程序。我需要保存图像的URL,并将其设置为墙纸。
I'm using Phonegap
and Jquery Mobile
on an Android app. I need to save an image from URL and set it as a wallpaper.
我找到了PhoneGap的下载插件,可以处理下载的一部分。是否有implemanets设置为墙纸功能的插件?
I found the Phonegap Downloader plugin that can handle the downloading part. Is there a plugin that implemanets "set as wallpaper" functionality?
您可以创建PhoneGap的插件 包com.android.test;
You can create Phonegap Plugin package com.android.test;
import java.io.IOException;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import android.app.WallpaperManager;
import android.content.Context;
public class testPlugin extends Plugin {
public final String ACTION_SET_WALLPAPER = "setWallPaper";
@Override
public PluginResult execute(String action, JSONArray arg1, String callbackId) {
PluginResult result = new PluginResult(Status.INVALID_ACTION);
if (action.equals(ACTION_SET_WALLPAPER)) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance((Context) this.ctx);
try {
wallpaperManager.setResource(R.drawable.ic_launcher);
result = new PluginResult(Status.OK);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = new PluginResult(Status.ERROR, e.getMessage());
}
}
return result;
}
}
这是JavaScript文件test.js
this is javascript file test.js
var TestPlugin = function () {};
TestPlugin.prototype.set = function (ms, successCallback, failureCallback) {
// navigator.notification.alert("OMG");
return cordova.exec(successCallback, failureCallback, 'testPlugin', "setWallPaper", [ms]);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("test", new TestPlugin());
})
和主文件中调用插件
window.plugins.test.set("kaka",
function () {
navigator.notification.alert("Set Success");
},
function (e) {
navigator.notification.alert("Set Fail: " + e);
}
);
;
与Android设备的许可
with android device permission
<uses-permission android:name="android.permission.SET_WALLPAPER" />
和plugin.xml中
and plugin.xml
<plugin name="testPlugin" value="com.android.test.testPlugin"/>
在你下载的图像下载插件并保存位图。你只需要调用
while you download image with downloader plugin and save with bitmap. you just call
wallpaperManager.setBitmap(bitmap)