📌  相关文章
📜  如何在Android中构建简单的警报设置器应用程序?

📅  最后修改于: 2021-05-08 21:04:01             🧑  作者: Mango

在本文中,我们将了解如何构建一个非常有趣的名为Alarm Setter的应用程序警报在我们的日常生活中起着至关重要的作用。如今,闹铃已成为我们的唤醒助手。每个手机都与一个警报应用程序相关联。我们将使用android studio创建此应用。 Android Studio提供了一个很棒的统一环境来构建适用于Android手机,平板电脑,Android Wear,Android TV和Android Auto的应用程序,因为它提供了大量的应用程序构建功能,并且非常易于使用。下面提供了一个示例视频,以使您对本文中的工作有个大概的了解。注意,我们将使用Java语言实现该项目。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。在此文件中,我们添加了两个项目‘TimePicker’‘ToggleButton’TimePicker用于捕获警报时间,并且添加了ToggleButton来设置警报的开启或关闭。最初,ToggleButton设置为关闭。设置警报时将其设置为开。以下是activity_main.xml文件的代码。

XML


  
    
    
    
  
    
    
    
  
    
      


Java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;
  
import androidx.appcompat.app.AppCompatActivity;
  
import java.util.Calendar;
  
public class MainActivity extends AppCompatActivity {
    TimePicker alarmTimePicker;
    PendingIntent pendingIntent;
    AlarmManager alarmManager;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  
    }
  
    // OnToggleClicked() method is implemented the time functionality
    public void OnToggleClicked(View view) {
        long time;
        if (((ToggleButton) view).isChecked()) {
            Toast.makeText(MainActivity.this, "ALARM ON", Toast.LENGTH_SHORT).show();
            Calendar calendar = Calendar.getInstance();
  
            // calender is called to get current time in hour and minute
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
  
            // using intent i have class AlarmReceiver class which inherits
            // BroadcastReceiver
            Intent intent = new Intent(this, AlarmReceiver.class);
  
            // we call broadcast using pendingIntent
            pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
  
            time = (calendar.getTimeInMillis() - (calendar.getTimeInMillis() % 60000));
            if (System.currentTimeMillis() > time) {
                // setting time as AM and PM
                if (calendar.AM_PM == 0)
                    time = time + (1000 * 60 * 60 * 12);
                else
                    time = time + (1000 * 60 * 60 * 24);
            }
            // Alarm rings continuously until toggle button is turned off
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);
            // alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (time * 1000), pendingIntent);
        } else {
            alarmManager.cancel(pendingIntent);
            Toast.makeText(MainActivity.this, "ALARM OFF", Toast.LENGTH_SHORT).show();
        }
    }
}


Java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Vibrator;
import android.widget.Toast;
  
import androidx.annotation.RequiresApi;
  
public class AlarmReceiver extends BroadcastReceiver {
    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    // implement onReceive() method
    public void onReceive(Context context, Intent intent) {
  
        // we will use vibrator first
        Vibrator vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
        vibrator.vibrate(4000);
  
        Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alarmUri == null) {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
          
        // setting default ringtone
        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
  
        // play ringtone
        ringtone.play();
    }
}


XML


    #0F9D58
    #0F4C2E
    #9D0F9B


XML

    
    
    
  


步骤3:使用MainActivity。 Java文件

转到MainActivity。 Java类。在MainActivity中。Java类onToggleClicked()方法,其中使用日历设置当前小时和分钟。警报服务使用AlarmManager类实现。警报的设置方式是使其响起并反复振动,直到关闭切换按钮为止。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;
  
import androidx.appcompat.app.AppCompatActivity;
  
import java.util.Calendar;
  
public class MainActivity extends AppCompatActivity {
    TimePicker alarmTimePicker;
    PendingIntent pendingIntent;
    AlarmManager alarmManager;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  
    }
  
    // OnToggleClicked() method is implemented the time functionality
    public void OnToggleClicked(View view) {
        long time;
        if (((ToggleButton) view).isChecked()) {
            Toast.makeText(MainActivity.this, "ALARM ON", Toast.LENGTH_SHORT).show();
            Calendar calendar = Calendar.getInstance();
  
            // calender is called to get current time in hour and minute
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
  
            // using intent i have class AlarmReceiver class which inherits
            // BroadcastReceiver
            Intent intent = new Intent(this, AlarmReceiver.class);
  
            // we call broadcast using pendingIntent
            pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
  
            time = (calendar.getTimeInMillis() - (calendar.getTimeInMillis() % 60000));
            if (System.currentTimeMillis() > time) {
                // setting time as AM and PM
                if (calendar.AM_PM == 0)
                    time = time + (1000 * 60 * 60 * 12);
                else
                    time = time + (1000 * 60 * 60 * 24);
            }
            // Alarm rings continuously until toggle button is turned off
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);
            // alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (time * 1000), pendingIntent);
        } else {
            alarmManager.cancel(pendingIntent);
            Toast.makeText(MainActivity.this, "ALARM OFF", Toast.LENGTH_SHORT).show();
        }
    }
}

步骤4:使用BroadCastReceiver(AlarmReceiver)类

创建一个名为“ AlarmReceiver”的新Java类。 Java”位于MainActivity所在的位置。 Java类驻留。在此类中,实现了onReceive()方法。在这里,我们添加了振动功能和默认铃声,该铃声会在计划闹钟时间后开始振动并响起。下面是AlarmReceiver的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Vibrator;
import android.widget.Toast;
  
import androidx.annotation.RequiresApi;
  
public class AlarmReceiver extends BroadcastReceiver {
    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    // implement onReceive() method
    public void onReceive(Context context, Intent intent) {
  
        // we will use vibrator first
        Vibrator vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
        vibrator.vibrate(4000);
  
        Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alarmUri == null) {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
          
        // setting default ringtone
        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
  
        // play ringtone
        ringtone.play();
    }
}

步骤5:玩颜色

首先转到“值”文件夹,然后选择colors.xml文件。在colors.xml文件中,您可以保留想要选择的颜色,最多可以在您的应用程序中使用。您只需要给出名称并放入相应颜色的颜色代码即可。我将AppBar颜色保持为“#0F9D58” ,我们将其命名为“ colorPrimary”。

XML格式



    #0F9D58
    #0F4C2E
    #9D0F9B

步骤6:变更应用程式的主题

首先转到“值”文件夹,然后选择themes.xml文件。在theme.xml文件中,我们使用了“ Theme.AppCompat.Light.DarkActionBar” ,它是带有深色ActionBar的浅色主题。我们可以通过“ Theme.AppCompat.Light.LightActionBar”将灯光主题与灯光动作栏一起使用,这完全取决于我们的选择和需求。

XML格式


    
    
    
  

步骤7:在“ AndroidManifest.xml”中添加权限

转到“ AndroidManifest.xml”文件。在应用程序部分结束后,通过添加接收器部分,在AndroidManifest.xml中注册了BroadcastReceiver。另外,请允许使用以下方法进行振动:

输出:

在这里,我们手动设置了闹钟时间。您也可以通过调节前面显示的时钟进行设置。您将不得不等待闹钟时间。它将连续显示“警报!醒来!醒来!”并响起并振动,直到关闭切换按钮。您可以在下面的给定GitHub链接上获取源代码:https://github.com/Babitababy/Alarm_setter