質問

I have a class that takes a time from a user by time picker ,the time picker action should be compared with current phone time ,when current phone time is same with which user set ,the app should trigger a media player called mediaplayer like an alarm .now the problem is I cant check the timepicker time with the current phone time more than one time .

  import java.util.Calendar;

  import android.app.Activity;
  import android.content.Intent;
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.os.Handler;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TimePicker;
  import android.widget.Toast;

public class Sabah2 extends Activity {
Button btn;
Intent i;
int hour,min;
static TimePicker picker;
private Handler hh;
private Runnable rr;
Calendar c = Calendar.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sabah2);
    final MediaPlayer mp = MediaPlayer.create(Sabah2.this, R.raw.salam);
    picker = (TimePicker) findViewById(R.id.timePicker1);
    btn = (Button) findViewById(R.id.sabah_save);


    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            hour=picker.getCurrentHour();
            min=picker.getCurrentMinute();

            Toast.makeText(getApplicationContext(), "The Alarm has been             
   Activated At "+hour+":"+min, Toast.LENGTH_SHORT).show();


        }
    });
   if(hour==c.get(Calendar.HOUR)&&min==c.get(Calendar.MINUTE))
 {
mp.start();
 }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.sabah2, menu);
    return true;
}

 }
役に立ちましたか?

解決

You have to set the alarmanager for the time in the timepicker.If the time matches with current time it will trigger the alarm and play the mediaplayer .something like this

 AlarmManager alarmanager=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
 TimePicker ti = (TimePicker) findViewById(R.id.time_alarm);
ti.setOnTimeChangedListener(new OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            selectedhour = hourOfDay;
            selectedminute = minute;
            count = 2;
        }
    });

now convert the selectedhour and selectedminute into millisecond and set it to alarmanager

alarmanager.set(AlarmManager.RTC_WAKEUP, time in millisec,pndingIntent) ;

他のヒント

You have to use a service to check for the time.In this service check your time with current time.This will be in background and thus will try to check the instance of time even when your ap is not running.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top