[英]when android device onopen, run application
我有android包更新应用程序。 有时我们发布新的Android版本或bug修复包。 然后用户下载包并加载包。 我们还发布了强制更新,并下载并加载了包。 它必须使用广播和服务与服务器通信。
如果应用程序运行一次,它可以正常工作 但我们需要一些有关设备报告的信息。 如果用户未运行应用程序,则服务将无法运行。 所以应用程序无法正常工作。
为此,我们需要我们的应用程序运行Android设备打开。 所以服务将是活跃的。 并且app工作正常。
更新包来自不同公司的公司。 一旦包加载,他们应该更改包以启动我的应用程序?
如果您在询问是否可以在下载后立即启动任何服务,那么这是不可能的。 当应用程序从未启动时,您如何期望代码运行?
编辑:
在设备启动后启动应用程序所能做的就是在清单中设置一个带有intent过滤器的接收器。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".blablaService"
android:enabled="true"/>
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
您还需要一个BroadcastReceiver
,因此请将此类添加到您的源中。
public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class); //MyActivity can be anything which you want to start on bootup...
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
我不知道这是否也适用于更新设备,但这是可能的。 这将在设备启动时弹出应用程序。
我也想知道,一旦设备更新,是否真的需要这样做? 你不能在启动应用程序后检查Android版本。 然后将此版本保存到sharedPreferences。 下次运行时将当前版本与存储的版本进行比较。 如果它不相同,那么更新你的包。 这可能是一种替代解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.