本文介绍了PhoneGap的设定从WWW资产壁纸? Android版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要建为Android PhoneGap的一个应用程序,需要一种方法来从包括在使用JavaScript应用程序的www目录.jpg格式设置壁纸。我怎么会去建立一个PhoneGap的插件,在我的PhoneGap应用www文件夹与资源工作的?
I'm building a phonegap app for Android and need a way to set wallpapers from a .jpg included in the www directory of the app using javascript. How would I go about building a phonegap plugin that works with resources in my phonegap apps www folder?
推荐答案
刚读从资产文件夹文件。用插件
just read file from asset folder. with Plugin
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 {
InputStream bitmap=null;
bitmap=getAssets().open("www/img/" + arg1.getString(0));//reference to image folder
Bitmap bit=BitmapFactory.decodeStream(bitmap);
wallpaperManager.setBitmap(bit);
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());
})
和映像文件名称主文件调用插件
and main file call Plugin with imagefilename
window.plugins.test.set("imageFileName.jpg",
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"/>
这篇关于PhoneGap的设定从WWW资产壁纸? Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!