问题描述:

I have created a small app, but I want it to execute automatically at a particular time. The time must be entered by the user.

For example :

If my activity is, to start a particular game and I enter 5:30pm as the time, it should run that game at 5:30pm automatically, even if I close the application after I entered time.

网友答案:

Create an alarm and a broadcast reciever for it, put the intent for this in the Android Manifest

Take a look at http://blog.mikesir87.io/2013/04/android-creating-an-alarm-with-alarmmanager/

Just set the alarms for a week in advanced.


For the touch thing try this replace:

Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
        stopAlarm.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent arg1) {
                mMediaPlayer.stop();
                finish();
                return false;
            }
        });

With

Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
        stopAlarm.setOnClickListener(new OnTouchListener() {
            @Override
            public void onClick(View arg0) {
                mMediaPlayer.stop();
                finish();
            }
        });

The reason we dont need to use arg0 or arg1 is that we already know where the touch or click is coming from, in the situation where you have a activity wide click listener (so the activity implements onClickListener) you would use something like this:

        @Override
        public void onClick(View arg0) {
            switch(arg0){
                  case R.id.id1:
                  //Do what you want here
                  break;
                  case R.id.id2:
                  //Do what you want here
                  break;
                  default:
                  //Do default stuff
                  break;
            }
        }

To my understanding however I believe you don't need any of this stuff however correct? You just want your activity to run at these times right?

So if I am correct you just need to do this:

public class AlarmReceiverActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm);

        //Do what you want here
        //This is now your activity to play with :D
    }

}
相关阅读:
Top