经过写上篇文章Android闹钟最终版【android源码闹钟解析】 .发现有一些留邮箱的,但凡留邮箱的,我就发源码过去了,不错,基本上收到邮箱的都留言感谢了,这样我的成就感就多一点,也能有更多留邮箱的。学习是一个相互的过程,就像师说中“问道有先后,术业有专攻,仅此而已!”,知识就是一层窗户纸,捅破了就没什么了,-------->个人浅薄的拙见,在这表达一下!


        前段时间参加一个CMDN的讲座,讲的是Android主题,其中就有小米手机的UI工程师,董红光简单介绍了5中换肤的方法。所以就想简单练手一下,拿上个闹钟来换个背景试一试。好多应用都有更换自己应用壁纸的功能,例如熊猫看书就有这个功能,还有韩国的SKY手机,也可以更换mainmeun的背景,我也小试牛刀一下;


转载请标明出处:http://blog.csdn.net/wdaming1986/article/details/7479359


 在做这个更换背景的大致思路如下:

        (1)用ImageSwitcher来切换图片,效果就和更换手机系统的壁纸类似,下面有个gallery,滚动gallery,当gallery,选中哪个的时候,就用ImageSwitcher来切换与之对应的大的图片。

         (2)SetBackGround  extends Activity implements  AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory, OnClickListener。重写与之对应的方法。

          (3)利用android提供的映射机制,当选中那个图片的时候,找到对应的gen下的R.java对应的16进制的值。

          (4)用SharedPreferences保存这个id的值,下次进入这个程序,就可以取出来用了。就能永久保存了。


遇到的问题如下:

           (1)在DeskClockMainActivity.java中用startActivityForResult(intent, request);这个方法来启动SetBackGround .java这个类,当点击设置后,finish()这个SetBackGround类,传递给DeskClockMainActivity的onActivityResult()方法一个int的值。问题:startActivityForResult启动的时候,当SetBackGround 这个类finish()的时候,onActivityResult()方法接受到的intent为空,resultCode==cancel,我就查资料,原来是DeskClockMainActivity的launcherMode=“singleInstance”导致的,它会启动一个新的栈,导致返回的时候不在一个栈中,传递intent传递不过去,我猜测是不同栈,intent回传的时候android有问题。修改方法,launcherMode=“singleTask”就可以了。详细参见api的startActivityForResult()介绍;

          (2)SetBackGround 设置背景的类有个button,当声明一个button类型的时候,像常规那样写:

button = (Button) findViewById(R.id.setButton_bg);这时候报错,说类型不匹配。解决办法有两种:1、把button声明为view类的对象,这样再像常规那样找id就不报错了。2、让类实现一个OnClickListener监听,通过:findViewById(R.id.setButton_bg).setOnClickListener(this);这样写就ok了,实现onClick(View v) 的点击方法就ok了。


先看效果图,如下:


            点击图中画红圈的图片,进入换背景界面;                     点击menu--->点击---->设置背景,

                               

进入到更换背景的界面:                      点击设置背景,即背景设成功      

                                

下面把主要的类贴出来,供大家参考:

一、DeskClockMainActivity.java类中的代码,这个是程序的入口类:

package com.cn.daming.deskclock;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CursorAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class DeskClockMainActivity extends Activity implements OnItemClickListener{
static final String PREFERENCES = "AlarmClock";
/** This must be false for production.  If true, turns on logging,
test code, etc. */
static final boolean DEBUG = false;
private SharedPreferences mPrefs;
private LayoutInflater mFactory;
private ListView mAlarmsList;
private Cursor mCursor;
private LinearLayout mBglinearlayout;
private static final int REQ_SET_BG = 100;
private static final String SETBACKGROUND = "deskclock_bground";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//取自定义布局的LayoutInflater
mFactory = LayoutInflater.from(this);
//取getSharedPreferences的实例
mPrefs = getSharedPreferences(PREFERENCES, 0);
//获取闹钟的cursor
mCursor = Alarms.getAlarmsCursor(getContentResolver());
//更新布局界面
updateLayout();
}
//加载更新界面布局
private void updateLayout() {
setContentView(R.layout.alarm_clock);
mBglinearlayout = (LinearLayout) findViewById(R.id.base_layout);
mAlarmsList = (ListView) findViewById(R.id.alarms_list);
AlarmTimeAdapter adapter = new AlarmTimeAdapter(this, mCursor);
mAlarmsList.setAdapter(adapter);
mAlarmsList.setVerticalScrollBarEnabled(true);
mAlarmsList.setOnItemClickListener(this);
mAlarmsList.setOnCreateContextMenuListener(this);
View addAlarm = findViewById(R.id.add_alarm);
addAlarm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addNewAlarm();
}
});
// Make the entire view selected when focused.
addAlarm.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
v.setSelected(hasFocus);
}
});
ImageButton deskClock =
(ImageButton) findViewById(R.id.desk_clock_button);
deskClock.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/**
* modify by wangxianming in 2012-4-17
* enter the set background class SetBackGround
* author:wangxianming
*/
Intent intent = new Intent();
intent.setClass(DeskClockMainActivity.this, SetBackGround.class);
startActivityForResult(intent, REQ_SET_BG);
}
});
}
@Override
protected void onResume() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int setBgId = sharedPreferences.getInt(SETBACKGROUND, 0);
Log.v("wangxianming", "DeskClockMainActivity onResume() setBgId == "+setBgId);
if(setBgId != 0){
//设置背景 add by wangxianming in 2012-04-17
mBglinearlayout.setBackgroundResource(setBgId);
}
super.onResume();
}
private void addNewAlarm() {
startActivity(new Intent(this, SetAlarm.class));
}
/**
* listview的适配器继承CursorAdapter
* @author wangxianming
* 也可以使用BaseAdapter
*/
private class AlarmTimeAdapter extends CursorAdapter {
public AlarmTimeAdapter(Context context, Cursor cursor) {
super(context, cursor);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View ret = mFactory.inflate(R.layout.alarm_time, parent, false);
DigitalClock digitalClock =
(DigitalClock) ret.findViewById(R.id.digitalClock);
digitalClock.setLive(false);
return ret;
}
//把view绑定cursor的每一项
public void bindView(View view, Context context, Cursor cursor) {
final Alarm alarm = new Alarm(cursor);
View indicator = view.findViewById(R.id.indicator);
// Set the initial resource for the bar image.
final ImageView barOnOff =
(ImageView) indicator.findViewById(R.id.bar_onoff);
barOnOff.setImageResource(alarm.enabled ?
R.drawable.ic_indicator_on : R.drawable.ic_indicator_off);
// Set the initial state of the clock "checkbox"
final CheckBox clockOnOff =
(CheckBox) indicator.findViewById(R.id.clock_onoff);
clockOnOff.setChecked(alarm.enabled);
// Clicking outside the "checkbox" should also change the state.
//对checkbox设置监听,使里外一致
indicator.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
clockOnOff.toggle();
updateIndicatorAndAlarm(clockOnOff.isChecked(),
barOnOff, alarm);
}
});
DigitalClock digitalClock =
(DigitalClock) view.findViewById(R.id.digitalClock);
// set the alarm text
final Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, alarm.hour);
c.set(Calendar.MINUTE, alarm.minutes);
digitalClock.updateTime(c);
digitalClock.setTypeface(Typeface.DEFAULT);
// Set the repeat text or leave it blank if it does not repeat.
TextView daysOfWeekView =
(TextView) digitalClock.findViewById(R.id.daysOfWeek);
final String daysOfWeekStr =
alarm.daysOfWeek.toString(DeskClockMainActivity.this, false);
if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) {
daysOfWeekView.setText(daysOfWeekStr);
daysOfWeekView.setVisibility(View.VISIBLE);
} else {
daysOfWeekView.setVisibility(View.GONE);
}
// Display the label
TextView labelView =
(TextView) view.findViewById(R.id.label);
if (alarm.label != null && alarm.label.length() != 0) {
labelView.setText(alarm.label);
labelView.setVisibility(View.VISIBLE);
} else {
labelView.setVisibility(View.GONE);
}
}
};
//更新checkbox
private void updateIndicatorAndAlarm(boolean enabled, ImageView bar,
Alarm alarm) {
bar.setImageResource(enabled ? R.drawable.ic_indicator_on
: R.drawable.ic_indicator_off);
Alarms.enableAlarm(this, alarm.id, enabled);
if (enabled) {
SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
alarm.daysOfWeek);
}
}
/*
* (non-Javadoc)
* @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
* 创建上下文菜单
*/
@Override
public boolean onContextItemSelected(final MenuItem item) {
final AdapterContextMenuInfo info =
(AdapterContextMenuInfo) item.getMenuInfo();
final int id = (int) info.id;
// Error check just in case.
if (id == -1) {
return super.onContextItemSelected(item);
}
switch (item.getItemId()) {
case R.id.delete_alarm:
// Confirm that the alarm will be deleted.
new AlertDialog.Builder(this)
.setTitle(getString(R.string.delete_alarm))
.setMessage(getString(R.string.delete_alarm_confirm))
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d,
int w) {
Alarms.deleteAlarm(DeskClockMainActivity.this, id);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
return true;
case R.id.enable_alarm:
final Cursor c = (Cursor) mAlarmsList.getAdapter()
.getItem(info.position);
final Alarm alarm = new Alarm(c);
Alarms.enableAlarm(this, alarm.id, !alarm.enabled);
if (!alarm.enabled) {
SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
alarm.daysOfWeek);
}
return true;
case R.id.edit_alarm:
Intent intent = new Intent(this, SetAlarm.class);
intent.putExtra(Alarms.ALARM_ID, id);
startActivity(intent);
return true;
default:
break;
}
return super.onContextItemSelected(item);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
* 创建菜单
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfo) {
// Inflate the menu from xml.
getMenuInflater().inflate(R.menu.context_menu, menu);
// Use the current item to create a custom view for the header.
final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
final Cursor c =
(Cursor) mAlarmsList.getAdapter().getItem((int) info.position);
final Alarm alarm = new Alarm(c);
// Construct the Calendar to compute the time.
final Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, alarm.hour);
cal.set(Calendar.MINUTE, alarm.minutes);
final String time = Alarms.formatTime(this, cal);
// Inflate the custom view and set each TextView's text.
final View v = mFactory.inflate(R.layout.context_menu_header, null);
TextView textView = (TextView) v.findViewById(R.id.header_time);
textView.setText(time);
textView = (TextView) v.findViewById(R.id.header_label);
textView.setText(alarm.label);
// Set the custom view on the menu.
menu.setHeaderView(v);
// Change the text based on the state of the alarm.
if (alarm.enabled) {
menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm);
}
}
/*
* (non-Javadoc)
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
* 设置菜单的点击事件的处理
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
case R.id.menu_item_alarm_set_bg:
/**
* modify by wangxianming in 2012-4-17
* enter the set background class SetBackGround
* author:wangxianming
*/
startActivityForResult((new Intent(this, SetBackGround.class)), REQ_SET_BG);
return true;
case R.id.menu_item_add_alarm:
addNewAlarm();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
* 创建菜单
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.alarm_list_menu, menu);
return super.onCreateOptionsMenu(menu);
}
/*
* (non-Javadoc)
* @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
* 创建菜单的点击事件响应
*/
public void onItemClick(AdapterView<?> adapterView, View v, int pos, long id) {
Intent intent = new Intent(this, SetAlarm.class);
intent.putExtra(Alarms.ALARM_ID, (int) id);
startActivity(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
if(resultCode != RESULT_OK) {
return;
}
if(requestCode == REQ_SET_BG) {
switch(resultCode) {
case RESULT_OK:
Bundle b = data.getExtras();
int id = b.getInt("getDrawable");
editor.putInt(SETBACKGROUND, id);
editor.commit();
if(0 != id) {
//设置背景
mBglinearlayout.setBackgroundResource(id);
}
Log.v("wangxianming", "DeskClockMainActivity onActivityResult()----> id == "+id);
break;
default:
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
super.onDestroy();
ToastMaster.cancelToast();
mCursor.close();
}
}

二、SetBackGround.java类中的代码,这个是设置背景图片的类:

package com.cn.daming.deskclock;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CursorAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class DeskClockMainActivity extends Activity implements OnItemClickListener{
static final String PREFERENCES = "AlarmClock";
/** This must be false for production.  If true, turns on logging,
test code, etc. */
static final boolean DEBUG = false;
private SharedPreferences mPrefs;
private LayoutInflater mFactory;
private ListView mAlarmsList;
private Cursor mCursor;
private LinearLayout mBglinearlayout;
private static final int REQ_SET_BG = 100;
private static final String SETBACKGROUND = "deskclock_bground";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//取自定义布局的LayoutInflater
mFactory = LayoutInflater.from(this);
//取getSharedPreferences的实例
mPrefs = getSharedPreferences(PREFERENCES, 0);
//获取闹钟的cursor
mCursor = Alarms.getAlarmsCursor(getContentResolver());
//更新布局界面
updateLayout();
}
//加载更新界面布局
private void updateLayout() {
setContentView(R.layout.alarm_clock);
mBglinearlayout = (LinearLayout) findViewById(R.id.base_layout);
mAlarmsList = (ListView) findViewById(R.id.alarms_list);
AlarmTimeAdapter adapter = new AlarmTimeAdapter(this, mCursor);
mAlarmsList.setAdapter(adapter);
mAlarmsList.setVerticalScrollBarEnabled(true);
mAlarmsList.setOnItemClickListener(this);
mAlarmsList.setOnCreateContextMenuListener(this);
View addAlarm = findViewById(R.id.add_alarm);
addAlarm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addNewAlarm();
}
});
// Make the entire view selected when focused.
addAlarm.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
v.setSelected(hasFocus);
}
});
ImageButton deskClock =
(ImageButton) findViewById(R.id.desk_clock_button);
deskClock.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/**
* modify by wangxianming in 2012-4-17
* enter the set background class SetBackGround
* author:wangxianming
*/
Intent intent = new Intent();
intent.setClass(DeskClockMainActivity.this, SetBackGround.class);
startActivityForResult(intent, REQ_SET_BG);
}
});
}
@Override
protected void onResume() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int setBgId = sharedPreferences.getInt(SETBACKGROUND, 0);
Log.v("wangxianming", "DeskClockMainActivity onResume() setBgId == "+setBgId);
if(setBgId != 0){
//设置背景 add by wangxianming in 2012-04-17
mBglinearlayout.setBackgroundResource(setBgId);
}
super.onResume();
}
private void addNewAlarm() {
startActivity(new Intent(this, SetAlarm.class));
}
/**
* listview的适配器继承CursorAdapter
* @author wangxianming
* 也可以使用BaseAdapter
*/
private class AlarmTimeAdapter extends CursorAdapter {
public AlarmTimeAdapter(Context context, Cursor cursor) {
super(context, cursor);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View ret = mFactory.inflate(R.layout.alarm_time, parent, false);
DigitalClock digitalClock =
(DigitalClock) ret.findViewById(R.id.digitalClock);
digitalClock.setLive(false);
return ret;
}
//把view绑定cursor的每一项
public void bindView(View view, Context context, Cursor cursor) {
final Alarm alarm = new Alarm(cursor);
View indicator = view.findViewById(R.id.indicator);
// Set the initial resource for the bar image.
final ImageView barOnOff =
(ImageView) indicator.findViewById(R.id.bar_onoff);
barOnOff.setImageResource(alarm.enabled ?
R.drawable.ic_indicator_on : R.drawable.ic_indicator_off);
// Set the initial state of the clock "checkbox"
final CheckBox clockOnOff =
(CheckBox) indicator.findViewById(R.id.clock_onoff);
clockOnOff.setChecked(alarm.enabled);
// Clicking outside the "checkbox" should also change the state.
//对checkbox设置监听,使里外一致
indicator.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
clockOnOff.toggle();
updateIndicatorAndAlarm(clockOnOff.isChecked(),
barOnOff, alarm);
}
});
DigitalClock digitalClock =
(DigitalClock) view.findViewById(R.id.digitalClock);
// set the alarm text
final Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, alarm.hour);
c.set(Calendar.MINUTE, alarm.minutes);
digitalClock.updateTime(c);
digitalClock.setTypeface(Typeface.DEFAULT);
// Set the repeat text or leave it blank if it does not repeat.
TextView daysOfWeekView =
(TextView) digitalClock.findViewById(R.id.daysOfWeek);
final String daysOfWeekStr =
alarm.daysOfWeek.toString(DeskClockMainActivity.this, false);
if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) {
daysOfWeekView.setText(daysOfWeekStr);
daysOfWeekView.setVisibility(View.VISIBLE);
} else {
daysOfWeekView.setVisibility(View.GONE);
}
// Display the label
TextView labelView =
(TextView) view.findViewById(R.id.label);
if (alarm.label != null && alarm.label.length() != 0) {
labelView.setText(alarm.label);
labelView.setVisibility(View.VISIBLE);
} else {
labelView.setVisibility(View.GONE);
}
}
};
//更新checkbox
private void updateIndicatorAndAlarm(boolean enabled, ImageView bar,
Alarm alarm) {
bar.setImageResource(enabled ? R.drawable.ic_indicator_on
: R.drawable.ic_indicator_off);
Alarms.enableAlarm(this, alarm.id, enabled);
if (enabled) {
SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
alarm.daysOfWeek);
}
}
/*
* (non-Javadoc)
* @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
* 创建上下文菜单
*/
@Override
public boolean onContextItemSelected(final MenuItem item) {
final AdapterContextMenuInfo info =
(AdapterContextMenuInfo) item.getMenuInfo();
final int id = (int) info.id;
// Error check just in case.
if (id == -1) {
return super.onContextItemSelected(item);
}
switch (item.getItemId()) {
case R.id.delete_alarm:
// Confirm that the alarm will be deleted.
new AlertDialog.Builder(this)
.setTitle(getString(R.string.delete_alarm))
.setMessage(getString(R.string.delete_alarm_confirm))
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d,
int w) {
Alarms.deleteAlarm(DeskClockMainActivity.this, id);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
return true;
case R.id.enable_alarm:
final Cursor c = (Cursor) mAlarmsList.getAdapter()
.getItem(info.position);
final Alarm alarm = new Alarm(c);
Alarms.enableAlarm(this, alarm.id, !alarm.enabled);
if (!alarm.enabled) {
SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
alarm.daysOfWeek);
}
return true;
case R.id.edit_alarm:
Intent intent = new Intent(this, SetAlarm.class);
intent.putExtra(Alarms.ALARM_ID, id);
startActivity(intent);
return true;
default:
break;
}
return super.onContextItemSelected(item);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
* 创建菜单
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfo) {
// Inflate the menu from xml.
getMenuInflater().inflate(R.menu.context_menu, menu);
// Use the current item to create a custom view for the header.
final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
final Cursor c =
(Cursor) mAlarmsList.getAdapter().getItem((int) info.position);
final Alarm alarm = new Alarm(c);
// Construct the Calendar to compute the time.
final Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, alarm.hour);
cal.set(Calendar.MINUTE, alarm.minutes);
final String time = Alarms.formatTime(this, cal);
// Inflate the custom view and set each TextView's text.
final View v = mFactory.inflate(R.layout.context_menu_header, null);
TextView textView = (TextView) v.findViewById(R.id.header_time);
textView.setText(time);
textView = (TextView) v.findViewById(R.id.header_label);
textView.setText(alarm.label);
// Set the custom view on the menu.
menu.setHeaderView(v);
// Change the text based on the state of the alarm.
if (alarm.enabled) {
menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm);
}
}
/*
* (non-Javadoc)
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
* 设置菜单的点击事件的处理
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
case R.id.menu_item_alarm_set_bg:
/**
* modify by wangxianming in 2012-4-17
* enter the set background class SetBackGround
* author:wangxianming
*/
startActivityForResult((new Intent(this, SetBackGround.class)), REQ_SET_BG);
return true;
case R.id.menu_item_add_alarm:
addNewAlarm();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
* 创建菜单
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.alarm_list_menu, menu);
return super.onCreateOptionsMenu(menu);
}
/*
* (non-Javadoc)
* @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
* 创建菜单的点击事件响应
*/
public void onItemClick(AdapterView<?> adapterView, View v, int pos, long id) {
Intent intent = new Intent(this, SetAlarm.class);
intent.putExtra(Alarms.ALARM_ID, (int) id);
startActivity(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
if(resultCode != RESULT_OK) {
return;
}
if(requestCode == REQ_SET_BG) {
switch(resultCode) {
case RESULT_OK:
Bundle b = data.getExtras();
int id = b.getInt("getDrawable");
editor.putInt(SETBACKGROUND, id);
editor.commit();
if(0 != id) {
//设置背景
mBglinearlayout.setBackgroundResource(id);
}
Log.v("wangxianming", "DeskClockMainActivity onActivityResult()----> id == "+id);
break;
default:
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
super.onDestroy();
ToastMaster.cancelToast();
mCursor.close();
}
}

【说明】有Android闹钟最终版【android源码闹钟解析】代码的,可以参照我的代码修改一下;

   当然如果没有代码的:

(一)想要源码的可以留下邮箱,我看到后就给你发过去!

                     (二)也可以到我的csdn资源中下载:http://download.csdn.net/detail/wdaming1986/4240708

Android闹钟拓展版【安卓闹钟可换壁纸版】相关推荐

  1. play home android,playhome手机版安卓版

    playhome手机版安卓版是一款采用高清3D画风打造的经典模拟养成类手游,游戏玩法相对很自由,玩家可以随意的在游戏里做各种想做的事情,通过不断的触发剧情来和新的女生互动,多个选择多个结局. play ...

  2. 微信分身 android,微信分身版安卓版

    微信分身版下载安卓版是一款能让您的安卓手机同时使用两个微信的应用.一部安卓手机可以安装两个个相同软件,同时运行互不影响.操作简单,只需一键点击分身制作,静待安装分身数秒便可获得.源自官方应用,体验一模 ...

  3. android位置闹钟测试图,位置闹钟安卓版

    位置闹钟安卓版是一款有趣的闹钟提醒APP,你可以设置自己需要提醒的地点和事项,当位置闹钟在后台检测到你已经接近制定地点的时候就会启动提醒服务,非常的实用哟,帮助你不会做车坐过站,本来先喊已经很累了,在 ...

  4. android闹钟app,安卓手机闹钟软件谁最好?四款安卓闹钟软件横评

    小编今天为您带来四款安卓手机闹钟软件横评,希望可以为您参考,找到适合自己的闹钟软件. 安卓手机闹钟软件横评之软件介绍 评测环境: 评测手机:HTC G6 (Legend) 评测系统:Android 2 ...

  5. Android开发之2048安卓版

    之前是在eclipse上写的,后面换成了android sudio. 2048游戏的UI整体可以采用线性布局,即LinearLayout,其中嵌套一个线性布局和一个GridLayout,内嵌的线性布局 ...

  6. android 换肤 视频,网易云音乐4.0版体验:自定义换肤和短视频来了

    原标题:网易云音乐4.0版体验:自定义换肤和短视频来了 日前,网易云音乐的iOS和Android更新到了4.0版本,对于期待更多创新功能的忠实粉来说,这着实是一个好消息.在新版本到来之后,不少人开始发 ...

  7. android9默认字体下载,iFont爱字体 v5.5.9 Android特别版-实用的手机换字体软件

    iFont爱字体 v5.5.9 Android特别版-实用的手机换字体软件 书法字体2015.09.28iFont iFont(爱字体)是安卓平台最强大.最专业的字体软件,精彩字体,随你所换!iFon ...

  8. android手机可以换字体吗,最新版安卓手机怎么换字体?

    手机是我们每天都要面对的,经常需要打开,手机屏幕是和我们最直观的交互界面,是不是看惯了自带的那个方方正正的字体,觉得不是那么美观呢.可爱的女生是不是也想自己的字体变得更加漂亮呢.简单的几部操作,就能让 ...

  9. android+os+2.3版本闹钟下载,AlarmDroid闹钟

    AlarmDroid闹钟是一款手机闹钟软件,这款软件是很任性智能化的闹钟软件,AlarmDroid闹钟可以根据你的状态来设置闹钟的状态,包括刚起床时的贪睡模式还有闹钟的铃声种类,方便更好的便利你的生活 ...

最新文章

  1. 写给Java程序员的Java虚拟机学习指南
  2. Linux系统Vi/Vim编辑器的简单介绍、安装/卸载、常用命令
  3. 向EXECL文件中导入数据的同时插入图片
  4. 以太坊geth结构解析和源码分析
  5. python退出循环快捷_python退出循环的方法
  6. [翻译:更新]Understanding Linux Network Internals - Table of Contents
  7. mysql 查看密码_Ubuntu安装和配置MySQL数据库
  8. 树状数组求逆序对_算法系列之-数组中的逆序对
  9. tongweb php,TongWeb服务器部署
  10. swift拖放的按钮如何在后台设置点击事件 www.cnblogs.com/foxting/p/SWIFT.html
  11. 计算机组成原理与汇编语言试题,2069电大《计算机组成原理与汇编语言》试题和答案2005...
  12. H.264视频压缩标准
  13. 计算机鼠标左右键作用,win7电脑鼠标右键有什么功能和作用
  14. 本科有计算机应用吗,计算机应用专业自考本科
  15. EI会议论文被检索难度评定
  16. 亮剑java web_为什么《亮剑Java Web 项目开发案例导航》第二个项目运行不了?
  17. CentOS7.6(1810)安装
  18. F(X)分布函数的通俗解释
  19. 非科班关于gan的一点点学习
  20. CMD命令格式化U盘(磁盘)

热门文章

  1. 服务器托管的必要性(上)
  2. Binder相关面试题目
  3. java binder_Binder基本使用
  4. Mac技巧之苹果电脑Mac OS X系统自带的鼠标所在点颜色RGB值查看工具:数码测色计...
  5. html里ajax使用、webpy服务在img显示图片、ajax修改table里的数据、$.post()、window,onload、$(button).click、mjpg_streamer
  6. 感谢你”在英语邮件中的十种最佳表达方式
  7. python的foolnltk库实现中文NER
  8. 常见MySQL面试题(1)(MySQL面试笔试题)
  9. Application类的使用方法
  10. 机器视觉LED光源类型及技术简介