我正在努力解决gcm超时问题,这个主题有很多主题,这里有one供参考。
建议的解决方法是以比tcp超时短的间隔广播一对意图。
我的实现是创建一个扩展CountDownTimer类的类,并在现有服务中保存该类的实例。这个派生类在完成后重新启动并且服务被标记为STICKY_START,所以一旦启动,我认为它应该每4分钟继续广播意图,但由于某种原因存在间隙,当计数器没有广播时意图和我仍然与GCM服务器失去联系。
以下两个相关课程。任何人都可以解释并提供解决方案,为什么这个策略不起作用?
我创建了一个扩展CounDownTimer的类,它应该每4分钟广播一次意图。
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类的实例
import android.app.Service; import android.content.Intent; import 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
答案 0 :(得分:3)
我实际上是在前一段时间解决了这个问题,Erik Z最近的评论让我发布我的解决方案。
我通过创建一个触发广播的定期闹钟来解决这个问题,广播创建并广播意图。由于START_STICKY标志导致原始服务被杀死然后重新启动,导致间隙。
以下是各个部分(从各种文件中提取)
这至少需要pre-kitkat,我不知道是否还需要它,我认为是。但我没有把它关掉以确认。
警报管理员,意图和未决意图。
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);
}
}