2016-12-08 4 views
0

Я стартер в Android. Я хочу создать Service.java из этого Activity.class.создать службу с CountDownTimer в android

В моей основной деятельности у меня есть 2 кнопки (aBTN и bBTN). с нажатием на каждый из них Adult1Activity с запуском таймера. этот таймер управляется с помощью PlayBTN и StopBTN. Кроме того, я хочу показать таймер в уведомлении. Я хочу использовать две другие кнопки «RestoreBTN» и «CloseBTN» для подключения между активностью и уведомлением. Я использую CountDownTimer class для таймера. но я не знаю, как я пишу

  1. TextView
  2. RestoreBTN

Примечание: "aBTN" и ​​"bBTN" имеют разное время. «aBTN» - 10 минут, а «bBTN» - 5 минут.

Этот код является первым.

public class Adult1Activity extends AppCompatActivity implements View.OnClickListener{ 
private Button playBTN,stopBTN ; // contoroll buttons 
private Button OnStop; 
private TextView tv ; 
private Toolbar toolbar; 
private CountDownTimer countDownTimer; 
private int total; 
private long timeBlinkInMilliseconds ; // start time of start blinking 
private boolean blink; // controls the blinking .. on and off 

public CustomDialogClass cdd; // back dialog 

private boolean isRunning = false; 
public String timeString; 
public NotificationManager notificationManager; 
public NotificationCompat.Builder notificationBuilder; 
public final int notification_id=01; 
private static final String EXTRA_NOTE = "NOTE"; 
private static final String NOTE_RESTORE = "restore"; 
private static final String NOTE_CLOSE = "close"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_adult1); 

    //define toolbar 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 



    playBTN = (Button) findViewById(R.id.play); 
    stopBTN = (Button) findViewById(R.id.stop); 
    tv = (TextView) findViewById(R.id.TimeCount); 

    playBTN.setOnClickListener(this); 
    stopBTN.setOnClickListener(this); 

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    handleIntent(getIntent()); 
} 

@Override 
public void onClick(View v) { 
    switch(v.getId()) 
    { case R.id.play: 
     tv.setTextAppearance(getApplicationContext(), 
       R.style.normalText); 
     setTimer(); 
     stopBTN.setVisibility(View.VISIBLE); 
     playBTN.setVisibility(View.GONE); 
     startTimer(total); 
     break; 

     case R.id.stop : 
      countDownTimer.cancel(); 
      Intent reloadIntent = new Intent(Adult1Activity.this, Adult1Activity.class); 
      startActivity(reloadIntent); // refresh activity 
      finish(); 
      isRunning = false; 
      notificationManager.cancel(notification_id); 
      break; 
    } 
} 
//------------------------------------------------ 
private void setTimer() { 
    int time=30; 
    total = time*60 * 1000; // 30*60 aeconds 
    timeBlinkInMilliseconds = 10 * 1000; 
} 


private void startTimer(long enter) { 
    countDownTimer = new CountDownTimer(enter, 500) { 
     // 500 means, onTick function will be called at every 500 
     // milliseconds 

     @Override 
     public void onTick(long leftTimeInMilliseconds) { 
      final long seconds = leftTimeInMilliseconds/1000; 
      long save = leftTimeInMilliseconds/1000; 

      if (leftTimeInMilliseconds < timeBlinkInMilliseconds) { 
       tv.setTextAppearance(getApplicationContext(), 
         R.style.blinkText); 
       // change the style of the textview .. giving a red 
       // alert style 

       if (blink) { 
        tv.setVisibility(View.VISIBLE); 
        // if blink is true, textview will be visible 
       } else { 
        tv.setVisibility(View.INVISIBLE); 
       } 

       blink = !blink; // toggle the value of blink 
      } 

      // format the textview to show the easily readable format 
      timeString = String.format("%02d", seconds/60) 
        + ":" + String.format("%02d", seconds % 60); 
      tv.setText(timeString); 
      ExtendedNotification(timeString); 
     } 

     @Override 
     public void onFinish() { 
      Adult1Activity.this.finish(); 
      vibrate(1000); 
      notificationManager.cancel(notification_id); 

      // turn on screen 
      PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE); 
      PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG"); 
      wakeLock.acquire(); 

      // *********** notification 
      PendingIntent i = PendingIntent.getActivity(getBaseContext(), 0, 
        new Intent(getBaseContext(), Sport2Activity.class),0); 

      finish(); 
     } 
    }.start(); 
} 
//---------------------------------------------------------- 
public void onFinish() { 
    moveTaskToBack(true); 
} 
//-------------vibrate--------------------------------------------- 
public void vibrate(int duration) { 
    Vibrator vibs = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
    vibs.vibrate(duration); 
} 

//**************************************************** 
//alert dialog for back btn 
public void onBackPressed() { 
    cdd = new CustomDialogClass(Adult1Activity.this); 
    cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    cdd.show(); 
} 


public class CustomDialogClass extends Dialog { 
    public Activity c; 
    public Button minimizeBTN, exitBTN; 

    public CustomDialogClass(Activity a) { 
     super(a); 
     this.c = a; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.back_btn); 
     minimizeBTN = (Button) findViewById(R.id.minimizing_app); 
     exitBTN = (Button) findViewById(R.id.exit_completely); 

     minimizeBTN.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       MinimizeApp(); 
       isRunning = true; 
       ExtendedNotification(timeString); 
       cdd.dismiss(); 
       // notificationManager.notify(notification_id,notificationBuilder.build()); 

      } 
     }); 


     exitBTN.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       CloseApp(); 
      } 
     }); 

    } 


} 

//*****************Notification class****** 
    private void ExtendedNotification(String time) { 
     final Intent resultIntentRestore = new Intent(this, Adult1Activity.class); 
     resultIntentRestore.putExtra(EXTRA_NOTE,NOTE_RESTORE); 
     resultIntentRestore.putExtra(time,NOTE_RESTORE); 
     PendingIntent restoreIntent = PendingIntent.getActivity(Adult1Activity.this, 
       0, resultIntentRestore, PendingIntent.FLAG_UPDATE_CURRENT); 

     final Intent resultIntentClose = new Intent(this, Adult1Activity.class); 
     resultIntentClose.putExtra(EXTRA_NOTE, NOTE_CLOSE); 
     PendingIntent closeIntent = PendingIntent.getActivity(Adult1Activity.this, 
       1, resultIntentClose, PendingIntent.FLAG_UPDATE_CURRENT); 

      notificationBuilder = new NotificationCompat.Builder(Adult1Activity.this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("Stand Up!") 
       .setContentText(time) 
       .setAutoCancel(true) 
       .addAction(new NotificationCompat.Action(R.drawable.ic_note_restore, "Restore", restoreIntent)) 
       .addAction(new NotificationCompat.Action(R.drawable.ic_note_close, "Close", closeIntent)) 
       .setContentIntent(restoreIntent); 


     //final Notification notification = notificationBuilder.build(); 
     //notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 
     notificationManager.notify(notification_id,notificationBuilder.build()); 

    } 
@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); // Make sure to call super 
    handleIntent(intent); 
} 

private void handleIntent(Intent intent) { 
    final String a = intent.getStringExtra(EXTRA_NOTE); 
    if (a != null) { 
     switch (a) { 
      case NOTE_RESTORE: 
       tv.setText(timeString); 
       // tv.setText(intent.getStringExtra(timeString)); 
       notificationManager.cancel(notification_id); 
       break; 

      case NOTE_CLOSE: 
       countDownTimer.cancel(); 
       isRunning = false; 
       notificationManager.cancel(notification_id); 
       // Adult1Activity.this.finish(); 
       break; 
     } 
    } 
} 
//**********************App statuse******************** 
public void MinimizeApp(){ 
    moveTaskToBack(true); //hide 

} 

private void CloseApp(){ 
    android.os.Process.killProcess(android.os.Process.myPid());// exit 
    notificationManager.cancel(notification_id); 
} 

}

это мой ForgroundService, что я написал до сих пор.

public class TimerService extends Service { 
public CountingDownTimer countingDownTimer; 
public static String total_time_string; 
public static int totalSeconds; 


public NotificationManager notificationManager; 
public NotificationCompat.Builder notificationBuilder; 
public final int notification_id=01; 
private static final String EXTRA_NOTE = "NOTE"; 
private static final String NOTE_RESTORE = "restore"; 
private static final String NOTE_CLOSE = "close"; 

@Override 
public void onCreate() { 

} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    countingDownTimer = new CountingDownTimer(totalSeconds,500); //milliseconds , (500 means) onTick function will be called at every 500 
    //countingDownTimer = new CountingDownTimer(intent.getExtras().getInt(total_time_string),500); //milliseconds , (500 means) onTick function will be called at every 500 
    countingDownTimer.start(); 

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    setupNotification(); 

    return super.onStartCommand(intent, flags, startId); 
} 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    countingDownTimer.cancel(); 
    notificationManager.cancel(notification_id); 
} 

//****************************************************************** 
public class CountingDownTimer extends CountDownTimer{ 
    final String timeString; 

    /** 
    * @param millisInFuture The number of millis in the future from the call 
    *       to {@link #start()} until the countdown is done and {@link #onFinish()} 
    *       is called. 
    * @param countDownInterval The interval along the way to receive 
    *       {@link #onTick(long)} callbacks. 
    */ 
    public CountingDownTimer(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
     timeString = null; 
    } 

    @Override 
    public void onTick(long leftTimeInMilliseconds) { 
     // format the textview to show the easily readable format 
     final long seconds = leftTimeInMilliseconds/1000; 
     timeString = String.format("%02d", seconds/60) 
       + ":" + String.format("%02d", seconds % 60); 

     Adult1Activity.tv.setText(timeString); 
    // ExtendedNotification(timeString); 

    } 

    @Override 
    public void onFinish() { 
     startActivity(new Intent(this,Sport2Activity.class)); 

     vibrate(1000); 

     // turn on screen 
     PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG"); 
     wakeLock.acquire(); 
    } 
} 

public void vibrate(int duration) { 
    Vibrator vibs = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
    vibs.vibrate(duration); 
} 

//****************Notification ******************** 
    private void setupNotification() { 
     Intent notificationIntent_Restore = new Intent(this,Adult1Activity.class); 
     notificationIntent_Restore.putExtra(EXTRA_NOTE,NOTE_RESTORE); 
     notificationIntent_Restore.putExtra(countingDownTimer.timeString,NOTE_RESTORE); 
     PendingIntent restoreIntent = PendingIntent.getActivity(this, 
       0, notificationIntent_Restore, PendingIntent.FLAG_UPDATE_CURRENT); 

     final Intent notificationIntent_Close = new Intent(this, Adult1Activity.class); 
     notificationIntent_Close.putExtra(EXTRA_NOTE, NOTE_CLOSE); 
     PendingIntent closeIntent = PendingIntent.getActivity(this, 
       1, notificationIntent_Close, PendingIntent.FLAG_UPDATE_CURRENT); 

     notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("Stand Up!") 
       .setContentText(countingDownTimer.timeString) 
       .setAutoCancel(true) 
       .addAction(new NotificationCompat.Action(R.drawable.ic_note_restore, "Restore", restoreIntent)) 
       .addAction(new NotificationCompat.Action(R.drawable.ic_note_close, "Close", closeIntent)) 
       .setContentIntent(restoreIntent); 

     startForeground(notification_id,notificationBuilder); 
     //final Notification notification = notificationBuilder.build(); 
     //notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 
     // notificationManager.notify(notification_id,notificationBuilder.build()); 

    } 
+0

@ сектор11. следует ли использовать «notificationIntent_Restore.setAction ...» и создать для него действие и определить его работу в нем? – Migitanar

+0

привет, я могу создать проект и поделиться им со мной, я буду нажимать обновленные изменения, когда я получу время? – sector11

+0

привет! Я размещаю свои коды здесь. разве они не достаточно? если так, я объясню больше. – Migitanar

ответ

1
private class OTPCountdown extends CountDownTimer { 

      /** 
      * @param millisInFuture The number of millis in the future from the call 
      *       to {@link #start()} until the countdown is done and {@link #onFinish()} 
      *       is called. 
      * @param countDownInterval The interval along the way to receive 
      *       {@link #onTick(long)} callbacks. 
      */ 
      public OTPCountdown(long millisInFuture, long countDownInterval) { 
       super(millisInFuture, countDownInterval); 
      } 

      @Override 
      public void onTick(long millisUntilFinished) { 
       tvOTPCountdown.setText(getString(R.string.otp_expiration_msg, millisUntilFinished/1000)); 
      } 

      @Override 
      public void onFinish() { 
       showManualWrap(); 
      } 
     } 



// use like this 
otpCountdown = new OTPCountdown(60000, 1000); 
     otpCountdown.start(); 
+0

Я хочу поставить таймер обратного отсчета и уведомление в классе обслуживания – Migitanar

+0

@surya не отвечает здесь, она отказалась от моего ответа, когда я не успел ответить здесь. Ссылка здесь http://stackoverflow.com/questions/34853271/how-to-set-service-startforeground-to-work-countdowntimer-правильно/34855177 # 34855177 – sector11

+0

, который отклонен.? :) –

0

Просто потому, что вы стартер в Android и кто-то не получил время, чтобы ответить на ваш вопрос, не означает, что вы бы downvote их принято отвечать и боссу над ними. Если вы не можете понять, что происходит с вашим кодом. Дайте время другим разработчикам, чтобы увидеть это снова. Модератор, пожалуйста, посмотрите на эту тему: How to set Service/StartForeground to work CountDownTimer properly

+0

DO читать комментарии на пост я сказал ей, я буду смотреть, когда я получу время. или если ей нужно срочно, она может где-то поделиться примерным проектом. – sector11

+0

thaks для вашего ответа, который является полезным. Я прочитал этот пост и задал какой-то вопрос, поэтому мне нужна ваша помощь. Я НЕ СОБИРАЮСЬ ВСЕ. Я становлюсь несчастным из-за вашего, хотя. Я думаю, что stackoverflow помогает друг другу, профессионалы должны помочь начать. – Migitanar

+0

Могу ли я попросить вас ответить на это: http://stackoverflow.com/q/41236481/7268725 – Migitanar

 Смежные вопросы

  • Нет связанных вопросов^_^