2012-04-05 7 views

答えて

3

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のファイルは、Androidデバイスの許可

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

とplugin.xmlの

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

; 

をプラグイン

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

とメインファイル呼び出しをtest.jsです

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

ダウンローダプラグインを使用して画像をダウンロードし、ビットマップで保存している間。あなたはちょうど私がtestPlugin.javaを固定する方法である。ここ

wallpaperManager.setBitmap(bitmap) 
+0

素晴らしい感謝! – MichaelS

+1

this.ctxがContextの代わりにLegacyContextを返すので、これはphonegap 2.0でもう機能しません。誰がそれが今になってどうなっているのか知っていますか?私はまだ適切な文書を見つけていません。 – Towa

0

を呼び出します。変更は太字で示しています。

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>  
関連する問題