2014-06-28 51 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

相关问题