以下是PushListenerService的一部分 - 其中android侦听正在接收的GCM消息。我试图通过webview实现一个javascript函数(fireRadius)。 webview已通过主活动创建。目的是让JS函数更新webview而不重新加载它。
我怀疑两件事中的任何一件都不对劲:
这不会导致任何错误。 JS函数根本不运行。
import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.amazon.mysampleapp.R;
import com.amazonaws.mobile.MainActivity;
import com.google.android.gms.gcm.GcmListenerService;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.amazonaws.mobile.util.ThreadUtils.runOnUiThread;
/**
* A service that listens to GCM notifications.
*/
public class PushListenerService extends GcmListenerService {
private static final String LOG_TAG = PushListenerService.class.getSimpleName();
// Intent action used in local broadcast
public static final String ACTION_SNS_NOTIFICATION = "sns-notification";
// Intent keys
public static final String INTENT_SNS_NOTIFICATION_FROM = "from";
public static final String INTENT_SNS_NOTIFICATION_DATA = "data";
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs. For Set of keys use
* data.keySet().
*/
@Override
public void onMessageReceived(final String from, final Bundle data) {
String message = getMessage(data);
//Log.d(LOG_TAG, "From: " + from);
Log.d(LOG_TAG, "Message: " + message);
//TODO create a popup where the user is
runOnUiThread(new Runnable() {
public void run() {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.activity_main, null);
final WebView webview = (WebView) view.findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setGeolocationEnabled(true);
webview.setEnabled(true);
webview.setClickable(true);
Log.d("DID IT WORK?", "YEP!!");
webview.loadUrl("file:///android_asset/MyMap.html");
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
webview.loadUrl("javascript:fireRadius(" +
43.0000000 + "," +
-79.4000000 + "," +
300 + ",\"" +
"A CIRCLE!" + "\")");
}
});
}
});
// Display a notification in the notification center if the app is in the background.
// Otherwise, send a local broadcast to the app and let the app handle it.
if (isForeground(this)) {
// broadcast notification
broadcast(from, data);
} else {
displayNotification(text);
}
}
}