当有人单击通知时,计时器停止计时

问题描述

每当我在他的通知中单击时,我的天文钟就会重置,我真的对修复它的想法不再有任何想法。 我希望在可能的情况下在同一通知中实现计时码表控件的帮助 因为该应用程序即将完成,但是这个天文台每天都在折磨我

chronometer.xml

   <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#68a39d"
android:theme="@style/AppTheme"
tools:context=".chronometer"

>

<TextView
    android:id="@+id/textView9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="136dp"
    android:layout_marginLeft="136dp"
    android:layout_marginTop="360dp"
    android:layout_marginEnd="141dp"
    android:layout_marginRight="141dp"
    android:layout_marginBottom="316dp"
    android:fontFamily="@font/bahnschrift"
    android:text="Smilink"
    android:textColor="#FFFFFF"
    android:textSize="40dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ToggleButton
    android:id="@+id/StartBtn"
    android:layout_width="119dp"
    android:layout_height="115dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="188dp"
    android:background="@drawable/play_button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView6"
    app:layout_constraintVertical_bias="1.0" />

<Button
    android:id="@+id/ResetBtn"
    android:layout_width="106dp"
    android:layout_height="102dp"
    android:background="@drawable/ic_baseline_refresh_24"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.868"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.724" />

<ImageView
    android:id="@+id/imageView6"
    android:layout_width="401dp"
    android:layout_height="438dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.054"
    app:srcCompat="@drawable/quadro" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="315dp"
    android:layout_height="60dp"
    android:layout_marginTop="24dp"
    android:text="Continue firme no tratamento hein ! :)"
    android:textColor="#000000"
    android:textSize="20sp"
    app:layout_constraintBottom_toBottomOf="@+id/imageView6"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/mychronometer"
    app:layout_constraintVertical_bias="0.0" />

<Button
    android:id="@+id/backbutton"
    android:layout_width="68dp"
    android:layout_height="68dp"
    android:background="@drawable/ic_baseline_arrow_back_24"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.046"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.049" />

<Chronometer
    android:id="@+id/mychronometer"
    android:layout_width="224dp"
    android:layout_height="97dp"
    android:paddingVertical="15dp"
    android:textColor="#000000"
    android:textSize="50sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.561"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.264" />

<ToggleButton
    android:id="@+id/notificacoes"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginStart="30dp"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="462dp"
    android:layout_marginEnd="281dp"
    android:layout_marginRight="281dp"
    android:layout_marginBottom="169dp"
    android:background="@drawable/notification"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="37dp"
    android:layout_marginLeft="37dp"
    android:layout_marginTop="562dp"
    android:layout_marginEnd="289dp"
    android:layout_marginRight="289dp"
    android:layout_marginBottom="149dp"
    android:text="Notificações"
    android:textColor="#FFFFFF"
    android:textSize="15dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

chronometer.java

public class chronometer extends AppCompatActivity {

private static char channel;
private static final String CHANNEL_ID = null;
private android.widget.Chronometer chronometer;
private long PauSEOffSet = 0;
private boolean isPlaying = false;
private ToggleButton toggleButton;
private Button reset_btn;
private Button backbutton;
private long elapsedtime;

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel);
        String description = getString(R.string.description);
        int importance = notificationmanager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name,importance);
        channel.setDescription(description);
        notificationmanager notificationmanager = getSystemService(notificationmanager.class);
        notificationmanager.createNotificationChannel(channel);
    }
}

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chronometer);


    Intent intent = new Intent(this,chronometer.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,intent,0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID)
            .setSmallIcon(R.drawable.timer_notification)
            .setContentTitle("My Smilink")
            .setContentText("Acesso rápido ao app")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);
    notificationmanagerCompat notificationmanager = notificationmanagerCompat.from(this);
    char notificationId = channel;
    notificationmanager.notify(channel,builder.build());
        //configuração do botão de voltar
        backbutton = (Button) findViewById(R.id.backbutton);
        backbutton.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                opendashboard();
            }

            public void opendashboard() {
                Intent intent = new Intent(chronometer.this,dashboard.class);
                startActivity(intent);
            }
        });

        chronometer = findViewById(R.id.mychronometer);
        chronometer.setonChronometerTickListener(new android.widget.Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(android.widget.Chronometer chronometer) {
                long time = SystemClock.elapsedRealtime() - chronometer.getBase();
                chronometer.setBase(time);
                chronometer.start();
                int h = (int) (time / 3600000);
                int m = (int) (time - h * 3600000) / 60000;
                int s = (int) (time - h * 3600000 - m * 60000) / 1000;
                String t = (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
                chronometer.setText(t);
            }
        });
        chronometer.setBase(SystemClock.elapsedRealtime());
        chronometer.setText("00:00:00");
        toggleButton = findViewById(R.id.StartBtn);
        reset_btn = findViewById(R.id.ResetBtn);

        toggleButton.setText(null);
        toggleButton.setTextOn(null);
        toggleButton.setTextOff(null);
        toggleButton.setonCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean b) {
                //b representa o botão,será verdadeiro "true" caso apertado,caso não,será falso"false"
                if (b) {
                    chronometer.setBase(SystemClock.elapsedRealtime() - PauSEOffSet);
                    elapsedtime = SystemClock.elapsedRealtime() - chronometer.getBase();
                    chronometer.start();
                    isPlaying = true;
                } else {
                    chronometer.stop();
                    PauSEOffSet = SystemClock.elapsedRealtime() - chronometer.getBase();
                    isPlaying = false;


                }
            }
        });
        //Configuração do botão de resetar o cronômetro
        reset_btn.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isPlaying) {
                    chronometer.setBase(SystemClock.elapsedRealtime());
                    PauSEOffSet = 0;
                    chronometer.start();
                    isPlaying = true;
                }
            }
        });
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)