我有一个用户在设备之间玩的游戏应用程序。我添加了一个服务,它会间歇性地检查远程玩家是否进行了移动,然后通知本地用户现在轮到他或她了。我在游戏的主要活动中使用此代码根据用户对“间歇性”的偏好启动服务
游戏
if (Settings.AlarmInterval != 0) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, (int)( Settings.AlarmInterval/1000/60));
Intent myIntent = new Intent(mCtx, WakeCheck.class);
pendingIntent = PendingIntent.getService(mCtx, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)mCtx.getSystemService(mCtx.ALARM_SERVICE);
alarmManager.cancel(pendingIntent); // cancel any previous alarms
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), Settings.AlarmInterval, pendingIntent);
}
这工作得很好。然而,即使本地用户当前正在玩游戏,该服务也会唤醒并检查是否已经下过棋。我希望仅在当前未玩游戏时通知用户。
如何在我的 WakeCheck 服务中检测到游戏正在运行且无需检查远程播放?
服务
公共类 WakeCheck 扩展服务 {
private triDbAdapter mDbHelper;
private static final int CHALLENGE = -2;
@Override
public void onCreate() {
// TODO Auto-generated method stub
//Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
//Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mDbHelper.close();
//Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Cursor c;
triDbAdapter mDbHelper = new triDbAdapter(this.getApplicationContext());
mDbHelper.open();
ServerCommunication sComm = new ServerCommunication(this.getApplicationContext());
c = mDbHelper.fetchAllGames();
Log.d("WAKE","Checking Game Status");
<snip>
}