Android標準Timer和標準LOGFile的MVC架構寫法
Timer(透過m_intID來分辨哪個UI並執行對應TIMER,不但可把所有控制寫在一起但UI又可獨立工作不互相干擾)MVC–>C
LOGFile(具有時間序的標準記錄方式)MVC–>M
MVC—>V
package com.example.android_timer_logfile;
import com.UITimer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
//MVC——->V
public class MainActivity extends Activity {
public UITimer m_Timer;
public TextView m_TV01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_TV01=(TextView)this.findViewById(R.id.textView1);
m_Timer=new UITimer((Context)this,1000,0);
m_Timer.SetTimer();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
|
MVC—>C
package com;
import java.util.Calendar;
import com.example.android_timer_logfile.MainActivity;
//MVC——->C
import android.content.Context;
import android.os.Handler;
public class UITimer {
public Handler m_HandlerTimer;
public Context m_Context;
public int m_intID;
public int m_intSleep;
public void MainActivity_fun1()
{
MainActivity Ma=(MainActivity)m_Context;
Calendar dt = Calendar.getInstance();
int thisYear = dt.get(Calendar.YEAR);
int thisMonth = dt.get(Calendar.MONTH)+1;
int thisDate = dt.get(Calendar.DAY_OF_MONTH);
int thisHour = dt.get(Calendar.HOUR_OF_DAY);
int thisMin = dt.get(Calendar.MINUTE);
int thisSec = dt.get(Calendar.SECOND);
Ma.m_TV01.setText(Integer.toString(thisHour)+”:” + Integer.toString(thisMin)+”:” + Integer.toString(thisSec));
String FileName = String.format(“%d%02d%02d.log”, thisYear,thisMonth,thisDate);
LogFile.WriteData2File(“ATL”, FileName, “Timer_Run”);
SetTimer();
}
private final Runnable TimerRun = new Runnable()
{
public void run()
{
if(m_intID==0)
{
MainActivity_fun1();
}
}
};
public void SetTimer()
{
m_HandlerTimer.postDelayed(TimerRun, m_intSleep);//啟動Timer
}
public UITimer(Context Context,int sleep,int ui)
{
m_intID=ui;
m_Context=Context;
m_intSleep=sleep;
m_HandlerTimer=new Handler();
LogFile.CreateAppFloder(“ATL”);
}
}
|
MVC—>M
package com;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
//MVC——->M
import android.os.Environment;
public class LogFile {
public static void CreateAppFloder(String StrDir) // 建立APP 資料夾
{
File sd=Environment.getExternalStorageDirectory();
String path=sd.getPath()+”/”+StrDir;
File floder=new File(path);
String logpath=sd.getPath()+”/”+StrDir+”/”+StrDir+”.log”;
File logfile=new File(logpath);
if(!floder.exists())
{
floder.mkdir();
}
else
{
if(logfile.exists())
{
logfile.delete();
}
}
}
public static void WriteData2File(String StrDir,String FileName,String StrMsg)//
{
File sd=Environment.getExternalStorageDirectory();
String Dir=StrDir;
CreateAppFloder(Dir);
String logpath=sd.getPath()+”/”+Dir+”/”+FileName;
try
{
FileWriter out = new FileWriter(logpath,true);
SimpleDateFormat formatter;
formatter = new SimpleDateFormat (“HH:mm:ss”);
Calendar cal = Calendar.getInstance();
String ctime = formatter.format(cal.getTime());
StrMsg=ctime+”_”+StrMsg;
out.write((StrMsg+”\n”));
out.close();
}
catch(IOException ioe){
System.out.print(ioe);
}
}
}
|