Android 带有多个闹钟的闹钟 Class 如何为每个闹钟填充 imageview

Android Alarm Clock Class with multiple alarms how to populate imageview for each alarm

非常感谢任何对此问题的帮助。

我可以用用户选择的声音设置多个闹钟,我想给 用户可以选择将不同的图像保存到 ImageView 时 警报被触发。 MainActivity 紧随其后。

以下是Alarm Class的相关代码及完整代码 向下滚动时,选择器 Activity 约为四分之三:

Main.Java

public class AlarmDetailsActivity extends Activity {

private AlarmDBHelper dbHelper = new AlarmDBHelper(this);

private AlarmModel alarmDetails;

private TimePicker timePicker;
private EditText edtName;
private CustomSwitch chkWeekly;
private CustomSwitch chkSunday;
private CustomSwitch chkMonday;
private CustomSwitch chkTuesday;
private CustomSwitch chkWednesday;
private CustomSwitch chkThursday;
private CustomSwitch chkFriday;
private CustomSwitch chkSaturday;
private TextView txtToneSelection;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_ACTION_BAR);

setContentView(R.layout.activity_details);

getActionBar().setTitle("Create New Alarm");
getActionBar().setDisplayHomeAsUpEnabled(true);

timePicker = (TimePicker) findViewById(R.id.alarm_details_time_picker);
edtName = (EditText) findViewById(R.id.alarm_details_name);
chkWeekly = (CustomSwitch)                          

findViewById(R.id.alarm_details_repeat_weekly);
chkSunday = (CustomSwitch) 
findViewById(R.id.alarm_details_repeat_sunday);
chkMonday = (CustomSwitch) 
findViewById(R.id.alarm_details_repeat_monday);
chkTuesday = (CustomSwitch) 
findViewById(R.id.alarm_details_repeat_tuesday);
chkWednesday = (CustomSwitch) 
findViewById(R.id.alarm_details_repeat_wednesday);
chkThursday = (CustomSwitch)
findViewById(R.id.alarm_details_repeat_thursday);
chkFriday = (CustomSwitch) 
findViewById(R.id.alarm_details_repeat_friday);
chkSaturday = (CustomSwitch) 
findViewById(R.id.alarm_details_repeat_saturday);
txtToneSelection = (TextView)
findViewById(R.id.alarm_label_tone_selection);

long id = getIntent().getExtras().getLong("id");

if (id == -1) {
alarmDetails = new AlarmModel();
} else {
alarmDetails = dbHelper.getAlarm(id);

timePicker.setCurrentMinute(alarmDetails.timeMinute);
timePicker.setCurrentHour(alarmDetails.timeHour);

edtName.setText(alarmDetails.name);

chkWeekly.setChecked(alarmDetails.repeatWeekly);
chkSunday.setChecked(alarmDetails.getRepeatingDay
(AlarmModel.SUNDAY));             
chkMonday.setChecked(alarmDetails.getRepeatingDay
(AlarmModel.MONDAY));
chkTuesday.setChecked(alarmDetails.getRepeatingDay
(AlarmModel.TUESDAY));
chkWednesday.setChecked(alarmDetails.getRepeatingDay
(AlarmModel.WEDNESDAY));
chkThursday.setChecked(alarmDetails.getRepeatingDay
(AlarmModel.THURSDAY));
chkFriday.setChecked(alarmDetails.getRepeatingDay
(AlarmModel.FRDIAY));
chkSaturday.setChecked(alarmDetails.getRepeatingDay
(AlarmModel.SATURDAY));

txtToneSelection.setText(RingtoneManager.getRingtone(this,  

alarmDetails.alarmTone).getTitle(this));
}

final LinearLayout ringToneContainer = (LinearLayout)  

findViewById(R.id.alarm_ringtone_container);
ringToneContainer.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
startActivityForResult(intent , 1);
        }
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1: {
alarmDetails.alarmTone =

data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);


txtToneSelection.setText(RingtoneManager.getRingtone(this,
alarmDetails.alarmTone).getTitle(this));
break;
}
default: {
break;
}
}
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.alarm_details, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case android.R.id.home: {
finish();
break;
}
case R.id.action_save_alarm_details: {
updateModelFromLayout();

AlarmManagerHelper.cancelAlarms(this);

if (alarmDetails.id < 0) {
dbHelper.createAlarm(alarmDetails);
} else {
dbHelper.updateAlarm(alarmDetails);
}

AlarmManagerHelper.setAlarms(this);

setResult(RESULT_OK);
finish();
}
}

return super.onOptionsItemSelected(item);
}

private void updateModelFromLayout() {      
alarmDetails.timeMinute = timePicker.getCurrentMinute().intValue();
alarmDetails.timeHour = timePicker.getCurrentHour().intValue();
alarmDetails.name = edtName.getText().toString();
alarmDetails.repeatWeekly = chkWeekly.isChecked();  
alarmDetails.setRepeatingDay(AlarmModel.SUNDAY, chkSunday.isChecked()); 
alarmDetails.setRepeatingDay(AlarmModel.MONDAY, chkMonday.isChecked()); 
alarmDetails.setRepeatingDay(AlarmModel.TUESDAY, chkTuesday.isChecked());
alarmDetails.setRepeatingDay(AlarmModel.WEDNESDAY,
chkWednesday.isChecked());  
alarmDetails.setRepeatingDay(AlarmModel.THURSDAY, 
chkThursday.isChecked());
alarmDetails.setRepeatingDay(AlarmModel.FRDIAY, chkFriday.isChecked());
alarmDetails.setRepeatingDay(AlarmModel.SATURDAY,
chkSaturday.isChecked());

alarmDetails.isEnabled = true;
}

}

Main XML


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true" >

<!--
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AlarmDetailsActivity" >
-->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".AlarmDetailsActivity" >

<TimePicker
android:id="@+id/alarm_details_time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<EditText
android:id="@+id/alarm_details_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/alarm_details_time_picker"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:ems="10"
android:hint="@string/details_alarm_name" />

<View
android:id="@+id/divider2"
style="@style/Divider"
android:layout_alignParentLeft="true"
android:layout_below="@+id/alarm_details_name" />

<com.trigg.alarmclock.CustomSwitch
 android:id="@+id/alarm_details_repeat_weekly"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignParentRight="true"
 android:layout_below="@+id/divider2"
 android:checked="true"
 android:text="@string/details_repeat_weekly" />

 <View
 android:id="@+id/divider1"
 style="@style/Divider"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/alarm_details_repeat_weekly" />

 com.trigg.alarmclock.CustomSwitch
 android:id="@+id/alarm_details_repeat_sunday"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignParentRight="true"
 android:layout_below="@+id/divider1"
 android:checked="true"
 android:text="@string/details_sunday" />

 <com.trigg.alarmclock.CustomSwitch
 android:id="@+id/alarm_details_repeat_monday"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignRight="@+id/alarm_details_repeat_sunday"
 android:layout_below="@+id/alarm_details_repeat_sunday"
 android:checked="true"
 android:text="@string/details_monday" />

 <com.trigg.alarmclock.CustomSwitch
 android:id="@+id/alarm_details_repeat_tuesday"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignRight="@+id/alarm_details_repeat_monday"
 android:layout_below="@+id/alarm_details_repeat_monday"
 android:checked="true"
 android:text="@string/details_tuesday" />

 com.trigg.alarmclock.CustomSwitch
 android:id="@+id/alarm_details_repeat_wednesday"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignRight="@+id/alarm_details_repeat_tuesday"
 android:layout_below="@+id/alarm_details_repeat_tuesday"
 android:checked="true"
 android:text="@string/details_wednesday" />

 <com.trigg.alarmclock.CustomSwitch
 android:id="@+id/alarm_details_repeat_thursday"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignRight="@+id/alarm_details_repeat_wednesday"
 android:layout_below="@+id/alarm_details_repeat_wednesday"
 android:checked="true"
 android:text="@string/details_thursday" />

 <com.trigg.alarmclock.CustomSwitch
 android:id="@+id/alarm_details_repeat_friday"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignRight="@+id/alarm_details_repeat_thursday"
 android:layout_below="@+id/alarm_details_repeat_thursday"
 android:checked="true"
 android:text="@string/details_friday" />

 <com.trigg.alarmclock.CustomSwitch
 android:id="@+id/alarm_details_repeat_saturday"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignRight="@+id/alarm_details_repeat_friday"
 android:layout_below="@+id/alarm_details_repeat_friday"
 android:checked="true"
 android:text="@string/details_saturday" />

 <View
 android:id="@+id/divider4"
 style="@style/Divider"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/alarm_details_repeat_saturday" />

 <LinearLayout
 android:id="@+id/alarm_ringtone_container"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/divider4"
 android:background="@drawable/view_touch_selector"
 android:orientation="vertical" >

 <TextView
 android:id="@+id/alarm_label_tone"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="@dimen/activity_vertical_margin"
 android:text="@string/details_alarm_tone"
 android:textSize="18sp" />

 <TextView
 android:id="@+id/alarm_label_tone_selection"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginBottom="@dimen/activity_vertical_margin"
 android:text="@string/details_alarm_tone_default"
 android:textSize="14sp" />
 </LinearLayout>

 <View
 android:id="@+id/divider3"
 style="@style/Divider"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/alarm_ringtone_container" />
 </RelativeLayout>

 </ScrollView>

 My problem and question is here with the ImagePicker activity and
 the setting of the image to ImageView.I can pick and set it to the
 ImageView,but have no clue on how to make it stay when the activity 
 is closed or destroyed. I need the same functionality for each 
 alarm as they will have different images in ImageView. I was
 thinking maybe use Preferences to keep track or use item position as
 way to keep track instead of file name.

 The following is what I have for the image picker activity:

 Main.Java


 public class MainActivity extends Activity {
 private Uri mImageCaptureUri;
 private ImageView mImageView;  

 private static final int PICK_FROM_CAMERA = 1;
 private static final int PICK_FROM_FILE = 2;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 setContentView(R.layout.main);

 final String [] items          = new String [] {"From Camera", "From SD
 Card"};                
 ArrayAdapter<String> adapter   = new ArrayAdapter<String> (this,
 android.R.layout.select_dialog_item,items);
 AlertDialog.Builder builder        = new AlertDialog.Builder(this);

 builder.setTitle("Select Image");
 builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
 public void onClick( DialogInterface dialog, int item ) {
 if (item == 0) {
 Intent intent   = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 File file       = new File(Environment.getExternalStorageDirectory(),
 "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
 mImageCaptureUri = Uri.fromFile(file);

 try {  
 intent.putExtra (android.provider.MediaStore.EXTRA_OUTPUT,
 mImageCaptureUri);

 intent.putExtra("return-data", true);

 startActivityForResult(intent, PICK_FROM_CAMERA);
 } catch (Exception e) {
 e.printStackTrace();
 }          

 dialog.cancel();
 } else {
 Intent intent = new Intent();

 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);

 startActivityForResult(Intent.createChooser(intent, "Complete action
 using"), PICK_FROM_FILE);
 }
 }
 } );

 final AlertDialog dialog = builder.create();

 mImageView = (ImageView) findViewById(R.id.test);

 ((Button) findViewById(R.id.btn_choose)).setOnClickListener(new
 View.OnClickListener() {           
 @Override
 public void onClick(View v) {
 dialog.show();
 }
 });
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent 
 data) {
 if (resultCode != RESULT_OK) return;

 Bitmap bitmap  = null;
 String path        = "";

 if (requestCode == PICK_FROM_FILE) {
 mImageCaptureUri = data.getData(); 
 path = getRealPathFromURI(mImageCaptureUri); //from Gallery 

 if (path == null)
 path = mImageCaptureUri.getPath(); //from File Manager

 if (path != null) 
 bitmap     = BitmapFactory.decodeFile(path);
 } else {
 path   = mImageCaptureUri.getPath();
 bitmap  = BitmapFactory.decodeFile(path);
 }

 mImageView.setImageBitmap(bitmap); 

 mImageView.setSelected(true);

 }

 public String getRealPathFromURI(Uri contentUri) {
 String [] proj         = {MediaStore.Images.Media.DATA};
 Cursor cursor      = managedQuery( contentUri, proj, null, null,null);

 if (cursor == null) return null;

 int column_index   =
 cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

 cursor.moveToFirst();

 return cursor.getString(column_index);
 }
 }

 Maim.XML

 <?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:padding="10dp">

 <TextView  
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="Android Image Choose Example"/>

 <Button 
 android:id="@+id/btn_choose"
 android:text="Choose Image"         
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
 android:layout_marginTop="10dp"/>

 <ImageView 
 android:id="@+id/test"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"       
 android:layout_marginTop="10dp"/>

 </LinearLayout>


 Any help will be most appreciated, thank you for time and interest.

 Mike

感谢所有对此问题感兴趣的人。经过大量的研究试验和错误,我的逻辑被证明是正确的。此时项目重点已更改为应用程序的一个新方面。迈克