1

我正在使用PhonegapAndroidJquery Mobile应用程序。我需要从 URL 保存图像并将其设置为墙纸。

我找到了可以处理下载部分的 Phonegap Downloader插件。是否有实现“设置为墙纸”功能的插件?

4

2 回答 2

3

您可以创建Phonegap插件包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

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

;

具有安卓设备权限

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

和 plugin.xml

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

当您使用下载器插件下载图像并使用位图保存时。你只要打电话

wallpaperManager.setBitmap(bitmap)
于 2012-04-06T02:45:35.107 回答
0

这是我修复 testPlugin.java 的方法。更改在下面以粗体显示。

package com.wizeideas.ImageEvolverApp;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class testPlugin extends CordovaPlugin {
    public final String ACTION_SET_WALLPAPER = "setWallPaper";
    @Override
    public boolean execute(String action, JSONArray arg1, CallbackContext callbackContext) {
        PluginResult result = new PluginResult(Status.INVALID_ACTION);
        if (action.equals(ACTION_SET_WALLPAPER)) {
            Context ctx = this.cordova.getActivity().getApplicationContext();
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(ctx);
            try {
                InputStream bitmap=null;
                try {
                    bitmap=this.cordova.getActivity().getAssets().open("www/img/" + arg1.getString(0));
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } //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());
            }
        }
        callbackContext.sendPluginResult(result);
        return true;
    }
}

您还需要更改 res/xml 下的 config.xml 文件以包括:

<feature name="testPlugin">
  <param name="android-package" value="com.companyname.yourappname.testPlugin"/>
</feature>    
于 2013-06-15T22:22:16.660 回答