如何使用 Android Phonegap 将图像设置为墙纸? [英] How to set image as wallpaper using Android Phonegap?

查看:23
本文介绍了如何使用 Android Phonegap 将图像设置为墙纸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 应用程序上使用 PhonegapJquery Mobile.我需要从 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 Downloader 插件可以处理下载部分.是否有实现设置为墙纸"功能的插件?

I found the Phonegap Downloader plugin that can handle the downloading part. Is there a plugin that implemanets "set as wallpaper" functionality?

推荐答案

您可以创建 Phonegap Plugin包 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);
        }
    );

;

具有安卓设备权限

<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天全站免登陆