2016-08-31 4 views
0

アンドロイドで5秒後にアラームがトリガーされますが、機能しません。 5分後に1分後にトリガされます。どうして? the docsから引用Androidで5秒後にアラームトリガーを作成する

@Override 
public void onReceive(Context context, Intent intent) { 
    PowerManager powerManager=(PowerManager)context.getSystemService(Context.POWER_SERVICE); 

    PowerManager.WakeLock wakeLock= powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"Power Manager"); 

    wakeLock.acquire(); 
    showNotification("Wake up. Alarm Triggered",context); 
} 
public void showNotification(String message ,Context context) 
{ 
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 

public void setAlarm(Context context) 
{ 
    Calendar calendar= Calendar.getInstance(); 
    AlarmManager alarmManager=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

    Intent intent=new Intent(context, Alarm.class); 
    PendingIntent pendingIntent= PendingIntent.getBroadcast(context,0,intent,0); 

    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),5000,pendingIntent); 

} 

}

+0

動作していません。毎分1分後に5秒ごとにトリガされます。なぜですか?誰も解決することができます –

+2

Androidは最小1分にアラームマネージャーのリピート時間を制限しました –

+0

@Sujith Niraikulathan - あなたは正しい.. :)その制限 –

答えて

1

注:API 19(キットカット)アラーム配信以降では不正確である:OSがウェイクアップし、電池使用を最小化するためにアラームをシフトします。厳しい配送保証が必要なアプリケーションをサポートするための新しいAPIがあります。 setWindow(int、long、long、PendingIntent)およびsetExact(int、long、PendingIntent)を参照してください。

アプリがHandlerを使用し、(私は5秒の間隔から想定するものである)の実行中に何らかのアクションを実行する場合:

注:アラームマネージャは、例を対象としていアプリケーションが現在実行されていなくても、特定の時間にアプリケーションコードを実行したい場合があります。通常のタイミング操作(ティック、タイムアウトなど)では、Handlerを使用する方が簡単で効率的です。

は特に、postDelayedまたはsendMessageDelayedがあなたのアプリケーションのための正しい選択かもしれません(ただし、あなたがあなたの主な目的を教えていなかったとして、それは単なる当て推量です)。

0

私はハンドラとコードの変更の下でしたが、それでもそれは1分間のセット販売し... :(

ます。public void setAlarm(コンテキストコンテキスト) { カレンダーカレンダー= Calendar.getInstance(); alarmManager =(alarmManager)context.getSystemService(Context.ALARM_SERVICE);

Intent intent=new Intent(context, Alarm.class); 
    final PendingIntent pendingIntent= PendingIntent.getBroadcast(context,0,intent,0); 

    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),5000,pendingIntent); 
    Handler mHandler = new Handler(); 
    mHandler.postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      alarmManager.cancel(pendingIntent); 
     } 

    }, 60 * 1000); 
} 
1

Praviin、あなたはすぐ下のようなハンドラを使用し、5秒後に何かをトリガしたい場合は、ハンドラ内alarmManagerコードを配置する必要はありません。

final Handler handler = new Handler(); 
         handler.postDelayed(new Runnable() { 
          @Override 
          public void run() { 

           //The code here will run after 5 sec, do what you want 

          } 
         }, 5*1000); 
+0

OK ..私は削除してみてください.. –

関連する問題