2014-11-25 47 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);