My app is about backing up SMS/MMS
messages.
So far my idea was to back up SMS/MMS
to an XML
file instantly when they arrive or are sent,
then periodically upload created XML file to a cloud storage, even if app is not running.
This approach is the only one I found that is reliable enough for me to not lose any conversations.
Functions to do all that are mostly complete but I have noticed that google does not allow service
to be running continuously
on a device.
So, my question is, is there a way to make service
run in background
continuously without getting killed after a while. I am not interested in foreground services
since i do not wish my users to have a notification
displayed at all times.
This is my first app basically and I am not even sure if my approach to back-up software is correct.
I would love to have some input how should I tackle this problem. All I care at this moment is that my app will back-up every conversation instantly on send
or receive
.
you can use android.permission.BIND_NOTIFICATION_LISTENER_SERVICE
permission in your service
but you have to ask user for permission and also android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
permission for "doze-mode" then use a Thread
in the service for your BroadCastReceiver
. this way guaranteed to make your service always running but it will cost battery life . i have tried and tested this for my app from api-level 19 to 28 and never stopped even in some devices like nokia with android O
background running service disabled but with this trick you can keep it running. apps like Gameboosters or Cleaners also use this trick to keep there service alwyase running.
UPDATE:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<service
android:name=".Service.NLService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
>
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Service
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NLService extends NotificationListenerService {
private static final String TAG = NLService.class.getSimpleName();
@Override
public void onCreate() {
super.onCreate();
new Thread(new Runnable() {
@Override
public void run() {
while (true)
try {
doSomeThing(NLService.this);
Thread.sleep(6000);
} catch (Exception ignore) {
}
}
}).start();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
doSomeThing(this);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
doSomeThing(this);
}
Request Permission
private static final String ACTION_NOTIFICATION_LISTENER_SETTINGS = "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS";
public void askForNotificationServicePermission() {
context.startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS));
}
public void askForDozeModePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = context.getPackageName();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
context.startActivity(intent);
}
}
doSomeThing(this);
is some method that you want to run for example your broudcastreceiver.