1

ユーザーがデバイス間でプレイするゲームアプリケーションがあります。リモートプレイヤーが移動したかどうかを断続的にチェックし、ローカルユーザーに今度は自分の番であることを通知するサービスを追加しました。ゲームのメインアクティビティでこのコードを使用して、ユーザーの「断続的」な設定に基づいてサービスを開始します

ゲーム

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はServiceを拡張します{

    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>
}
4

1 に答える 1

0

今年のGoogleI/ Oで誰かがこのアイデアを発表しました...ゲームアクティビティがフォアグラウンドになると(たとえば、onResume())、SharedPreferencesのブール値をtrueに設定して、ユーザーがプレイしていることを示します。WakeCheckはこの値を読み取り、それがtrueの場合、リモート再生をチェックする必要はありません。ゲームアクティビティがバックグラウンドに移行すると(たとえば、onPause())、値をfalseに設定して、リモートプレイをチェックする必要があることをWakeCheckに通知します。

于 2012-07-15T03:53:40.103 に答える