suchen

Heim  >  Fragen und Antworten  >  Hauptteil

java - Android中一个基本的闹钟程序,为什么不响呢?

public class ZZZActivity extends Activity {

public static final String WALLPAPER_FILE="wallpaper_file";
private static int RESULT_LOAD_IMAGE = 1;

private LinearLayout layout;
private Button Cancel;
private TextView Alarm;
private LinearLayout AlarmOne;
private CheckBox RepeatingCheck;
boolean isClicked = false , isLate = false;

int count = 1,option = 0;;

private Calendar c = Calendar.getInstance();
AlertDialog builder = null;  

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zzz);
    
    //layout = (LinearLayout)getLayoutInflater().inflate(R.layout.activity_main,null);
    layout = (LinearLayout)findViewById(R.id.layout);    
    
    
    RepeatingCheck = (CheckBox)findViewById(R.id.repeatingCheck);
    RepeatingCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
        @Override
        public void onCheckedChanged(CompoundButton buttonView, 
                boolean isChecked) { 
            
            isClicked = true;
        } 
    }); 
    
    Cancel = (Button)findViewById(R.id.cancel);
    Cancel.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(ZZZActivity.this, ZZTActivity.class);
            PendingIntent pi = PendingIntent.getBroadcast(ZZZActivity.this, 0,
                    intent, 0);
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            //取消警报
            am.cancel(pi);
            Alarm.setText("闹钟取消");
    
        }
        
    });
    
    Alarm = (TextView)findViewById(R.id.alarm);
    
    AlarmOne = (LinearLayout)findViewById(R.id.alarm_one);
    AlarmOne.setOnClickListener(new LinearLayout.OnClickListener(){
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            c.setTimeInMillis(System.currentTimeMillis());  
            int mHour_0=c.get(Calendar.HOUR_OF_DAY);  
            int mMinute_0=c.get(Calendar.MINUTE);  
                 
                
               new TimePickerDialog(ZZZActivity.this,  
                 new TimePickerDialog.OnTimeSetListener()  
                 {                  
                   public void onTimeSet(TimePicker view,int hourOfDay,  
                                         int minute)  
                   {  
                       c.setTimeInMillis(System.currentTimeMillis());  
                         
                         c.set(Calendar.HOUR_OF_DAY,hourOfDay);  
                         c.set(Calendar.MINUTE,minute);  
                         c.set(Calendar.SECOND,0);  
                         c.set(Calendar.MILLISECOND,0);  
                         
                         long date = c.getTimeInMillis();
                         
                         if(c.getTimeInMillis() < System.currentTimeMillis()){ 
                             c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 1); 
                         } 
                           
                         Intent intent = new Intent(ZZZActivity.this, ZZTActivity.class);  
                         PendingIntent sender=PendingIntent.getBroadcast(  
                                 ZZZActivity.this,0, intent, 0);  
                         AlarmManager am;  
                         am = (AlarmManager)getSystemService(ALARM_SERVICE);  
                         am.set(AlarmManager.RTC_WAKEUP,  
                                c.getTimeInMillis(),  
                                sender  
                          );  
                         
                         String str = format(hourOfDay)+":"+format(minute);  
                         Alarm.setText(str);
                         SharedPreferences textChange = getPreferences(0);  
                         SharedPreferences.Editor editor_1 = textChange.edit();  
                         editor_1.putString("TIME1", str);  
                         editor_1.commit();
                  
                         SharedPreferences timeShare = getPreferences(0);  
                         SharedPreferences.Editor editor = timeShare.edit();  
                         editor.putString("TIME1", str);  
                         editor.commit();  
                           
                         Toast.makeText(ZZZActivity.this,"您设置的闹钟时间为"+str,  
                           Toast.LENGTH_SHORT)  
                           .show();
                       
                     if(!isClicked){
                         
                         am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 
                                    1 * 60 * 1000, sender);
                         isClicked = false;
                     }
                   }            
                 },mHour_0,mMinute_0,true).show();  
        }    
    });
    
}

    
 private String format(int x)  
 {  
    String s=""+x;  
   if(s.length()==1) s="0"+s;  
   return s;  
}  
 

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

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        
        layout.setBackgroundResource(columnIndex);
        
       

    }
}

}

伊谢尔伦伊谢尔伦2807 Tage vor821

Antworte allen(1)Ich werde antworten

  • ringa_lee

    ringa_lee2017-04-18 10:30:21

    AlarmManager 没有设置CPU唤醒吧

    Antwort
    0
  • StornierenAntwort