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

Android 网络摄像头应用程序在随机时间后停止工作,android,service,webcam,photo,intentservice,Android,Service,Webcam,Photo,Intentservice,如果我的程序中出现了基本错误或类似错误,我很抱歉,但我对安卓系统还是相当陌生的。 我创建了一个应用程序,在给定的时间后拍摄一张照片,存储并上传到服务器。我使用了一个后台服务来上传,但我不确定它是否创建正确。 问题是应用程序总是在一段随机时间后关闭,我不明白为什么会发生这种情况。如果你能提出一些解决这个问题的想法或建议,我会非常高兴的,谢谢 我的计时器 public void timedWebcam() { Timer myTimer = new Timer(); //Initialize t

如果我的程序中出现了基本错误或类似错误,我很抱歉,但我对安卓系统还是相当陌生的。 我创建了一个应用程序,在给定的时间后拍摄一张照片,存储并上传到服务器。我使用了一个后台服务来上传,但我不确定它是否创建正确。 问题是应用程序总是在一段随机时间后关闭,我不明白为什么会发生这种情况。如果你能提出一些解决这个问题的想法或建议,我会非常高兴的,谢谢

我的计时器

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();  
        }
}
网络摄像机

    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();
                }
            }
        });
    }
}
最后是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();
                    }
                }


}

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


因此,从上层类创建计时器实例,并在方法内部调用timer.schedule(…)

你是在仿真器上还是在真实设备上进行测试。在我在三星Galaxy S4上进行测试时,我不相信模拟器会对任何与摄像头相关的app给出正确的反馈,但最终它应该适用于HTC Legend(设置了最低的Android版本)
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();
                    }
                }


}