当应用程序从GCM收到消息时,如何刷新webView

时间:2014-12-23 09:39:21

标签: android webview google-cloud-messaging

我可以通过以下代码成功接收推送通知

public class GcmIntentService extends IntentService {

public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

Toast toast;

public GcmIntentService() {
    super("GcmIntentService");
}

public static final String TAG = "GCM";

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
        /*
         * Filter messages based on message type. Since it is likely that GCM will be
         * extended in the future with new message types, just ignore any message types you're
         * not interested in, or that you don't recognize.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification("Deleted messages on server: " + extras.toString());
        // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // This loop represents the service doing some work.
            for (int i = 0; i < 5; i++) {
                Log.i(TAG, "Working... " + (i + 1)
                        + "/5 @ " + SystemClock.elapsedRealtime());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }
            }
            Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());

            String msg = extras.getString("message");
            sendNotification(msg);
            Log.i(TAG, "Received: " + msg);

        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_stat_gcm)
    .setContentTitle("InnoProperty Notification")
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(msg))
    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();

     }
   }

app在MainActivity中有一个webView。

我想在我的应用收到推送消息时刷新webView,但我不知道如何在GCMIntentService类中找到webView。

1 个答案:

答案 0 :(得分:2)

这不是经过测试,只是逻辑而且可能有拼写错误

这是GcmIntentService

public GcmIntentService(View yourWebView) { 
    super("GcmIntentService"); 
} 

在实例化GcmIntentService类对象时调用此构造函数。

我认为可以解决的另一个解决方案是制作公共课

public class myWebView extends WebView
{
 // Implement other methods..
..

public static void randomRefresh()
{
 //refresh you browser here
}
}

创建一个扩展Application

的全局类
public class GlobalClass extends Application{

    private myWebView name;


    public myWebView getWebview() {

        return name;
    } 

    public void setWebView(myWebView aName) {

       name = aName;

    } 
} 

只需将webview设置为全局,然后随时随地访问它。

我不知道这是一个很好的实现,但这就是我的全部。

修改

所以,我认为第二种解决方案更好,这是代码

上课 myWebview

public class myWebview扩展了WebView {

static WebView mWebView;
public myWebview(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    mWebView=this;
}

public static void refreshView()
{
    mWebView.reload();
}

}

创建一个 globalclass

public class globalclass extends Application {

    myWebview webview;

    private myWebview name;


    public myWebview getWebview() { 

        return name;
    }  

    public void setWebView(myWebview aName) {

       name = aName;

    } 
}

不要忘记编辑Android清单

<application
        android:name="com.example.blabla.globalclass"
...
...
...

在webview所在的MainActivity中

final globalclass globalc = (globalclass) getApplicationContext(); 
        globalc.setWebView(YourWebviewHere);

然后在要刷新的GCMIntentService中

final globalclass gc = (globalclass) getApplicationContext();
            gc.getWebview().reload();

这是我能帮助他们理解的最好方法。

希望它会有所帮助。