1

我正在尝试构建一个我正在同时播放视频和图像的应用程序。每隔一小时左右,该应用程序必须从互联网上下载材料。所以为了使用新材料,我试图让它重新启动。我已经实现了一个可运行文件,并将下载和重新启动的代码放在运行方法中。问题是应用程序在启动后很快就冻结了-

synchronized public void run() {
    download("http://www.justieltsshaddi.com/pankaj/list.txt",
            Environment.getExternalStorageDirectory() + "/alpha/list.txt");
    File beta = new File(Environment.getExternalStorageDirectory()
            + "/beta/");
    File betalist = new File(beta + "/list.txt");
    File alpha = new File(Environment.getExternalStorageDirectory()
            + "/alpha/");
    File alphalist = new File(alpha + "/list.txt");
    if (alphalist.lastModified() == betalist.lastModified()) {
        return;
    }
    try {
        FileReader inAlpha = new FileReader(alphalist);
        BufferedReader br = new BufferedReader(inAlpha);
        String s;
        Toast.makeText(this, "Starting Download...", Toast.LENGTH_SHORT)
                .show();
        while ((s = br.readLine()) != null) {
            download("http://www.justieltsshaddi.com/pankaj" + "/" + s,
                    Environment.getExternalStorageDirectory() + "/alpha/"
                            + s);
        }
        // stop the activity to rename folders
        Toast.makeText(this, "Download done. Restarting...",
                Toast.LENGTH_SHORT).show();
        Log.d("Pankaj", "Download Done");
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();

        Log.d("Pankaj", "MainActivity Killed");
        // rename alpha to beta
        deleteSubFolders(beta.toString());
        alpha.renameTo(beta);
        if (!alpha.exists()) {
            alpha.mkdir();
        }
        File upper = new File(alpha + "/upper/");
        if (!upper.exists())
            upper.mkdirs();
        File lower = new File(alpha + "/lower/");
        if (!lower.exists())
            lower.mkdirs();
        // restart the activity
        overridePendingTransition(0, 0);
        startActivity(intent);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

任何帮助表示赞赏。提前致谢。

4

2 回答 2

0

我建议您使用 Aysnc Task 下载文件

private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
    try {

        //do your download 
        while ((s = br.readLine()) != null) {
        download("http://www.justieltsshaddi.com/pankaj" + "/" + s,
                Environment.getExternalStorageDirectory() + "/alpha/"
                        + s);
         }
    } catch (Exception e) {
    }
    return null;
}

如下调用下载任务 // 当必须触发下载器时执行此任务 DownloadFile downloadFile = new DownloadFile(); downloadFile.execute("你要下载的文件的url");

可能正在考虑循环触发下载任务。

于 2013-07-17T04:14:38.593 回答
0

使用服务和警报管理器

          Calendar cal = Calendar.getInstance();
          cal.add(Calendar.SECOND, 10);

          Intent intent = new Intent(this, reciever.class);

          PendingIntent pi = PendingIntent.getBroadcast(context, _id, i, 0);

          AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
          //for 30 mint 60*60*1000
          alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                       60*60*1000, pi );

在广播接收器中启动服务

public class reciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

}
于 2013-07-17T04:25:59.763 回答