編輯:關於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開發中我們常常用到圓形的頭像,如果每次加載之後再進行圓形裁剪特別麻煩。所以在這裡寫一個自定義圓形ImageView,直接去加載網絡圖片,這樣的話就特別的方
提出疑問這幾天研究工廠模式的時候,看到網上的一些文章中舉的例子我就很疑惑,我相信這也是許多人的疑惑:工廠模式的功能就是創建實例,我們創建實例直接new不就完了嗎,干嘛還得
Android 中的 Service按運行地點分類:1、本地服務(Local) 該服務依附在主進程上, 服務依附在主進程上而不是獨立的進程,這樣在一定程度上節約了資源,另
?Activity?BroadcastReceiver 廣播接收者?Service 服務?ContentProvider 內容提供者Activity的繼承關系public