Phonegap setting wallpaper from www assets? Android

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

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?

回答1:

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;         }     } 

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);         }     );  ; 

with android device permission

<uses-permission android:name="android.permission.SET_WALLPAPER" /> 

and plugin.xml

<plugin name="testPlugin" value="com.android.test.testPlugin"/> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!