intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
创建一个新的activity栈,启动MainActivvity()。
startActivityForResult的使用:
主Activity实现 protected void onActivityResult(int requestCode, int resultCode, Intent data)获取返回的数据
其中requestCode代表申请编码(因为有不同的申请发起者),resultCode代表结果编码(返回的结果也有所不同)
第二个Activity调用setResult(int resultCode,Intent intent)方法设置返回的数据。
创建继承BaseAdapter的类填充ListView:
public class NoteAdapter extends BaseAdapter {
private List<String> items;
private List<String> times;
private List<String> ids;
private LayoutInflater inflater;
private DBOper db;
private Context context;
public NoteAdapter(Context context,List<String> items,List<String> times,List<String> ids){
inflater = LayoutInflater.from(context);
db = new DBOper(context);
this.items = items;
this.times = times;
this.ids = ids;
this.context = context;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//创建每个列表项的View
@Override
public View getView(int position, View view, ViewGroup group) {
ViewHolder holder = new ViewHolder();
if(view==null){
view = inflater.inflate(R.layout.note_list_item, null);//加载列表项的布局文件.
holder.title = (TextView)view.findViewById(R.id.note_title);
holder.createtime = (TextView)view.findViewById(R.id.note_createtime);
holder.clock = (ImageView)view.findViewById(R.id.note_clock);
view.setTag(holder); //将view和holder进行绑定
}else{
holder = (ViewHolder)view.getTag();
}
String title = items.get(position);
title = title.length()>10 ? title.substring(0, 10)+"..." : title;
holder.title.setText(title);
holder.createtime.setText(times.get(position));
Cursor cursor = db.getClock(ids.get(position));
//进行判断闹钟是否开启
if(cursor.moveToFirst()){
String isOpen = cursor.getString(cursor.getColumnIndex(DBOper.CLOCK_ISOPEN));
if(isOpen.equals(context.getResources().getString(R.string.yes))){
holder.clock.setImageResource(R.drawable.clock);
}
}
return view;
}
private class ViewHolder{
private TextView title;
private TextView createtime;
private ImageView clock;
}
}
设置闹钟代码:
private void openAlarm(){
cal.setTimeInMillis(System.currentTimeMillis());
if(!time.equals("")){
String[] temp = time.split(":");
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(temp[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(temp[1]));
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(AlarmActivity.this,CallAlarm.class);
intent.putExtra(DBOper.CLOCK_ID, id);
//声明延迟性的intent,因为程序要在关闭主程序的情况下调用AlarmActivity的context
PendingIntent pi = PendingIntent.getBroadcast(AlarmActivity.this, Integer.parseInt(id), intent, 0);
//cal.getTimeMillis()返回从cai.setTimeMills()和cal.set()设置的时间的毫秒数。
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
}
isOpen = yesOrNo[0];
setListItem();
}
调用系统的Activity选取音乐:public void setRings(){
Intent intent = new Intent();
//允许用户选择特殊种类的数据,并返回
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
this.startActivityForResult(intent, 0);
}
使用sqliteOpenHelper对数据库的操作:1.继承sqliteOpenHepler
2.实现onCreate()方法初始化表格。
3实现onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 方法,当数据库版本发生改变时调用的方法
4.通过SQLiteDatabase db = this.getReadableDatabase()或者getWriteableDatabase();获取对数据库的实例.
5.通过db对数据库进行操作.
使用AudioManager实例播放音乐
1.设置原始资源,MediaPlayer.setDataSource(Context context, Uri uri) 。
2.设置铃声类型。MediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM)。
3.准备播放。Media.prepare().
4.开始播放。Media.start().