欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android闹铃简单实现

程序员文章站 2022-03-23 14:10:27
1.闹铃activity 复制代码 代码如下: package com.example.myalarm; import java.util.calendar; im...

1.闹铃activity

复制代码 代码如下:

package com.example.myalarm;
import java.util.calendar;
import java.util.date;
import java.util.locale;
import android.media.ringtonemanager;
import android.net.uri;
import android.os.bundle;
import android.provider.mediastore;
import android.app.activity;
import android.app.alarmmanager;
import android.app.pendingintent;
import android.app.timepickerdialog;
import android.app.timepickerdialog.ontimesetlistener;
import android.content.intent;
import android.database.cursor;
import android.util.log;
import android.view.menu;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.timepicker;
public class alarmactivity extends activity {
    private static final string tag = "alarmactivity";
    alarmmanager alarmmanager;
    calendar calendar = calendar.getinstance(locale.chinese);
    button settime;
    button setring;
    button setover;
    uri ringuri = ringtonemanager.getdefaulturi(ringtonemanager.type_alarm);
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_alarm);
        alarmmanager = (alarmmanager) getsystemservice(alarm_service);
        settime = (button) findviewbyid(r.id.settime);
        setring = (button) findviewbyid(r.id.setring);
        setover = (button) findviewbyid(r.id.setover);
        //settime();
        //setringtone();
        settimeandring();
    }
    private void settimeandring(){
        settime.setonclicklistener(new onclicklistener() {
            @override
            public void onclick(view view) {
                settime();
            }
        });
        setring.setonclicklistener(new onclicklistener() {
            @override
            public void onclick(view view) {
                setringtone();
            }
        });
        setover.setonclicklistener(new onclicklistener() {
            @override
            public void onclick(view view) {
                setalarm(calendar);
            }
        });
    }
    //启动闹玲,设置闹玲
    private void setalarm(calendar calendar){
        intent intent = new intent();
        intent.setclass(this, alarmbroadcastreceiver.class);
        intent.putextra("msg", "get up!get up!");
        intent.putextra("ringuri", ringuri.tostring());
        log.d(tag, ringuri.tostring());
        pendingintent pendingintent = pendingintent.getbroadcast(this, 0, intent, 0);
        alarmmanager.set(alarmmanager.rtc_wakeup,calendar.gettimeinmillis(), pendingintent);
    }
    //设置时间
    private void settime(){
        date date = new date();
        calendar.settime(date);
        int hour = calendar.get(calendar.hour);
        int minute = calendar.get(calendar.minute);
        new timepickerdialog(this, new ontimesetlistener() {
            @override
            public void ontimeset(timepicker timepicker, int hour, int minute) {
                calendar.set(calendar.hour,hour);
                calendar.set(calendar.minute,minute);
            }
        }, hour, minute, true).show();
    }
    //设置闹玲铃声
    private void setringtone(){
        intent intent = new intent();
        intent.setaction(ringtonemanager.action_ringtone_picker);
        intent.putextra(ringtonemanager.extra_ringtone_show_default, false);
        intent.putextra(ringtonemanager.extra_ringtone_title, "设置闹玲铃声");
        intent.putextra(ringtonemanager.extra_ringtone_type, ringtonemanager.type_all);
        uri pickeduri = ringtonemanager.getactualdefaultringtoneuri(this,ringtonemanager.type_alarm);
        if (pickeduri!=null) {
            intent.putextra(ringtonemanager.extra_ringtone_existing_uri,pickeduri);
            ringuri = pickeduri;
        }
        startactivityforresult(intent, 1);
    }
    @override
    protected void onactivityresult(int requestcode, int resultcode, intent data) {
        super.onactivityresult(requestcode, resultcode, data);
        if (resultcode!=result_ok) {
            return;
        }
        switch (requestcode) {
        case 1:
            //获取选中的铃声的uri
            uri pickeduri = data.getparcelableextra(ringtonemanager.extra_ringtone_picked_uri);
            log.i(tag,pickeduri.tostring());
            ringtonemanager.setactualdefaultringtoneuri(this, ringtonemanager.type_alarm, pickeduri);
            getname(ringtonemanager.type_alarm);
            break;
        default:
            break;
        }
    }
    private void getname(int type){
        uri pickeduri = ringtonemanager.getactualdefaultringtoneuri(this, type);
        log.i(tag,pickeduri.tostring());
        cursor cursor = this.getcontentresolver().query(pickeduri, new string[]{mediastore.audio.media.title}, null, null, null);
        if (cursor!=null) {
            if (cursor.movetofirst()) {
                string ring_name = cursor.getstring(0);
                log.i(tag,ring_name);
                string[] c = cursor.getcolumnnames();
                for (string string : c) {
                    log.i(tag,string);
                }
            }
            cursor.close();
        }
    }
    @override
    public boolean oncreateoptionsmenu(menu menu) {
        getmenuinflater().inflate(r.menu.alarm, menu);
        return true;
    }
}

2,布局文件

复制代码 代码如下:

<relativelayout xmlns:android=""
    xmlns:tools=""
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <button android:id="@+id/settime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置时间"/>
    <button android:id="@+id/setring"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/settime"
        android:text="设置铃声"/>
    <button android:id="@+id/setover"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/setring"
        android:text="设置完成"/>
</relativelayout>

3.广播接收闹铃信息:

复制代码 代码如下:

package com.example.myalarm;
import java.io.ioexception;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.media.mediaplayer;
import android.net.uri;
import android.util.log;
import android.widget.toast;
public class alarmbroadcastreceiver extends broadcastreceiver {
    uri ringuri;
    @override
    public void onreceive(context context, intent intent) {
        string msg = intent.getstringextra("msg");
        toast.maketext(context, msg, toast.length_short).show();
        mediaplayer mp = new mediaplayer();
        string uri = intent.getstringextra("ringuri");
        if (uri!=null) {
            ringuri = uri.parse(uri);
            log.d("alarmactivity", ringuri.tostring());
        }
        try {
            mp.setdatasource(context, ringuri);
            mp.prepare();
        } catch (illegalargumentexception e) {
            e.printstacktrace();
        } catch (securityexception e) {
            e.printstacktrace();
        } catch (illegalstateexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        }
        mp.start();
    }
}

4.在manifest文件中注册广播

复制代码 代码如下:

 <receiver android:name="com.example.myalarm.alarmbroadcastreceiver"></receiver>

以上代码就是实现android闹钟的全部代码了,希望大家能够喜欢。