-
-
Save bennofs/e955daeecf1702c8770fadc6ecff672f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/app/build.gradle b/app/build.gradle | |
| index 6d9100f1dfb4d160333b81657651a689c30e27bd..c4528b0 100644 | |
| --- a/app/build.gradle | |
| +++ b/app/build.gradle | |
| @@ -60,6 +60,8 @@ dependencies { | |
| implementation 'android.arch.persistence.room:runtime:1.0.0' | |
| annotationProcessor 'android.arch.persistence.room:compiler:1.0.0' | |
| testImplementation 'android.arch.persistence.room:testing:1.0.0' | |
| + androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3' | |
| + | |
| } | |
| task launch() { | |
| diff --git a/app/src/androidTest/java/de/carli_card/AndroidManifest.xml b/app/src/androidTest/java/de/carli_card/AndroidManifest.xml | |
| deleted file mode 100644 | |
| index e69de29..0000000 | |
| diff --git a/app/src/androidTest/java/de/carli_card/BackgroundService/PollServiceTest.java b/app/src/androidTest/java/de/carli_card/BackgroundService/PollServiceTest.java | |
| index 38e0a12d55745fd69f7c86d7b2504865f3cf99f4..ceae5de 100644 | |
| --- a/app/src/androidTest/java/de/carli_card/BackgroundService/PollServiceTest.java | |
| +++ b/app/src/androidTest/java/de/carli_card/BackgroundService/PollServiceTest.java | |
| @@ -1,4 +1,35 @@ | |
| package de.carli_card.BackgroundService; | |
| +import android.support.test.InstrumentationRegistry; | |
| +import android.support.test.filters.SdkSuppress; | |
| +import android.support.test.runner.AndroidJUnit4; | |
| +import android.support.test.uiautomator.UiDevice; | |
| + | |
| +import org.junit.Test; | |
| +import org.junit.runner.RunWith; | |
| + | |
| +import java.io.IOException; | |
| + | |
| +@RunWith(AndroidJUnit4.class) | |
| +@SdkSuppress(minSdkVersion = 18) | |
| public class PollServiceTest { | |
| -} | |
| + | |
| + private static final String BASIC_SAMPLE_PACKAGE | |
| + = "com.example.android.testing.uiautomator.BasicSample"; | |
| + private static final int LAUNCH_TIMEOUT = 5000; | |
| + private static final String STRING_TO_BE_TYPED = "UiAutomator"; | |
| + private UiDevice mDevice; | |
| + | |
| + @Test | |
| + public void startMainActivityFromHomeScreen() throws IOException { | |
| + // Initialize UiDevice instance | |
| + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); | |
| + | |
| + // Start from the home screen | |
| + mDevice.pressHome(); | |
| + | |
| + // Simulate restart | |
| + mDevice.executeShellCommand("stop"); | |
| + mDevice.executeShellCommand("start"); | |
| + } | |
| +} | |
| \ No newline at end of file | |
| diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml | |
| index 276a82107a7288409d32fccbff52aa6fa4f4e5a4..ce08479 100644 | |
| --- a/app/src/main/AndroidManifest.xml | |
| +++ b/app/src/main/AndroidManifest.xml | |
| @@ -6,6 +6,7 @@ | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| <uses-permission android:name="android.permission.VIBRATE" /> | |
| + <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> | |
| <!-- | |
| The ACCESS_COARSE/FINE_LOCATION permissions are not required to use | |
| @@ -79,6 +80,14 @@ | |
| <service android:name=".BackgroundService.PollService" /> | |
| + <receiver | |
| + android:name=".BackgroundService.BootCompleted" | |
| + android:enabled="true"> | |
| + <intent-filter> | |
| + <action android:name="android.intent.action.BOOT_COMPLETED" /> | |
| + </intent-filter> | |
| + </receiver> | |
| + | |
| <!-- | |
| The API key for Google Maps-based APIs is defined as a string resource. | |
| (See the file "res/values/google_maps_api.xml"). | |
| diff --git a/app/src/main/java/de/carli_card/BackgroundService/BootCompleted.java b/app/src/main/java/de/carli_card/BackgroundService/BootCompleted.java | |
| index 916e59b663d60e3ad68497053517fceaf1fb1a23..8160cfa 100644 | |
| --- a/app/src/main/java/de/carli_card/BackgroundService/BootCompleted.java | |
| +++ b/app/src/main/java/de/carli_card/BackgroundService/BootCompleted.java | |
| @@ -1,4 +1,29 @@ | |
| package de.carli_card.BackgroundService; | |
| -public class BootCompleted { | |
| +import android.content.BroadcastReceiver; | |
| +import android.content.Context; | |
| +import android.content.Intent; | |
| +import android.util.Log; | |
| + | |
| +import java.util.Random; | |
| + | |
| +/** Dieser BroadcastReceiver wird vom Android-System aufgerufen, wenn das Handy gebootet ist. | |
| + * | |
| + * Hier wird der Alarm für den Background-Service gesetzt, damit neue Angebote abgerufen werden | |
| + * auch ohnen das der Nutzer die App nach Neustart des Handys erst manuell starten muss. | |
| + */ | |
| +public class BootCompleted extends BroadcastReceiver { | |
| + | |
| + @Override | |
| + public void onReceive(Context context, Intent intent) { | |
| + Log.i("BGNotify", "boot completed"); | |
| + if(intent == null) return; | |
| + if(!intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) return; | |
| + | |
| + // start background service the first time after 30 + jitter (0..10) seconds | |
| + // randomness is to make it less likely that this collides with some other app | |
| + int jitter = new Random().nextInt(10000); | |
| + | |
| + PollService.schedulePoll(context, 30000 + jitter); | |
| + } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment