簡體   English   中英

有沒有辦法在Android中安排重復鬧鍾的結束時間?

[英]Is there a way to schedule the end of the repeating alarm in Android?

我是 Android 開發的新手,我決定創建一個重復的鬧鍾應用程序,您可以在其中選擇循環何時結束,例如 5 次鬧鍾爆發后。 我已經設置了警報和所有這些,我有一個按鈕可以取消警報,但我無法限制它,因此它會在所述警報爆發后自動停止。 有沒有辦法做到這一點? 我希望能夠在EditText窗口中寫下我想要的突發次數,寫下警報之間的延遲,然后按下按鈕進行設置。

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    private Double delay;
    private int howManyTimes;
    private EditText remaining;
    private EditText iterator;

我想在howManyTimes存儲突發量。

我的OnClickListener看起來像這樣(迭代器是一個EditText ,我在其中寫入了突發次數,剩余的是一個EditText ,我在其中寫入了突發之間的延遲):

public void onClick(View v) {
    if (remaining.getText().toString().equals("") || remaining.getText().toString().equals(".")) {
        delay = 0.0;
    } else {
        delay = (60 * 60 * 1000) * Double.parseDouble(remaining.getText().toString());
    }
    if (iterator.getText().toString().equals("") || iterator.getText().toString().equals(".")) {
        howManyTimes = 0;
    } else {
        howManyTimes = Integer.parseInt(iterator.getText().toString());
    }
    if (howManyTimes > 0) {
        double tmpDelay = delay;
        int tmpIterator = howManyTimes;
        updateTimeText(tmpIterator, tmpDelay);
        startAlarm();
    }
}

startAlarm()看起來像這樣:

private void startAlarm() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);


    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + delay.longValue(),
            delay.longValue(), pendingIntent);
}

這是我的廣播接收器:

    public class AlertReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationHelper notificationHelper = new NotificationHelper(context);
        NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
        notificationHelper.getManager().notify(1, nb.build());

    }
}

我認為實現它的一種方法是在持久內存中存儲所需的突發數量以及到目前為止經過的突發數量,即在您的應用程序首選項中。 這樣,即使您在鬧鍾之間關閉應用程序,信息也會保留。 然后只需添加一個 if 語句並在經過的突發數量等於所需突發數量時取消警報。 我會在一秒鍾內添加一些代碼。

在這里,您將 var 保存到共享首選項的次數並將經過的計數器重置為零。

public void onClick(View v) {
    if (remaining.getText().toString().equals("") || remaining.getText().toString().equals(".")) {
        delay = 0.0;
    } else {
        delay = (60 * 60 * 1000) * Double.parseDouble(remaining.getText().toString());
    }
    if (iterator.getText().toString().equals("") || iterator.getText().toString().equals(".")) {
        howManyTimes = 0;
    } else {
        howManyTimes = Integer.parseInt(iterator.getText().toString());
        //here you store the amount of desired bursts to persistent memory and reset elapsed bursts to 0
        SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putInt("desiredBursts",howManyTimes);
            editor.putInt("elapseddBursts",0);
            editor.apply();
    }
    if (howManyTimes > 0) {
        double tmpDelay = delay;
        int tmpIterator = howManyTimes;
        updateTimeText(tmpIterator, tmpDelay);
        startAlarm();
    }
}

那么您需要將以下代碼添加到您的廣播接收器(您沒有包含在您的問題中)。 它將更新經過的脈沖串數量並在經過的脈沖串與所需的脈沖串總數匹配時取消警報。

//this goes in your broadcastreceiver
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(context);
int desiredBursts = sharedPreferences.getInt("desiredBursts", 1);//this will get the desired bursts
int elapsedBursts = sharedPreferences.getInt("elapsedBursts", 0);//this will get the running burst count
if(desiredBursts>elapsedBursts){
    SharedPreferences.Editor editor=sharedPreferences.edit();
    editor.putInt("elapsedBursts",elapsedBursts+1);
    editor.apply();
    //play actual alarmsound here either with soundpool or mediaplayer
}else{
    //cancel the alarm here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM