Cordova 是否有自动将 onActivityResult 传递给它的 CordovaPlugin 类的方法?
这是我当前的文件,手动执行:
package com.myapp;
import android.os.Bundle;
import org.apache.cordova.*;
import android.content.Intent;
import com.flyingsoftgames.googleplaytoken.GooglePlayToken;
public class MyApp extends CordovaActivity {
@Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {
GooglePlayToken.runOnActivityResult (requestCode, resultCode, data);
}
@Override public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
super.init ();
super.loadUrl(Config.getStartUrl());
}
}
我尝试使用与 https://github.com/apache/cordova-plugin-camera/blob/master/src/android/CameraLauncher.java 中使用的相同技术让数据“自动”通过——但它崩溃了。这是目标类的工作(手动和无数据传递)和非工作(自动数据传递)版本。先工作,然后不工作。
工作,没有数据传递:
// Working, no data pass-through.
package com.flyingsoftgames.googleplaytoken;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.AccountPicker;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.content.Intent;
import android.app.Activity;
import android.accounts.AccountManager;
import android.os.AsyncTask;
import android.os.Bundle;
import java.io.IOException;
import android.util.Log;
public class GooglePlayToken extends CordovaPlugin {
private static final String LOG_TAG = "GooglePlayToken";
private static final int REQ_SIGN_IN_REQUIRED = 55664;
public static CordovaInterface cordova = null;
public static CallbackContext tryConnectCallback = null;
public static String accessToken = "";
public static final int REQUEST_CODE_PICK_ACCOUNT = 1000;
@Override public void initialize (CordovaInterface initCordova, CordovaWebView webView) {
cordova = initCordova;
super.initialize (cordova, webView);
}
private void pickUserAccount () {
String[] accountTypes = new String[]{"com.google"};
Intent intent = AccountPicker.newChooseAccountIntent(null, null, accountTypes, false, null, null, null, null);
cordova.getActivity().startActivityForResult (intent, REQUEST_CODE_PICK_ACCOUNT);
}
public static void runOnActivityResult (int requestCode, int resultCode, Intent data) {
if ((requestCode == REQUEST_CODE_PICK_ACCOUNT) && (resultCode == Activity.RESULT_OK)) {
new RetrieveTokenTask().execute (data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME));
}
}
public boolean execute (String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException {
if ("tryConnect".equals(action)) {
tryConnect (callbackContext);
} else if ("getAccessToken".equals(action)) {
callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, accessToken));
}
return true;
}
// tryConnect runs the callback with a value of false if Google Play Services isn't available.
public void tryConnect (CallbackContext callbackContext) {
tryConnectCallback = callbackContext;
pickUserAccount ();
}
private static class RetrieveTokenTask extends AsyncTask<String, Void, String> {
@Override protected String doInBackground (String... params) {
String accountName = params[0];
String scope = "oauth2:" + Scopes.PROFILE;
Context context = cordova.getActivity().getApplicationContext();
try {
accessToken = GoogleAuthUtil.getToken(context, accountName, scope);
} catch (IOException e) {
String errormessage = e.getMessage();
if (tryConnectCallback != null) tryConnectCallback.error ("Error: " + errormessage + "."); tryConnectCallback = null;
} catch (UserRecoverableAuthException e) {
cordova.getActivity().startActivityForResult (e.getIntent(), REQ_SIGN_IN_REQUIRED);
} catch (GoogleAuthException e) {
String errormessage = e.getMessage();
if (tryConnectCallback != null) tryConnectCallback.error ("Error: " + errormessage + "."); tryConnectCallback = null;
}
return accessToken;
}
@Override protected void onPostExecute (String newAccessToken) {
super.onPostExecute (newAccessToken);
accessToken = newAccessToken;
if (tryConnectCallback != null) {
tryConnectCallback.sendPluginResult (new PluginResult (PluginResult.Status.OK, accessToken));
tryConnectCallback = null;
}
}
}
}
非工作,数据传递:
// Crashy / non-working, data pass-through.
package com.flyingsoftgames.googleplaytoken;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.AccountPicker;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.content.Intent;
import android.app.Activity;
import android.accounts.AccountManager;
import android.os.AsyncTask;
import android.os.Bundle;
import java.io.IOException;
import android.util.Log;
public class GooglePlayToken extends CordovaPlugin {
private final String LOG_TAG = "GooglePlayToken";
private final int REQ_SIGN_IN_REQUIRED = 55664;
public CordovaInterface cordova = null;
public CallbackContext tryConnectCallback = null;
public String accessToken = "";
public final int REQUEST_CODE_PICK_ACCOUNT = 1000;
@Override public void initialize (CordovaInterface initCordova, CordovaWebView webView) {
Log.e (LOG_TAG, "initialize");
cordova = initCordova;
super.initialize (cordova, webView);
}
private void pickUserAccount () {
Log.e (LOG_TAG, "pickUserAccount");
String[] accountTypes = new String[]{"com.google"};
Intent intent = AccountPicker.newChooseAccountIntent(null, null, accountTypes, false, null, null, null, null);
//cordova.startActivityForResult ((CordovaPlugin) this, intent, REQUEST_CODE_PICK_ACCOUNT); // Tried this, too.
cordova.getActivity().startActivityForResult (intent, REQUEST_CODE_PICK_ACCOUNT);
}
//public static void runOnActivityResult (int requestCode, int resultCode, Intent data) {
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.e (LOG_TAG, "runOnActivityResult");
if ((requestCode == REQUEST_CODE_PICK_ACCOUNT) && (resultCode == Activity.RESULT_OK)) {
new RetrieveTokenTask().execute (intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME));
}
}
public boolean execute (String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException {
Log.e (LOG_TAG, "execute: " + action);
if ("tryConnect".equals(action)) {
tryConnect (callbackContext);
} else if ("getAccessToken".equals(action)) {
callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, accessToken));
}
return true;
}
// tryConnect runs the callback with a value of false if Google Play Services isn't available.
public void tryConnect (CallbackContext callbackContext) {
Log.e (LOG_TAG, "tryConnect");
tryConnectCallback = callbackContext;
pickUserAccount ();
}
private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
@Override protected String doInBackground (String... params) {
Log.e (LOG_TAG, "RetrieveTokenTask");
String accountName = params[0];
String scope = "oauth2:" + Scopes.PROFILE;
Context context = cordova.getActivity().getApplicationContext();
try {
accessToken = GoogleAuthUtil.getToken(context, accountName, scope);
GoogleAuthUtil.clearToken (context, accessToken);
accessToken = GoogleAuthUtil.getToken(context, accountName, scope);
Log.e (LOG_TAG, accessToken);
} catch (IOException e) {
String errormessage = e.getMessage();
if (tryConnectCallback != null) tryConnectCallback.error ("Error: " + errormessage + "."); tryConnectCallback = null;
} catch (UserRecoverableAuthException e) {
cordova.getActivity().startActivityForResult (e.getIntent(), REQ_SIGN_IN_REQUIRED);
} catch (GoogleAuthException e) {
String errormessage = e.getMessage();
if (tryConnectCallback != null) tryConnectCallback.error ("Error: " + errormessage + "."); tryConnectCallback = null;
}
return accessToken;
}
@Override protected void onPostExecute (String newAccessToken) {
super.onPostExecute (newAccessToken);
accessToken = newAccessToken;
if (tryConnectCallback != null) {
tryConnectCallback.sendPluginResult (new PluginResult (PluginResult.Status.OK, accessToken));
tryConnectCallback = null;
}
}
}
}
最佳答案
事实证明,我需要做的就是在 cordova.getActivity().startActivityForResult (intent, REQUEST_CODE_PICK_ACCOUNT); 之前运行 cordova.setActivityResultCallback (this);。
关于java - 在 Cordova 中传递 ActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27280625/
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg