Skip to content

Commit

Permalink
Merge pull request #219 from mvglasow/issue218
Browse files Browse the repository at this point in the history
Make main service a foreground service to avoid throttling in Android 8 (Oreo) 
Thx @mvglasow
  • Loading branch information
wish7code committed Aug 20, 2018
2 parents 7fe1355 + 93f6577 commit 6bc7147
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 42 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -4,6 +4,12 @@ build
*.iml
/gradle
local.properties
/android/app/src/main/.classpath
/android/app/src/main/.project
/android/app/src/main/.settings
/android/app/src/main/bin
/android/app/src/main/gen
/android/app/src/main/project.properties
/android/app/src/main/res/raw/build.txt
/artwork
/android/captures
3 changes: 2 additions & 1 deletion android/app/build.gradle
Expand Up @@ -26,6 +26,7 @@ android {

lintOptions {
disable 'MissingTranslation'
abortOnError false
}

testOptions {
Expand All @@ -41,7 +42,7 @@ buildscript {
}
dependencies {
// replace with the current version of the Android plugin
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.3.0'
// the latest version of the android-apt plugin
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
Expand Down
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Expand Up @@ -14,6 +14,7 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-sdk android:minSdkVersion="13" android:targetSdkVersion="23"/>

<application
android:name="org.openbmap.RadiobeaconApplication"
Expand Down
Expand Up @@ -209,7 +209,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
@Override
public void onDestroy() {
unregisterReceiver();
hideNotification();
stopForeground(true);
EventBus.getDefault().unregister(this);

unbindAll();
Expand Down Expand Up @@ -331,7 +331,38 @@ private void startTracking(final int session) {
mSession = setupNewSession();
}
bindAll();
showNotification();

PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, TabHostActivity.class), 0);

// Set the icon, scrolling text and timestamp
//final Notification notification = new Notification(R.drawable.icon_greyed_25x25, getString(R.string.notification_caption),
// System.currentTimeMillis());
//notification.setLatestEventInfo(this, getString(R.string.app_name), getString(R.string.notification_caption), contentIntent);

// Set the info for the views that show in the notification panel.

Notification notification;
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
builder.setAutoCancel(false);
builder.setContentTitle(getString(R.string.app_name));
builder.setContentText(getString(R.string.notification_caption));
builder.setSmallIcon(R.drawable.icon_greyed_25x25);
builder.setContentIntent(intent);
builder.setOngoing(true);
notification = builder.build();
} else {
NotificationCompat.Builder compat = new NotificationCompat.Builder(getApplicationContext());
compat.setAutoCancel(false);
compat.setContentTitle(getString(R.string.app_name));
compat.setContentText(getString(R.string.notification_caption));
compat.setSmallIcon(R.drawable.icon_greyed_25x25);
compat.setContentIntent(intent);
compat.setOngoing(true);
notification = compat.build();
}

startForeground(NOTIFICATION_ID, notification);
}

/**
Expand All @@ -354,7 +385,7 @@ private void stopTracking(int reason) {
}
}

hideNotification();
stopForeground(true);
}

/**
Expand Down Expand Up @@ -448,42 +479,6 @@ private void updateDatabase() {
* Shows Android notification while this service is running.
*/
private void showNotification() {
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, TabHostActivity.class), 0);

// Set the icon, scrolling text and timestamp
//final Notification notification = new Notification(R.drawable.icon_greyed_25x25, getString(R.string.notification_caption),
// System.currentTimeMillis());
//notification.setLatestEventInfo(this, getString(R.string.app_name), getString(R.string.notification_caption), contentIntent);

// Set the info for the views that show in the notification panel.


if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
builder.setAutoCancel(false);
builder.setContentTitle(getString(R.string.app_name));
builder.setContentText(getString(R.string.notification_caption));
builder.setSmallIcon(R.drawable.icon_greyed_25x25);
builder.setContentIntent(intent);
builder.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, builder.build());
} else if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN) {
NotificationCompat.Builder compat = new NotificationCompat.Builder(getApplicationContext());
compat.setAutoCancel(false);
compat.setContentTitle(getString(R.string.app_name));
compat.setContentText(getString(R.string.notification_caption));
compat.setSmallIcon(R.drawable.icon_greyed_25x25);
compat.setContentIntent(intent);
compat.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, compat.build());
}
}

/**
* Hides Android notification
*/
private void hideNotification() {
mNotificationManager.cancel(NOTIFICATION_ID);
}

}
Expand Up @@ -355,7 +355,7 @@ public void onSignalStrengthsChanged(final SignalStrength signalStrength) {
// TODO we need a timestamp for signal strength
try {
mCdmaStrengthDbm = signalStrength.getCdmaDbm();
mCdmaEcIo = signalStrength.getmCdmaEcIo();
mCdmaEcIo = signalStrength.getCdmaEcio();
} catch (final Exception e) {
Log.e(TAG, e.toString(), e);
}
Expand All @@ -368,7 +368,7 @@ public void onSignalStrengthsChanged(final SignalStrength signalStrength) {
}

try {
mGsmBitErrorRate = signalStrength.getmGsmBitErrorRate();
mGsmBitErrorRate = signalStrength.getGsmBitErrorRate();
mGsmStrengthAsu = signalStrength.getGsmSignalStrength();
mGsmStrengthDbm = -113 + 2 * mGsmStrengthAsu; // conversion ASU in dBm
} catch (final Exception e) {
Expand Down
Empty file modified android/gradlew 100644 → 100755
Empty file.

0 comments on commit 6bc7147

Please sign in to comment.