2014-06-28 52 views
0

如果我在程序中遇到基本錯誤或類似問題,我很抱歉,但我對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(); 
        } 
       } 


} 
+0

是你在模擬器或真實設備上測試的。我不相信模擬器給任何相機相關的應用程序提供正確的反饋 – oat

+0

目前我正在測試三星Galaxy S4,但最終它應該爲HTC Legend(最低Android版本設置) – Mayrhofer

回答

0

我會嘗試將定時器實例放入上級處理程序類中。我不確定,但我猜如果你在方法內部實例化Timer對象,這個引用將在方法關閉後被銷燬。之後,(GC)垃圾收集器將終止創建時給定時器實例的所有資源。

因此,從上層類創建計時器實例,並在方法內調用timer.schedule(...)。

+0

對不起我沒有看到我在幾年前得到了這個問題的答案,在這裏問自己的問題,我很新。 我不認爲這是問題,因爲計時器的東西是爲我工作,如果我只是延遲整個應用程序幾秒鐘後延遲。如果我使用AsynTask或IntentService,它會在隨機時間後停止工作。 因此,目前對我而言,什麼是對我每5秒響應UI的不好方法,同時做我的東西,但不是解決方案,我喜歡它如何 你能跟着我嗎?^^ – Mayrhofer

相關問題