簡體   English   中英

如何使用 Android Phonegap 將圖像設置為牆紙?

[英]How to set image as wallpaper using Android Phonegap?

我在 Android 應用程序上使用PhonegapJquery Mobile 我需要保存來自 URL 的圖像並將其設置為牆紙。

我找到了可以處理下載部分的 Phonegap Downloader插件。 是否有實現“設置為牆紙”功能的插件?

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

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 設備權限

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

和插件.xml

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

當您使用下載器插件下載圖像並使用 bitmap 保存時。您只需調用

wallpaperManager.setBitmap(bitmap)

這是我修復 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>    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM