gpt4 book ai didi

java - 无法使 Google Cloud Message 保持 Activity 状态

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:10:37 27 4
gpt4 key购买 nike

我正在尝试解决 gcm 超时问题,关于这个主题有很多话题,这里是 one供引用。

建议的解决方法是以比 tcp 超时更短的间隔广播一对 Intent 。

我的实现是创建一个扩展 CountDownTimer 类的类,并在现有服务中保存该类的一个实例。这个派生类在完成后自行重启,服务被标记为 STICKY_START,所以一旦启动,我认为它应该每 4 分钟继续广播一次 Intent ,但由于某种原因存在间隙,当计数器不广播 Intent ,我仍然与 GCM 服务器失去联系。

下面是两个相关的类。谁能解释并提供解决方案,说明为什么此策略不起作用?

我创建了一个扩展 CounDownTimer 的类,它应该每 4 分钟广播一次 Intent 。

public class GcmKeepAlive extends CountDownTimer {

protected CountDownTimer timer;
protected Context mContext;
protected Intent gTalkHeartBeatIntent;
protected Intent mcsHeartBeatIntent;


public GcmKeepAlive(Context context) {
super(4*60* 1000,4*60*1000);
mContext = context;
gTalkHeartBeatIntent = new Intent("com.google.android.intent.action.GTALK_HEARTBEAT");
mcsHeartBeatIntent = new Intent("com.google.android.intent.action.MCS_HEARTBEAT");
System.out.println("stariing heartbeat countdown timer");
this.start();
}


@Override
public void onTick(long millisUntilFinished) {

}

@Override
public void onFinish() {
System.out.println("sending heart beat to keep gcm alive");
mContext.sendBroadcast(gTalkHeartBeatIntent);
mContext.sendBroadcast(mcsHeartBeatIntent);
this.start();

}

}

这是我的应用程序中的服务,它包含 GcmKeepAlive 类的实例

导入android.app.Service;导入 android.content.Intent;导入android.os.IBinder;

public class LocationMonitorService extends Service {

private DeviceLocationClient deviceLocationClient;
private GcmKeepAlive gcmKeepAlive;

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("creating the LocationMonitorService");
deviceLocationClient = new DeviceLocationClient(this);
gcmKeepAlive = new GcmKeepAlive(this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("inside service making request for location updates");
deviceLocationClient.requestLLocationUpdates();
gcmKeepAlive.start();
return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}


}

这是一个在 logcat 中看到的差距示例。

07-13 14:59:05.583 I/System.out(21651): sending heart beat to keep gcm alive
07-13 15:03:05.640 I/System.out(21651): sending heart beat to keep gcm alive
07-13 15:07:05.776 I/System.out(21651): sending heart beat to keep gcm alive
07-13 15:11:05.922 I/System.out(21651): sending heart beat to keep gcm alive
07-13 15:27:31.994 I/System.out(21651): sending heart beat to keep gcm alive

最佳答案

我实际上在前一段时间解决了这个问题,Erik Z 最近的评论让我发布了我的解决方案。

我通过创建一个触发广播的循环警报解决了这个问题,该广播创建并广播了 Intent 。这些差距由于原始服务被终止然后由于 START_STICKY 标志而重新启动造成的。

这是各个部分(从各种文件中提取)

这至少是 kitkat 之前需要的,我不知道是否仍然需要,我想是的。但是我没有关闭它来确认。


警报管理器、 Intent 和未决 Intent 。

AlarmManager alarmManager = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE);

Intent gcmKeepAliveIntent = new Intent("com.gmail.npnster.ourlatitude.gcmKeepAlive");

PendingIntent gcmKeepAlivePendingIntent = PendingIntent.getBroadcast(mContext, 0, gcmKeepAliveIntent, PendingIntent.FLAG_CANCEL_CURRENT);

alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, 4*60*1000, gcmKeepAlivePendingIntent);

广播接收器:

public class GcmKeepAliveBroadcastReceiver extends BroadcastReceiver {

private GcmKeepAlive gcmKeepAlive;

@Override
public void onReceive(Context context, Intent intent) {
MyLog.p(this,"inside gcm keep alive receiver");
gcmKeepAlive = new GcmKeepAlive(context);
gcmKeepAlive.broadcastIntents();

}

}

发送保活广播的保活类。

public class GcmKeepAlive  {

protected Context mContext;
protected Intent gTalkHeartBeatIntent;
protected Intent mcsHeartBeatIntent;

public GcmKeepAlive(Context context) {
mContext = context;
gTalkHeartBeatIntent = new Intent(
"com.google.android.intent.action.GTALK_HEARTBEAT");
mcsHeartBeatIntent = new Intent(
"com.google.android.intent.action.MCS_HEARTBEAT");
}

public void broadcastIntents() {
MyLog.p(this,"sending heart beat to keep gcm alive");
mContext.sendBroadcast(gTalkHeartBeatIntent);
mContext.sendBroadcast(mcsHeartBeatIntent);
}

}

关于java - 无法使 Google Cloud Message 保持 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24726416/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com