2017-02-10 4 views
2

これは、毎日午後3時に電話をかけるために私のコードです:Androidで60分間隔で午前12時から午後6時まで、毎日仕事をスケジュールするにはどうすればよいですか?

long INTERVAL_MSEC = 24 * 60 * 60 * 1000; 

Date date2am = new java.util.Date(); 
date2am.setHours(15); 
date2am.setMinutes(0);   

Timer timer = new Timer(); 
TimerTask task = new TimerTask() { 
    public void run() { 
     Intent callIntent = new Intent(Intent.ACTION_CALL); 
     callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     String finalPhonenumber = Globals.sms1PhoneNumbers.get(rad).number + getResources().getString(R.string.extention); 
     callIntent.setData(Uri.parse("tel:" + finalPhonenumber)); 
     startActivity(callIntent); 
    } 
}; 

timer.scheduleAtFixedRate(task, date2am, INTERVAL_MSEC); 

は今、私は60分間隔で午後6時まで午前12時との間の通話を毎日行う必要があります - どのように私はこれを達成することができますか?あなたがその下に作成したアラームを作成する必要が

+3

はAlarmManager –

+0

を使用しますが、私はアラームマネージャで継続時間を定義することができると確信しています。 – HRCJ

+0

Firebase Job Dispatcherを見てくださいhttps://github.com/firebase/firebase-jobdispatcher-android –

答えて

1

は午前12時または00:00に実行されます:

private void startat12() { 
    AlarmManager alarmManager; 
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    Calendar c = Calendar.getInstance(); 
    c.set(Calendar.HOUR_OF_DAY, 00); 
    c.set(Calendar.MINUTE, 00); 

    Long milliseconds = c.getTimeInMillis(); 
    Long daily = 24L * 60L * 60L * 1000L; 

    //check if the time is already passed 
    if (milliseconds < System.currentTimeMillis()) { 
     //if already passed then push it for next day by adding just 24 hrs 
     milliseconds = milliseconds + daily; 
    } 
    Intent intent = new Intent(YourActivity.this, MyReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(YourActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     alarmManager.setExact(AlarmManager.RTC_WAKEUP, milliseconds, pendingIntent); 
    } else { 
     alarmManager.set(AlarmManager.RTC_WAKEUP, milliseconds, pendingIntent); 
    } 
    String dateFormat = "dd/MM/yyyy HH:mm"; 
    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); 
    Toast.makeText(this, "Alarm is set for " + formatter.format(milliseconds), Toast.LENGTH_SHORT).show(); 
} 

今すぐあなたのMyReceiver 00:00翌日 コードのために放送を受信しますあなたの受信機は次のようになります。

public class MyReceiver extends BroadcastReceiver { 

PowerManager powerManager; 
PowerManager.WakeLock wakeLock; 
AlarmManager alarmManager; 
Calendar c; 
SimpleDateFormat formatter; 
String dateFormat; 


@Override 
public void onReceive(final Context context, Intent intent) { 
    //acquire wake lock 
    powerManager = (PowerManager) context.getSystemService(POWER_SERVICE); 
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag"); 
    wakeLock.acquire(); 

    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    c = Calendar.getInstance(); 
    c.set(Calendar.HOUR_OF_DAY, 12); 
    c.set(Calendar.MINUTE, 00); 
    Long daily = 24L * 60L * 60L * 1000L; 
    //Set Unlocked notification broadcast 
    Intent intentnew= new Intent(context, MyService.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentnew, PendingIntent.FLAG_UPDATE_CURRENT); 

    dateFormat = "dd/MM/yyyy HH:mm"; 
    formatter = new SimpleDateFormat(dateFormat); 
    Toast.makeText(context, "Alarm is set for " + formatter.format(c.getTimeInMillis()+daily), Toast.LENGTH_SHORT).show(); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     alarmManager.setExact(AlarmManager.RTC_WAKEUP, (c.getTimeInMillis() + daily), pendingIntent); 
    } else { 
     alarmManager.set(AlarmManager.RTC_WAKEUP, (c.getTimeInMillis() + daily), pendingIntent); 
    } 

    final Handler mHandler = new Handler(); 

    Runnable mRunnable = new Runnable() { 
     @Override 
     public void run() { 
      //DO your Work for each Hour 
      Intent callIntent = new Intent(Intent.ACTION_CALL); 
      callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      String finalPhonenumber = Globals.sms1PhoneNumbers.get(rad).number + context.getResources().getString(R.string.extention); 
      callIntent.setData(Uri.parse("tel:" + finalPhonenumber)); 
      context.startActivity(callIntent); 
      Toast.makeText(context, "It Ran", Toast.LENGTH_SHORT).show(); 
      //Also post your mHandler for next hour 

      if (System.currentTimeMillis() < c.getTimeInMillis()) { 
       mHandler.postDelayed(this, 60L * 60L * 1000L); 
       Toast.makeText(context, "Task will repeat after an hour", Toast.LENGTH_SHORT).show(); 
      } 

     } 
    }; 

    c = Calendar.getInstance(); 
    c.set(Calendar.HOUR_OF_DAY, 18); 
    c.set(Calendar.MINUTE, 00); 
    mHandler.post(mRunnable); 

    wakeLock.release(); 
} 

}

そして、あなたのマニフェストで復帰ロックの権限が含まれています:

<uses-permission android:name="android.permission.WAKE_LOCK"/> 

もあなたの受信機:

<receiver android:name=".Notificationtwo.MyReceiver"/> 
+1

ありがとうございました。あなたは私の日を救った! – HRCJ

関連する問題