【问题标题】:Webcam Application stops working after a random time网络摄像头应用程序在随机时间后停止工作
【发布时间】:2014-06-28 18:31:32
【问题描述】:

如果我的程序出现基本错误或类似问题,我很抱歉,但我对 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();  
        }
}

网络摄像头定时器

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


}

【问题讨论】:

  • 你是在模拟器还是真实设备上测试这个。我不相信模拟器会对任何与相机相关的应用程序提供正确的反馈
  • 目前我正在三星 Galaxy S4 上进行测试,但最终它应该适用于 HTC Legend(已设置最低 Android 版本)

标签: android service webcam photo intentservice


【解决方案1】:

我将尝试将计时器实例放在您的上层处理程序类中。我不确定,但我猜如果您在方法中实例化 Timer 对象,则该引用将在方法关闭后被销毁。然后稍后 (GC)Garbage Collector 将杀死在创建时分配给计时器实例的所有资源。

所以从上层类创建计时器实例,然后在方法内部调用 timer.schedule(...)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2010-12-06
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    相关资源
    最近更新 更多