网络摄像头应用程序在随机时间后停止工作

时间:2014-06-28 18:31:32

标签: android service webcam photo intentservice

如果我在程序中遇到基本错误或其他类似错误,我很抱歉,但我对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();
                    }
                }


}

1 个答案:

答案 0 :(得分:0)

我将尝试将计时器实例放在上层处理程序类中。我不确定,但我想如果你在方法中实例化了Timer对象,这个引用将在方法关闭后被销毁。然后,(GC)垃圾收集器将终止在创建时给予计时器实例的所有资源。

因此,从上层创建计时器实例,只需在方法内调用timer.schedule(...)。