How to set image as wallpaper using Android Phonegap?

前端 未结 2 2073
攒了一身酷
攒了一身酷 2020-12-30 17:55

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.

I found the Phonegap D

2条回答
  •  一向
    一向 (楼主)
    2020-12-30 18:28

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

    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

    window.plugins.test.set("kaka",
            function () { 
                navigator.notification.alert("Set Success");    
            },
            function (e) {
                navigator.notification.alert("Set Fail: " + e);
            }
        );
    
    ;
    

    with android device permission

    
    

    and plugin.xml

     
    

    while you download image with downloader plugin and save with bitmap. you just call

    wallpaperManager.setBitmap(bitmap)
    

提交回复
热议问题