亲宝软件园·资讯

展开

android闹铃简单实现 android闹铃简单实现

人气:0
想了解android闹铃简单实现的相关内容吗,在本文为您仔细讲解android闹铃简单实现的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,闹铃,下面大家一起来学习吧。

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="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/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闹钟的全部代码了,希望大家能够喜欢。

加载全部内容

相关教程
猜你喜欢
用户评论