28

AlarmManager に問題があり、繰り返しアラームをスケジュールするためのコードを設定しました。アプリケーションを実行した後、アラームは正常に動作します。ホームボタンをクリックしても(そしてアプリケーションが一時停止しても)、アラームはその間隔で実行されます。

問題は、タスク マネージャーを開いてアプリケーションを強制終了すると、アラームが停止することです。

これは正常な動作ですか?これを回避し、アプリケーションを閉じた後もアラームを実行し続ける方法はありますか?

コードは以下のとおりです。このメソッドは、ApplicationContext クラス onCreate() によって呼び出されます。

 private void scheduleAlarm() {
  if (alarmScheduled == true) { return; } // we only need to schedule once.

  int alarmInterval = Def.pref(getApplicationContext()).getInt("alarmInterval", 30);

  final Intent intent = new Intent(getApplicationContext(), CollectorAlarmReceiver.class);
  final PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

  AlarmManager alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

  alarmMgr.cancel(pending); // cancel others.

  alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000,
    alarmInterval*1000, pending);

  Def.log(TAG,"scheduleAlarm(): alarm scheduled, interval: "+alarmInterval+" seconds");
  alarmScheduled = true;
 }

受信者コード:

public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "CollectorAlarmReceiver invoked, starting CollectorService in background");

    context.startService(new Intent(context, CollectorService.class));

    Intent collectorService = new Intent(context,CollectorService.class);
    collectorService.putExtra("action", CollectorService.ACTION_BACKGROUND_REQUEST_MESSAGES);

    context.sendBroadcast(collectorService);
}

ありがとう!

4

7 に答える 7

9

@GSreeは間違っていると思います。これを実現する簡単な方法があります。カスタムアクションを使用するだけです。方法は次のとおりです。

最初に、次のようなカスタム アクション名を定義します。

public static final String MY_ACTION = "com.sample.myaction"

次に、BroadcastReceiver を作成します。

public class MyAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(MY_ACTION)) {
            // Do something here
        }
    }    
}

AndroidManifest.xml にレシーバーを登録します。

<receiver android:name="com.sample.MyAlarmReceiver">
    <intent-filter>
        <action android:name="com.sample.myaction"/>
    </intent-filter>
</receiver>

次に、アラームを設定するには、次の PendingIntent を使用します。

Intent i = new Intent(MY_ACTION);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);

他の方法があるかもしれませんが、この解決策を試してみたところ、シェルからアプリを強制終了しても BroadcastReceiver が呼び出されます。

このソリューションでは、サービスを作成する必要がないことに注意してください。

于 2014-01-04T14:39:46.900 に答える
9

これは正常な動作です。ユーザーが自発的にアプリケーションを強制停止した場合は、アプリケーションを停止する必要があります。それ以外の場合は、ウイルスのようなアプリケーションを作成しています。

本当に必要な場合は、他のサービスが実行されているかどうかを監視し、実行されていない場合はサービスを実行する別のサービスを作成できます。しかし、これは別のアプリケーションであり、ユーザーがタスク マネージャーを使用してこのアプリを強制終了することはありません。

個人的には心配いりません。ユーザーがそれを停止した場合、ユーザーはそれを停止したいと考えました。ユーザーを困らせないでください。

于 2011-01-10T05:44:44.810 に答える
0

それは正常な動作です ! アプリが onDestroy() に移行すると、アラームは機能しなくなります。

于 2016-08-29T10:03:41.307 に答える
0

私はあなたが必要とすることを正確に行うことができました. アプリケーション マネージャー ツールの [実行中] タブでアプリケーションを停止しても、サービスは自動的に再起動します。これを強制終了する唯一の方法は、アプリケーション マネージャーのアンインストール オプションの横にある強制停止ボタンを使用することです。基本的に、Service を直接呼び出します。これが私のコードです:

public class TestService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Ejecutando Servicio ...", Toast.LENGTH_SHORT).show();
    return Service.START_NOT_STICKY;    
    }

@Override
    public void onCreate() {
      super.onCreate();
      Toast.makeText(this, "Iniciando Servicio ...", Toast.LENGTH_SHORT).show();
}

    @Override
    public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this, "Deteniendo Servicio ...", Toast.LENGTH_SHORT).show();
}   

クライアント アクティビティで、次のコードを記述します。

            Calendar cal = Calendar.getInstance();
        Intent intent = new Intent(this,TestService.class);
        PendingIntent pIntent = PendingIntent.getService(this.getApplicationContext(), 0, intent, 0);
        alarm=  (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), 3*1000, pIntent);

マニフェストでサービスを宣言します。

        <service
      android:name="TestService"
      android:icon="@drawable/ic_launcher"
      android:label="TestService"
      >
    </service>         
于 2013-08-21T20:20:59.617 に答える
-1

追加

<receiver android:name=".AlarmReceiver" android:process=":remote" />

マニフェスト ファイルで。アプリが強制終了されてもアラームは機能します。

于 2016-01-01T13:23:46.810 に答える
-1

アプリケーションを強制的に閉じることは、アプリを閉じる正しい方法ではなく、正しくない動作を引き起こす可能性があります。

この質問も参考になるかもしれません。

于 2011-01-10T06:28:27.797 に答える