mirror of
https://github.com/tijder/SmsMatrix.git
synced 2024-10-27 18:24:01 +00:00
oreo need a foreground notification (it can be hidden by user)
This commit is contained in:
parent
faa8984c43
commit
fd08bb747a
@ -1,14 +1,22 @@
|
||||
package eu.droogers.smsmatrix;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Color;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
/**
|
||||
* Created by gerben on 7-10-17.
|
||||
*/
|
||||
@ -24,6 +32,7 @@ public class MatrixService extends Service {
|
||||
private String syncDelay;
|
||||
private String syncTimeout;
|
||||
private MMSMonitor mms;
|
||||
private String mChannelId = "";
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
@ -32,6 +41,21 @@ public class MatrixService extends Service {
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
|
||||
if (mChannelId.isEmpty()) {
|
||||
mChannelId = createNotificationChannel("sync", "Sync Service");
|
||||
}
|
||||
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, mChannelId);
|
||||
Notification notification = notificationBuilder.setOngoing(true)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentText(getApplicationInfo().loadLabel(getPackageManager()))
|
||||
.setPriority(NotificationCompat.PRIORITY_MIN)
|
||||
.setCategory(Notification.CATEGORY_SERVICE)
|
||||
.build();
|
||||
startForeground(1, notification);
|
||||
}
|
||||
|
||||
SharedPreferences sp = getSharedPreferences("settings", Context.MODE_PRIVATE);
|
||||
botUsername = sp.getString("botUsername", "");
|
||||
botPassword = sp.getString("botPassword", "");
|
||||
@ -76,6 +100,17 @@ public class MatrixService extends Service {
|
||||
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
private String createNotificationChannel(String channelId, String channelName){
|
||||
NotificationChannel chan = new NotificationChannel(channelId,
|
||||
channelName, NotificationManager.IMPORTANCE_NONE);
|
||||
chan.setLightColor(Color.BLUE);
|
||||
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
|
||||
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
service.createNotificationChannel(chan);
|
||||
return channelId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
mx.destroy();
|
||||
|
@ -6,7 +6,9 @@ import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.telephony.SmsMessage;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -26,7 +28,7 @@ public class ReceiverListener extends BroadcastReceiver {
|
||||
}
|
||||
else if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
|
||||
Intent intentServ = new Intent(context, MatrixService.class);
|
||||
context.startService(intent);
|
||||
ContextCompat.startForegroundService(context, intentServ);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user