【问题标题】:How to auto update a continuously running Android app without user interaction如何在没有用户交互的情况下自动更新持续运行的 Android 应用程序
【发布时间】:2014-06-27 07:58:58
【问题描述】:

我们在 Google Play 商店中有一个应用程序在前台持续运行。它运行的设备不受我们的控制,也没有root。它们在 Android 4.2 或 4.4 上运行。

我们的目标是让应用更新到我们通过 Play 商店发布的最新版本,而无需用户交互。重启设备将是唯一可接受的“交互”选项。

我们发现一个正在运行的应用在运行时即使开启了“自动更新”也不会自动更新。

实现目标的方法是什么?

【问题讨论】:

    标签: android auto-update google-play


    【解决方案1】:

    使用警报管理器来安排更新,然后使用创建类并扩展服务或 IntentService 类。检查是否有互联网连接,如果是,则继续更新如下:检查此链接Android Services - Tutorial 这样即使不显示您的活动,您也可以使用服务进行更新。

    创建警报管理器:

     Calendar cal = Calendar.getInstance();
    
    Intent intent = new Intent(this, MyService.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
    
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    // Start every 30 seconds
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
    

    服务:

        public class DownloadService extends IntentService {
    
      private int result = Activity.RESULT_CANCELED;
      public static final String URL = "urlpath";
      public static final String FILENAME = "filename";
      public static final String FILEPATH = "filepath";
      public static final String RESULT = "result";
      public static final String NOTIFICATION = "com.vogella.android.service.receiver";
    
      public DownloadService() {
        super("DownloadService");
      }
    
      // will be called asynchronously by Android
      @Override
      protected void onHandleIntent(Intent intent) {
        String urlPath = intent.getStringExtra(URL);
        String fileName = intent.getStringExtra(FILENAME);
        File output = new File(Environment.getExternalStorageDirectory(),
            fileName);
        if (output.exists()) {
          output.delete();
        }
    
        InputStream stream = null;
        FileOutputStream fos = null;
        try {
    
          URL url = new URL(urlPath);
          stream = url.openConnection().getInputStream();
          InputStreamReader reader = new InputStreamReader(stream);
          fos = new FileOutputStream(output.getPath());
          int next = -1;
          while ((next = reader.read()) != -1) {
            fos.write(next);
          }
          // successfully finished
          result = Activity.RESULT_OK;
    
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          if (stream != null) {
            try {
              stream.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
          if (fos != null) {
            try {
              fos.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
        publishResults(output.getAbsolutePath(), result);
      }
    
      private void publishResults(String outputPath, int result) {
        Intent intent = new Intent(NOTIFICATION);
        intent.putExtra(FILEPATH, outputPath);
        intent.putExtra(RESULT, result);
        sendBroadcast(intent);
      }
    } 
    

    【讨论】:

    • 请注意,上面的代码只是一个示例......来自我给出的链接
    • 这将下载更新,但应用更新的实际安装时间/地点是什么时候?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 2012-09-29
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多