简体   繁体   English

如何在没有用户交互的情况下自动更新连续运行的Android应用

[英]How to auto update a continuously running Android app without user interaction

We have an app in the Google Play Store that runs in the foreground continuously. 我们在Google Play商店中有一个应用程序,它在前台不断运行。 The devices that it runs on are out of our control and are not rooted. 它运行的设备不受我们的控制,并且没有root。 They run on either Android 4.2 or 4.4. 它们运行在Android 4.2或4.4上。

Our goal is to have the app update to the newest version that we release via the Play Store without user interaction. 我们的目标是让应用程序更新到我们通过Play商店发布的最新版本,而无需用户交互。 Restarting the device would be the only acceptable "interaction" option. 重启设备将是唯一可接受的“交互”选项。

We find that a running app does not updated automatically when it is running even if the "automatic update" is turned on. 我们发现正在运行的应用程序在运行时不会自动更新,即使打开了“自动更新”也是如此。

What is the way to achieve our goal? 实现目标的方法是什么?

Use an Alarm Manager to scheduled your update and then use a create a class and extend the service or IntentService class. 使用警报管理器来安排更新,然后使用创建类并扩展服务或IntentService类。 Check if theres an internet connection if yes proceed to update like this: Check this link Android Services - Tutorial In this way you can update even not showing your Activity by using service. 检查是否有互联网连接,如果是,继续更新如下:检查此链接Android服务 - 教程通过这种方式,您甚至可以通过使用服务更新甚至不显示您的活动。

Creating the Alarm Manager: 创建警报管理器:

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

For service: 对于服务:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有 Google Play 或用户交互的情况下自动更新 Android 应用程序 - How to update android app automatically without Google Play or user interaction 如何在flutter中自动更新android应用程序而无需用户交互 - How to update android app automatically without user interaction in flutter 如何在没有用户交互和/或应用更新的情况下更改android视图 - How to change android views without user interaction and or app update 无需用户干预即可自动更新Android应用 - Auto update of Android app without user intervention 无需用户交互即可自动更新Android应用 - Automatically update an android app without requiring user interaction 如何在没有用户交互的情况下以编程方式卸载Android应用程序? - How to uninstall an Android app programmatically without user interaction? 连续运行后台服务和android中的ui交互 - running background service continuously and ui interaction in android 是否可以在没有人工干预的情况下使Android平板电脑应用自动更新? - Possible to make Android tablet app auto-update with no human interaction? 即使应用未运行,如何连续更新通知? - How to continuously update notification even when app is not running? 在android中无需用户交互即可通过whats App发送消息 - Sending message through whats App without user interaction in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM