2011-01-27 92 views
1

我的应用程序中有一些文件,但其中只有3个文件很重要。这是一个提醒应用程序与报警声音和通知。 我有一个maincode.java文件,其中包含一个复选框及其侦听器。如果用户检查chechbox,AlarmManager会将意图发送到启动MyService.java的AlarmReceiver.java。 MyService java包含有关播放声音的代码。代码是部分的。 MyService.java:Android AlarmManager和服务问题

public void onCreate() { 
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onCreate"); 

    player = MediaPlayer.create(this, R.raw.sound); 
    player.setLooping(false); // Set looping 
} 

@Override 
public void onDestroy() { 
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onDestroy"); 
    player.stop(); 
} 

@Override 
public void onStart(Intent intent, int startid) { 
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onStart"); 
    player.start(); 
} 

AlarmReceiver.java:

public void onCreate() { 
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onCreate"); 

    player = MediaPlayer.create(this, R.raw.sound); 
    player.setLooping(false); // Set looping 

maincode.java的重要组成部分:

cb1 = (CheckBox) findViewById(R.id.CheckBox01); 
    cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     {   
      if (cb1.isChecked()) 
       { 
       if (GlobalVars.getHourOfDay() >= 0) 
       { 
        Toast.makeText(maincode.this, "ok", Toast.LENGTH_SHORT).show(); 
        rem1.setText(GlobalVars.getReminder1name()); 
         Intent intent = new Intent(maincode.this, AlarmReceiver.class); 
         PendingIntent pendingIntent = PendingIntent.getBroadcast(bInsulinReminder.this, 0, 
          intent, PendingIntent.FLAG_UPDATE_CURRENT); 
         AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
         Calendar cal = Calendar.getInstance(); 
         cal.set(Calendar.HOUR_OF_DAY, GlobalVars.getHourOfDay()); 
         cal.set(Calendar.MINUTE, GlobalVars.getMinute()); 
         cal.set(Calendar.SECOND, 0); 
         cal.set(Calendar.MILLISECOND, 0); 
         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+ 3000, 6000, pendingIntent); 

       } 
       Toast.makeText(maincode.this, "Checked", Toast.LENGTH_SHORT).show(); 
       } else { 
        rem1.setText("No reminder set"); 
        Toast.makeText(maincode.this, "Not checked", Toast.LENGTH_SHORT).show(); 
       } 
     } 

     }); 

(REM1是在提醒按钮,其文本取决于名任何用户想要的)

问题的代码是,如果我开始警报,我无法阻止它。我知道MyService.java中有player.stop()命令,但是我怎样才能从maincode.java的末尾调用它,其中复选框未选中?

回答

3

不,你不能直接从监听器那样做。您可以禁用报警是这样的:

Intent intent = new Intent(maincode.this, AlarmReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(bInsulinReminder.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
pendingItem.cancel(); 
alarmManager.cancel(pendingItem); 

或者,如果(我想)AlarmReceiver是实现广播接收器和距离的onReceive方法,你开始你的MyService这是实现服务类。

因此,如果您想要停止从maincode.java侦听器内部发出此警报,您可以通过重新创建您在AlarmReceiver中使用的PendingIntent并执行stopService方法来停止MyService。

希望有所帮助。