I would like to compile an Android application, the function is this: there is a period of time, such as 08:00:00 to 09:00:00, like in this period of time to play video files.
My idea is this: I. Activity if the start time is between 8:00 to 9:00, then played directly into the 9:00 stop;
Scenario 2, if you start Activity time is not between 8:00 to 9:00, then set a AlarmManager, my question is, in this class AlarmReceiver extends BroadcastReceiver in the onReceive method, there should be how to write ah?
I do not know if it is the wrong way, so I want to ask you, thank<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Write a service bar, every once in a while to get the system time to the specified time on, off activity
Reply:
Upstairs is said not to use the alarm mechanism to deal with it?
I am unfamiliar service, do not know how to do
Reply:
service write thread ah, one minute if the current time is not 8:00, service can be found in the online demo, android help documentation is also very good, I personally think service is good, so you can automatically pop up player, of course, is to start service boot time slightly, the boot will be broadcast brother, started service
Reply:
Then I look at service
Reply:
Ah, with the service, to recommend to you an article http://hi.baidu. com/xtlp/blog/item/e6e18810ad6c030c213f2eaf.html
Reply:
.
Reply:
Start service
Reply:
In the service regularly play video on how to achieve it?
I want it not time to start the activity, but the activity has been in operation, the time to play the video on.
Reply:
Well this is more simple, with the handler, as the thread of control
Reply:
public class PlayerByTimeActivity extends Activity {
public static final String TAG = "PlayerByTimeActivity";
public static final String SDCARD = Environment.getExternalStorageDirectory () + "/" ;/ / path
SD card
public SurfaceView surfaceView = null;
private PlayerVideo playerVideo;
private static PlayerByTimeActivity playerByTimeActivity = null;
public PlayerByTimeActivity () {
playerVideo = new PlayerVideo ();
}
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
playerByTimeActivity = this;
surfaceView = (SurfaceView) this.findViewById (R.id.surfaceview);
String beginTime = "21:32:00";
String endTime = "23:00:00";
try {
/ / Determine whether the current time and end time of the period at the beginning of time
boolean isPlay = isInTimes (beginTime, endTime);
if (isPlay) {/ / if within this time, then the direct broadcast
playing ();
} Else {/ / if not within this period of time, set a timer
Date when = getDateByTime (beginTime);
Timer timer = new Timer ();
timer.schedule (timerTask, when);
Toast.makeText (PlayerByTimeActivity.this, "! Current time there is no need to play the file", Toast.LENGTH_LONG) show ();.
}
} Catch (ParseException e) {
Toast.makeText (PlayerByTimeActivity.this, "start time or end time format is not correct!", Toast.LENGTH_LONG) show ();.
e.printStackTrace ();
}
}
private TimerTask timerTask = new TimerTask () {
@ Override
public void run () {
/ / In the run method requires the use sendMessage method to send a message
Message message = new Message ();
message.what = 1;
firstHandler.sendMessage (message) ;/ / send the task to a message queue
}
};
private Handler firstHandler = new Handler () {
public void handleMessage (Message msg) {
switch (msg.what) {
case 1:
Log.i (TAG, "handleMessage ()");
playing ();
break;
default:
super.handleMessage (msg);
}
}
};
@ Override
protected void onPause () {
Log.i (TAG, "onPause ()");
super.onPause ();
}
@ Override
protected void onRestart () {
Log.i (TAG, "onRestart ()");
super.onRestart ();
}
@ Override
protected void onResume () {
Log.i (TAG, "onResume ()");
super.onResume ();
}
@ Override
protected void onStart () {
Log.i (TAG, "onStart ()");
super.onStart ();
}
@ Override
protected void onStop () {
Log.i (TAG, "onStop ()");
super.onStop ();
}
@ Override
protected void onDestroy () {
Log.i (TAG, "onDestroy ()");
playerVideo.close ();
super.onDestroy ();
}
public static PlayerByTimeActivity getApp () {
return playerByTimeActivity;
}
public void playing () {
playerVideo.setView (surfaceView);
}
/ ** Determine whether the current time and end time of the time at the beginning of this period
* Cross-day time period is also handling
* Start time and end time of the time period does not exceed 24 hours
* @ Param beginTime start time
* @ Param endTime End time
* @ Throws ParseException
* /
public boolean isInTimes (String beginTime, String endTime) throws ParseException {
Date date = new Date ();
Calendar calendar = Calendar.getInstance ();
SimpleDateFormat sd = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss");
String strDate = sd.format (date);
String strDay = strDate.substring (0, 11);
String strBeginDate = strDay + beginTime;
String strEndDate = strDay + endTime;
Date beginDate = sd.parse (strBeginDate);
Date endDate = sd.parse (strEndDate);
/ / First, determine whether the start time and end time across days
/ / Of course, assuming the start time and end time of the time period is less than 24 hours
if (beginDate.before (endDate)) {/ / do not cross day
if (date.before (beginDate) | | date.after (endDate)) {
return false;
} Else {
return true;
}
} Else {/ / Cross-day
/ / Get tomorrow's date
according to today's dateint nowDate = calendar.get (Calendar.DAY_OF_YEAR);
calendar.set (Calendar.DAY_OF_YEAR, nowDate +1);
Date nextDate = calendar.getTime ();
String strNextDate = sd.format (nextDate);
/ / End date for processing
String strDayNextDate = strNextDate.substring (0, 11);
String strEndDateNew = strDayNextDate + endTime;
Date endDateNew = sd.parse (strEndDateNew);
if (date.before (beginDate) | | date.after (endDateNew)) {
return false;
} Else {
return true;
}
}
}
/ ** According only hours, minutes, seconds to return a current date Calendar object
* @ Param time Time 08:00:00
* /
public Calendar getCalendarByTime (String time) {
Calendar calendar = Calendar.getInstance ();
int hour = Integer.parseInt (time.substring (0, 2));
int minute = Integer.parseInt (time.substring (3, 5));
int second = Integer.parseInt (time.substring (6, 8));
/ / Set the calendar time, is designed to allow the calendar date and the current synchronization
calendar.setTimeInMillis (System.currentTimeMillis ());
calendar.set (Calendar.HOUR_OF_DAY, hour);
calendar.set (Calendar.MINUTE, minute);
calendar.set (Calendar.SECOND, second);
calendar.set (Calendar.MILLISECOND, 0);
return calendar;
}
/ ** According only hours, minutes, seconds to return a Date object
current date* @ Param time Time 08:00:00
* @ Throws ParseException
* /
public Date getDateByTime (String time) throws ParseException {
Date nowDate = new Date ();
SimpleDateFormat sd = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss");
String strNowDate = sd.format (nowDate);
String strDate = strNowDate.substring (0, 11) + time;
Date date = sd.parse (strDate);
return date;
}
}
This is what I just wrote the code, if the current time in the start and end times of the words, it can be played.
However, if the current time is not the beginning and the end of time, then wait until after the beginning of time, it can not be played.
Time to, Log.i (TAG, "handleMessage ()"); phrase can be printed out in LogCat in,
Just can not play the video, which is why?
Reply:
Their top ......
Reply:
End quote within five days
Reply:
Results posted yet?
I now have an activity which demand has been running non-stop running after updating the display data from the service could have been passed over to the hands of people every day requires setting range is now required after regular daily range is automatically set to do this kind of work ...... < br />Seeking advice ......
Reply:
The landlord is now solved yet, I have encountered ...
No comments:
Post a Comment