我正在构建一个应该在启动时启动的应用程序。它基于WebView。不幸的是,我首次启动应用时会收到ERR_NAME_NOT_RESOLVED或类似内容。我认为这是因为设备未正确执行正确连接到Internet所需的所有任务/设置。我引用this question似乎也提出了类似的问题,但似乎接受的答案不适合更新版本的Android。此外,我不是在寻找用户何时激活Wi-Fi或移动数据,而是在设备本身准备好访问网页时。
我最初的想法是创建一个启动画面以延迟应用程序。我引用了此post并添加了对wait()
的调用(MainActivity包含WebView)。
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Set;
public class LaunchActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
final SharedPreferences.Editor editor = preferences.edit();
startLockTask();
editor.putBoolean("IsLocked", true);
editor.commit();
if(preferences.getBoolean("configured", false)) {
try {
wait(3000);
} catch (InterruptedException ex) {
System.out.print("Interrupted");
}
Intent i = new Intent(LaunchActivity.this, MainActivity.class);
startActivity(i);
finish();
} else {
Intent i = new Intent(LaunchActivity.this, Setup.class);
i.putExtra("Auth", true);
startActivity(i);
finish();
}
}
}
然而,这导致java.lang.RuntimeException: Unable to start activity ComponentInfo{midamcorp.com.cargoview/midamcorp.com.cargoview.LaunchActivity}: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
。
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="midamcorp.com.cargoview">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label= "@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:lockTaskMode="if_whitelisted"
android:screenOrientation="portrait"></activity>
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".Setup" />
<activity
android:name=".LaunchActivity"
android:lockTaskMode="if_whitelisted"
android:theme="@style/SplashTheme"
android:screenOrientation="portrait">
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="@string/title_activity_login"></activity>
</application>
</manifest>
BootReceiver:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context c, Intent i) {
final AlarmManager manager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
if (i.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent launch = new Intent(c, LaunchActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(launch);
}
}
}
在当前的Android开发中,最佳做法是什么?
感谢。
答案 0 :(得分:0)
在最新的操作系统上,当您的设备启动时,您可以安排在互联网连接可用时将触发的JobScheduler。触发JobScheduler后,您可以按如下方式开始活动:
@Override
public boolean onStartJob(final JobParameters params) {
Intent launch = new Intent(c, LaunchActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(launch);
return true;
}
您可以找到安排作业的教程here