0

I am building an application that is supposed to launch at start-up. It is based around a WebView. Unfortunately, I typically receive an ERR_NAME_NOT_RESOLVED or similar when the app is first launched. I presume this is because the device has not properly performed all tasks/setup necessary to properly connect to the Internet. I have referenced this question which seems to pose a similar problem however, it appears the accepted answer is unsuitable for more recent versions of Android. Additionally, I am not looking for when the user activates Wi-Fi or mobile data, but rather when the device itself is ready to access webpages.

My initial idea was to create a Splash Screen to delay the app. I referenced this post and added a call to wait() (MainActivity contains the 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();
        }

    }
}

This, however, results in 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().

My manifest:

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

What is the best practice for this in current Android development?

Thanks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
KellyM
  • 2,472
  • 6
  • 46
  • 90

1 Answers1

0

On latest OS, when your device is booted, you can schedule a JobScheduler to which will be triggered when internet connection is available. When the JobScheduler is triggered, you can start your activity as follows:

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

You can find the tutorial to schedule a job here

Sagar
  • 23,903
  • 4
  • 62
  • 62