我想编个Android程序,功能是这样的:有一个时间段,比如08:00:00到09:00:00,想在这个时间段内播放视频文件。
我的想法是这样的:情况一,如果启动Activity的时间是在8点到9点之间,则直接播放,到9点则停止;
情况二,如果启动Activity的时间是不在8点到9点之间,则设置一个AlarmManager,我的问题是,在这个类AlarmReceiver extends BroadcastReceiver里的onReceive方法里,应该怎么写啊?
也不知道是不是我用的方法不对,所以想请教一下各位,先谢谢了
我的想法是这样的:情况一,如果启动Activity的时间是在8点到9点之间,则直接播放,到9点则停止;
情况二,如果启动Activity的时间是不在8点到9点之间,则设置一个AlarmManager,我的问题是,在这个类AlarmReceiver extends BroadcastReceiver里的onReceive方法里,应该怎么写啊?
也不知道是不是我用的方法不对,所以想请教一下各位,先谢谢了
我对service不熟,不知道怎么做
public class PlayerByTimeActivity extends Activity {
public static final String TAG = "PlayerByTimeActivity";
public static final String SDCARD = Environment.getExternalStorageDirectory() + "/";//SD卡的路径
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 {
//判断当前时间是否在开始时间与结束时间的这个时间段内
boolean isPlay = isInTimes(beginTime, endTime);
if (isPlay) {//如果在这段时间之内,则直接播放
playing();
} else {//如果不在这段时间之内,则设置一个定时器
Date when = getDateByTime(beginTime);
Timer timer = new Timer();
timer.schedule(timerTask, when);
Toast.makeText(PlayerByTimeActivity.this, "当前时间没有需要播放的文件!", Toast.LENGTH_LONG).show();
}
} catch (ParseException e) {
Toast.makeText(PlayerByTimeActivity.this, "开始时间或结束时间格式不正确!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
private TimerTask timerTask = new TimerTask() { @Override
public void run() {
//在run方法中需要使用sendMessage方法发送一条消息
Message message = new Message();
message.what = 1;
firstHandler.sendMessage(message);//将任务发送到消息队列
}
};
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);
}
/**判断当前时间是否在开始时间与结束时间的这个时间段内
* 对跨天的时间段也有处理
* 开始时间与结束时间的这个时间段不超过24小时
* @param beginTime 开始时间
* @param endTime 结束时间
* @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);
//首先判断开始时间与结束时间是否跨天
//当然,这里假设开始时间与结束时间的这个时间段是不超过24小时的
if (beginDate.before(endDate)) {//不跨天
if (date.before(beginDate) || date.after(endDate)) {
return false;
} else {
return true;
}
} else {//跨天
//根据今天的日期获取明天的日期
int nowDate = calendar.get(Calendar.DAY_OF_YEAR);
calendar.set(Calendar.DAY_OF_YEAR, nowDate+1);
Date nextDate = calendar.getTime();
String strNextDate = sd.format(nextDate);
//对结束日期进行处理
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;
}
}
}
/**根据只有时,分,秒的时间返回一个当前日期的Calendar对象
* @param 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));
//设置日历的时间,主要是让日历的年月日和当前同步
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;
}
/**根据只有时,分,秒的时间返回一个当前日期的Date对象
* @param 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;
}
}
这是我刚写的代码,如果当前时间在开始和结束时间之内的话,是可以播放的。
但是如果当前时间不在开始和结束时间之内,则等到开始时间之后,就播放不了。时间到了,Log.i(TAG, "handleMessage()");这句还能在LogCat中打印出来,
就是播放不了视频,这是为什么呢?
我现在也有这需求 一个一直运行的activity不停的更新显示来自service传过来的数据 本来是每天需要人来动手设置范围后运行起来 现在要求是每天定时自动设置范围后就做这样的工作……
求指教……