用Android studio做一个可以运行的小程序。

安妮果果33 2021-09-18 16:39 350 次浏览 赞 105

最新问答

  • 雨田里得麦圈

    环境搭建就不讲了,直接说开发。

    小闹钟程序开发中的要点就是:

    1、时间选择对话框(TimePicker)

    2、获取闹钟管理器并对其进行设置

    3、注册广播接收器

    掌握了这两点,写程序就很简单了。

    1、新建android项目:Alarm,sdk版本选择2.2,Package name:com.lql.activity,Main Activity:Alarm

    2、编写界面:直接修改layout中的main.xml文件,代如下:

    Xml代

    • android:orientation="vertical"

    • android:layout_width="fill_parent"

    • android:layout_height="fill_parent"

    • android:gravity="center_vertical"

    • >

    • android:id="@+id/timeBtn"

    • android:layout_width="fill_parent"

    • android:layout_height="wrap_content"

    • android:text="@string/time"

    • android:textSize="20sp"

    • />

    • android:id="@+id/cancelAlarmBtn"

    • android:layout_width="fill_parent"

    • android:layout_height="wrap_content"

    • android:text="@string/cancelAlarm"

    • />

    • 界面的效果如下:


      3、修改Alarm.java这个activity,在该Activity中需要做这样几件事:

    • 获取界面上的两个按钮组件,并给其绑定

    • 第一个时间按钮,点击后,显示时间选择对话框(TimePicker),供选择小时和分钟,并设置闹钟

    • 第二个按钮,点击之后需要当前设定的闹钟

    • 比较难写的代就是闹钟设置:

      //设置时间

      Java代

    • timeBtn.setOnClickListener(new Button.OnClickListener(){

    • @Override

    • public void onClick(View arg0) {

    • Log.d(TAG, "click the time button to set time");

    • calendar.setTimeInMillis(System.currentTimeMillis());

    • new TimePickerDialog(Alarm.this,new TimePickerDialog.OnTimeSetListener() {

    • @Override

    • public void onTimeSet(TimePicker arg0, int h, int m) {

    • //更新按钮上的时间

    • timeBtn.setText(formatTime(h,m));

    • //设置日历的时间,主要是让日历的年月日和当前同步

    • calendar.setTimeInMillis(System.currentTimeMillis());

    • //设置日历的小时和分钟

    • calendar.set(Calendar.HOUR_OF_DAY, h);

    • calendar.set(Calendar.MINUTE, m);

    • //将秒和毫秒设置为0

    • calendar.set(Calendar.SECOND, 0);

    • calendar.set(Calendar.MILLISECOND, 0);

    • //建立Intent和PendingIntent来调用闹钟管理器

    • Intent intent = new Intent(Alarm.this,AlarmReceiver.class);

    • PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarm.this, 0, intent, 0);

    • //获取闹钟管理器

    • AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    • //设置闹钟

    • alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

    • alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);

    • Toast.makeText(Alarm.this, "设置闹钟的时间为:"+String.valueOf(h)+":"+String.valueOf(m), Toast.LENGTH_SHORT).show();

    • Log.d(TAG, "set the time to "+formatTime(h,m));

    • }

    • },calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();

    • }

    • });

    • 代里面有注释,这里就不多解释了,其中new TimePickerDialog为创建时间选择对话框。为了能够看到效果,我给闹钟了重复提醒:alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);。

      还要为取消闹钟按钮:

      Java代

    • //取消闹钟按钮

    • final Button cancelAlarmBtn = (Button)findViewById(R.id.cancelAlarmBtn);

    • cancelAlarmBtn.setOnClickListener(new Button.OnClickListener(){

    • @Override

    • public void onClick(View arg0) {

    • Intent intent = new Intent(Alarm.this,AlarmReceiver.class);

    • PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarm.this, 0, intent, 0);

    • //获取闹钟管理器

    • AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    • alarmManager.cancel(pendingIntent);

    • Toast.makeText(Alarm.this, "闹钟已经取消!", Toast.LENGTH_SHORT).show();

    • }

    • });

    • 在点击取消闹钟按钮时,取消之前设置的闹钟,核心代就4行。

      4、编写广播接收器,用来接收闹钟的广播,然后进行相关处理,

      Java代

    • public class AlarmReceiver extends BroadcastReceiver {

    • /* (non-Javadoc)

    • * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)

    • */

    • @Override

    • public void onReceive(Context arg0, Intent data) {

    • Log.d(Alarm.TAG, "the time is up,start the alarm...");

    • Toast.makeText(arg0, "闹钟时间到了!", Toast.LENGTH_SHORT).show();

    • }

    • }

    • 这个代就很简单了,主要是要继 承 BroadcastReceiver 这个类,然后重写onRecive方法。onRecive方法在闹钟的时间达到之后会执行,在这里我们可以做自己的事情,比如启动某个程序,或者播放铃声,我这里就是简单的提示一下,使用的是Toast。

      5、在android的AndroidManifest.xml文件中注册广播接收器:

      Xml代

    • package="com.ql.activity"

    • android:versionCode="1"

    • android:versionName="1.0">

    • android:label="@string/app_name">

    • 核心的配置为,这也是闹钟程序的关键,如果不做这个配置,那么时间到了之后,闹钟将不会提示。

      接下来就是到模拟器上测试,运行截图如上图。程序源代见附件。

    浏览 235赞 62时间 2023-07-29

用Android studio做一个可以运行的小程序。