2014-11-25 56 views
0

我希望能够从Google Play安装中获取传递给我的Cordova应用的查询字符串。我一直在努力让它可靠地工作,不必介意在安装Google Play后运行它。我已经尝试过动态和静态广播接收器,并且暂时使用静态广播接收器,但现在它不能。通过广播接收器从Google Play获取查询字符串?

编辑:首先,让我补充一点,我从试图获得该输出并未能注入广播(详见下文)。 (我的手机是不是植根......确实adb shell am broadcast正常工作无根?):

D/AndroidRuntime(19631): 
D/AndroidRuntime(19631): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<< 
D/AndroidRuntime(19631): CheckJNI is OFF 
D/AndroidRuntime(19631): setted country_code = USA 
D/AndroidRuntime(19631): setted countryiso_code = US 
D/AndroidRuntime(19631): setted sales_code = ATT 
D/AndroidRuntime(19631): readGMSProperty: start 
D/AndroidRuntime(19631): readGMSProperty: already setted!! 
D/AndroidRuntime(19631): readGMSProperty: end 
D/AndroidRuntime(19631): addProductProperty: start 
D/dalvikvm(19631): Trying to load lib libjavacore.so 0x0 
D/dalvikvm(19631): Added shared lib libjavacore.so 0x0 
D/dalvikvm(19631): Trying to load lib libnativehelper.so 0x0 
D/dalvikvm(19631): Added shared lib libnativehelper.so 0x0 
D/dalvikvm(19631): No JNI_OnLoad found in libnativehelper.so 0x0, skipping init 
D/dalvikvm(19631): Note: class Landroid/app/ActivityManagerNative; has 194 unimplemented (abstract) 
methods 
D/AndroidRuntime(19631): Calling main entry com.android.commands.am.Am 
D/AndroidRuntime(19631): Shutting down VM 



对于静态的版本,这里是什么在我的XML清单文件:

<receiver android:exported="true" android:name="com.flyingsoftgames.googleplayquery.QueryReceiver "> 
    <intent-filter> 
     <action android:name="com.android.vending.INSTALL_REFERRER" /> 
    </intent-filter> 
</receiver> 


而且,我有两个文件,GooglePlayQuery.java和QueryReceiver.java。 GooglePlayQuery.java:

package com.flyingsoftgames.googleplayquery; 

import org.apache.cordova.CallbackContext; 
import org.apache.cordova.CordovaInterface; 
import org.apache.cordova.CordovaWebView; 
import org.apache.cordova.CordovaPlugin; 

import org.json.JSONArray; 
import org.json.JSONException; 

import android.content.Intent; 

public class GooglePlayQuery extends CordovaPlugin { 
    public static CallbackContext queryCallback = null; 
    public static CordovaInterface cordova = null; 

    @Override public void initialize (CordovaInterface initCordova, CordovaWebView webView) { 
    cordova = initCordova; 
    super.initialize (cordova, webView); 
    } 

    public boolean execute (String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException { 
    if ("getURI".equals(action)) {this.queryCallback = callbackContext;} 
    return true; 
    } 
} 


QueryReceiver.java:

package com.flyingsoftgames.googleplayquery; 

import com.flyingsoftgames.googleplayquery.GooglePlayQuery; 
import org.apache.cordova.PluginResult; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.app.Activity; 
import android.content.ComponentName; 
import android.content.pm.PackageManager; 

import android.util.Log; 

public class QueryReceiver extends BroadcastReceiver { 
    @Override public void onReceive (Context context, Intent intent) { 
    Log.d ("QueryReceiver", "com.android.vending.INSTALL_REFERRER"); // <-- doesn't log this. 
    GooglePlayQuery.queryCallback.sendPluginResult (new PluginResult (PluginResult.Status.OK, intent.toURI())); 

    // Now destroy the broadcast receiver since we don't need it anymore. 
    Activity activity = GooglePlayQuery.cordova.getActivity(); 
    ComponentName receiver = new ComponentName (activity, QueryReceiver.class); 
    PackageManager pm = activity.getPackageManager(); 
    pm.setComponentEnabledSetting (receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 
    } 
} 


我运行这个测试吧:

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.mygame/com.flyingsoftgames.googleplayquery.QueryReceiver --es "referrer" "textinreferrer"

我没有从onReceive响应。



这是我的动态广播接收机版本。我运行这个测试的动态版本:

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.mygame/com.flyingsoftgames.googleplayquery.GooglePlayQuery --es "referrer" "textinreferrer"

onReceive这里没有响应,无论是。有小费吗?

package com.flyingsoftgames.googleplayquery; 

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.json.JSONArray; 
import org.json.JSONException; 

import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.Context; 
import android.content.BroadcastReceiver; 

import android.util.Log; 

public class GooglePlayQuery extends CordovaPlugin { 
    public static CallbackContext queryCallback = null; 
    private BroadcastReceiver receiver = null; 

    @Override public void initialize (CordovaInterface cordova, CordovaWebView webView) { 
    super.initialize (cordova, webView); 
    IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.setPriority (999); 
    intentFilter.addAction ("com.android.vending.INSTALL_REFERRER"); 
    Log.d ("GooglePlayQuery", "initialize"); // <-- runs. 
    receiver = new BroadcastReceiver() { 
     @Override public void onReceive (Context context, Intent intent) { 
     Log.d ("GooglePlayQuery", "com.android.vending.INSTALL_REFERRER"); // <-- No response. 
     queryCallback.sendPluginResult (new PluginResult (PluginResult.Status.OK, intent.toURI())); 
     } 
    }; 
    webView.getContext().registerReceiver (this.receiver, intentFilter); 
    } 

    public boolean execute (String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException { 
    Log.d ("GooglePlayQuery", "execute"); // <-- runs. 
    if ("getURI".equals(action)) this.queryCallback = callbackContext; 
    return true; 
    } 
} 

回答

0

好的。我并不完全确定如何使动态格式实际工作,但似乎静态格式需要通过包管理器重新启用接收器 - 广播接收器不会自动重新启用接收器在应用程序运行时。在我的情况下:

Activity activity = cordova.getActivity(); 
ComponentName receiver = new ComponentName (activity, QueryReceiver.class); 
PackageManager pm = activity.getPackageManager(); 
pm.setComponentEnabledSetting (receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);