1

私はここSOや他のウェブサイトのいくつかの質問からいくつかのコードを取り、合理的な解決策を思いつきました。

私がやろうとしていること: 2分間操作がないと、アプリをシャットダウンする必要があります。つまり、アプリが「onPause」に入ったときにアラームサービスを開始し、アプリが「onResume」に入ったときにサービスをキャンセルするという考え方です。

私が現在持っているもの:これが現在実行されている関連コードです。私の問題は、TimeoutServiceJavaファイルが「onCreated」にならないことです。単にAlarmManager.setを呼び出すだけでは、保留中のインテントは起動しませんか?

Timeout.javaファイル

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Timeout 
{
    private static final int REQUEST_ID = 0;
    private static final long DEFAULT_TIMEOUT = 2 * 60 * 1000;  // 2 minutes

    public static final String INTENT_TIMEOUT = "timeoutintent";

    public static void start(Context ctx) 
    {
        //long triggerTime = System.currentTimeMillis() + DEFAULT_TIMEOUT;
        long triggerTime = System.currentTimeMillis() + (5000);
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ctx, TimeoutService.class);
        PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, 0);
        am.set(AlarmManager.RTC, triggerTime, pi);
    }

    public static void cancel(Context ctx) 
    {
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ctx, TimeoutService.class);
        PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, 0);
        am.cancel(pi);
    }

}

LockingActivityファイル。これは、私のすべてのアクティビティのスーパークラスとして使用されます。

import android.app.Activity;
import android.widget.Toast;

public class LockingActivity extends Activity
{
    @Override
    protected void onPause() 
    {
        super.onPause();
        Timeout.start(this);
    }

    @Override
    protected void onResume() 
    {
        super.onResume();
        Timeout.cancel(this);
        checkShutdown();
    }

    private void checkShutdown() 
    {
        if ( AppVM.isShutDown() )
        {
            Toast.makeText(this, "Shuting Down", 1).show();
            finish();
        }
    }
}

TimeoutServiceファイルを送信することもできますが、これは単なる典型的なサービスファイルです。問題は、TimeoutServiceクラスがインスタンス化されていないことです。そのため、問題がそのクラスにあるとは想像できません。

4

2 に答える 2

2

あなたはアラームで物事を複雑にしていると思います。Handlerとを使用して、すべてメインアクティビティで2分以内postdelayed()に設定します。Runnableユーザーが操作すると、投稿がキャンセルされ、次の2分間に新しい投稿が設定されます。ランナブルに必要なのはyourActivity.finish();

ここでこの答えに従ってください:タイマーの例とコールバックを削除する方法について、45秒間カウントし、20秒間一時停止してから、別のタイトルで繰り返します。

于 2011-05-23T17:46:30.243 に答える
0

こんにちは保留中のインテントにPendingIntent.FLAG_CANCEL_CURRENTフラグを使用してみてください。

 PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent,PendingIntent.FLAG_CANCEL_CURRENT);

アラームマネージャをRTC_WAKEUPにして、デバイスがどこかでオフになっている場合にデバイスをウェイクアップして確認します。

これを使用すると、ブロードキャストレシーバーを登録し、アラームが鳴ったときに何らかのアクションでインテントを起動する必要があります。アクションを、保留中のインテントに使用しているのと同じインテントに設定します。受信機でサービスを開始します。このブロードキャストレシーバーは、アラームがオフになったときにこのインテントによって起動されたアクションをリッスンできる必要があります。

インテントインテント=newIntent(SCH_ALARM_ACTION); intent.setClass(getBaseContext()、SchAlarmReciever.class); registerReceiver(new SchAlarmReciever()、new IntentFilter(SCH_ALARM_ACTION)); PresidentingIntent pi = PresidentingIntent.getBroadcast(getBaseContext()、0、intent、PendingIntent.FLAG_CANCEL_CURRENT);

于 2011-05-23T17:46:23.363 に答える