如何设置图像采用Android PhoneGap的壁纸? [英] How to set image as wallpaper using Android Phonegap?

查看:112
本文介绍了如何设置图像采用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)

这篇关于如何设置图像采用Android PhoneGap的壁纸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆