如果我在程序中遇到基本錯誤或類似問題,我很抱歉,但我對Android很新。 我創建了一個應用程序,可以在給定的時間後拍照,存儲並上傳到服務器。我使用BackgroundService進行上傳,但我不確定它是否正確創建。 問題是,應用程序隨機時間後總是關閉,我不明白爲什麼會發生這種情況。對於一些想法或建議如何解決這個問題真的很高興,謝謝!網絡攝像頭應用程序在隨機時間後停止工作
我的計時器
public void timedWebcam()
{
Timer myTimer = new Timer();
//Initialize the Timer Task
WebcamTimer webcamTimer = new WebcamTimer();
if(intervall != 0)
{
//Starting the Timer
myTimer.scheduleAtFixedRate(webcamTimer, 0, 60000*intervall);
}
else
{
Toast.makeText(this, "interval not set", Toast.LENGTH_LONG).show();
}
}
WebcamTimer
private class WebcamTimer extends TimerTask
{
@Override
public void run()
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
Date date = new Date();
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
actualHour = calendar.get(Calendar.HOUR_OF_DAY);
System.out.println("Actual Hour= "+actualHour);
System.out.println("Start Time= "+startTime);
System.out.println("Stop Time= "+stopTime);
if((actualHour >= startTime) && (actualHour < stopTime))
{
takePhoto(); //Takes the Picture
uploadToFTP(); //Uploads taken Picture to FTP
}
else
{
nightMode();
}
}
});
}
}
服務調用
Intent intent = new Intent(this,UploadService.class);
this.startService(intent);
最後UploadService類
public class UploadService extends IntentService
{
//Server Properties
public String server = "sample.aon.at";
public int port = addPort;
public String user = "user";
public String pass = "password";
FTPClient ftpClient = new FTPClient();
public UploadService()
{
super("UploadService");
}
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return null;
}
@Override
protected void onHandleIntent(Intent intent)
{
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
try
{
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.changeWorkingDirectory("webcam");
// uploads file using an InputStream
File firstLocalFile = new File(MainActivity.path);
System.out.println("Path used for upload: "+MainActivity.path);
String firstRemoteFile = "webcam.jpg";
InputStream inputStream = new FileInputStream(firstLocalFile);
boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
inputStream.close();
if (done)
{
System.out.println("Upload finished");
}
}
catch (IOException ex)
{
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
}
finally
{
try {
if (ftpClient.isConnected())
{
ftpClient.logout();
ftpClient.disconnect();
stopSelf();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
是你在模擬器或真實設備上測試的。我不相信模擬器給任何相機相關的應用程序提供正確的反饋 – oat
目前我正在測試三星Galaxy S4,但最終它應該爲HTC Legend(最低Android版本設置) – Mayrhofer