小白接触Android Studio不久,关于一个自制小闹钟的问题,请路过的大神指教

奉上代码:第一个活动(MainActivity)

package com.example.lesson18_clock;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    private Button btnStart,btnStop;
    private TextView tvInfo;
    private PendingIntent pendingIntent;
    private AlarmManager alarmManager;
    private Intent intent;
    private int requestCode=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar currentTime=Calendar.getInstance();//用于存放时间选择器中的默认时间(当前时间)
                new TimePickerDialog(MainActivity.this,new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Calendar c=Calendar.getInstance();
                        long sysTime=System.currentTimeMillis();//取出当前的时间毫秒值。
                        c.set(Calendar.HOUR,hourOfDay);
                        c.set(Calendar.MINUTE,minute);
                        c.set(Calendar.SECOND,0);
                        c.set(Calendar.MILLISECOND,0);
                        long selTime=c.getTimeInMillis();//取出设置时间的毫秒值。
                        if(sysTime>selTime){
                           c.add(Calendar.DAY_OF_MONTH,1);
                        }
                        tvInfo.setText("你设置的闹钟时间为:"+hourOfDay+":"+minute);
                        //API版本大于19
                        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
                            alarmManager.setExact(AlarmManager.RTC,c.getTimeInMillis(),pendingIntent);
                            alarmManager.setRepeating(AlarmManager.RTC,c.getTimeInMillis(),1000*60*60*24,pendingIntent);
                        }else {
                            alarmManager.set(AlarmManager.RTC,c.getTimeInMillis(),pendingIntent);
                        }
                    }
                }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime.get(Calendar.MINUTE), false).show();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alarmManager.cancel(pendingIntent);
            }
        });
    }
    void initView(){
        btnStart=findViewById(R.id.btnStart);
        btnStop=findViewById(R.id.btnStop);
        tvInfo=findViewById(R.id.tvInfo);
        alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
        intent=new Intent(this,ClockActivity.class);
        pendingIntent=PendingIntent.getActivity(this,requestCode,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    }
}

还有第一个活动上的布局文件main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnStart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="设置闹钟" />

    <Button
        android:id="@+id/btnStop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消闹钟" />
    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

第二个活动:(ClockActivity用于启动闹钟)

package com.example.lesson18_clock;

import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

public class ClockActivity extends AppCompatActivity {
    MediaPlayer mediaPlayer=new MediaPlayer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.click);
        //使用mediaplay播放闹钟声音。
        mediaPlayer=MediaPlayer.create(this, R.raw.wgni);
        mediaPlayer.start();
        //弹出一个闹钟提醒对话框——关闭闹钟,让播放闹钟的mediaplay停止播放声音。
        new AlertDialog.Builder(this)
                .setTitle("闹钟")
                .setMessage("时间到了")
                .setPositiveButton("关闭闹钟", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mediaPlayer.stop();
                        ClockActivity.this.finish();
                    }
                }).show();
    }
}

第二活动上的布局文件click,由于无需交互,所以没有控件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>

注册信息:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lesson18_clock">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ClockActivity" android:theme="@style/Theme.AppCompat"/>
    </application>
</manifest>

资源文件如下:
layout下的两个布局文件,还有mp3格式的闹钟铃声
|求问:运行正常,但是设置闹钟以后为什么时间到了却没有响铃,也没有对话框提示?求大神指点。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值