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闹钟的全部代码了,希望大家能够喜欢。
您可能感兴趣的文章:android开发之蜂鸣提示音和震动提示的实现原理与参考代码Android实现调用震动的方法android滑动解锁震动效果的开启和取消android获取情景模式和铃声 实现震动、铃声提醒android 触屏的震动响应接口调用方法Android实现手机震动效果Android震动与提示音实现代码Android设置铃声实现代码Android闹铃服务AlarmManager用法深入分析Android编程实现震动与振铃的方法详解
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341