Friendiqa/source-android/androidnative.pri/java/src/androidnative/AndroidNativeService.java

71 lines
2.8 KiB
Java
Raw Normal View History

2019-01-09 22:03:16 +01:00
package androidnative;
2020-01-27 21:53:51 +01:00
import android.os.Build;
2019-01-09 22:03:16 +01:00
import android.content.Context;
import android.content.Intent;
2020-01-27 21:53:51 +01:00
import android.app.PendingIntent;
import android.content.Context;
import android.app.NotificationChannel;
import android.app.NotificationManager;
2019-01-09 22:03:16 +01:00
import android.util.Log;
2020-01-27 21:53:51 +01:00
import android.app.Notification.Builder;
import android.app.Notification;
import android.app.job.JobScheduler;
2019-01-09 22:03:16 +01:00
import org.qtproject.qt5.android.bindings.QtService;
2020-01-27 21:53:51 +01:00
import org.qtproject.qt5.android.QtNative;
import org.qtproject.friendiqa.R;
import androidnative.friendiqa.FriendiqaActivity;
2019-01-09 22:03:16 +01:00
public class AndroidNativeService extends QtService
{
private static String TAG = "AndroidNative";
public void startQtService(Context ctx) {
Log.d(TAG, "QtActivity active "+String.valueOf(QtNative.activity()!=null));
2020-01-27 21:53:51 +01:00
if (QtNative.activity()==null){
Log.d(TAG,"Friendiqasync Stop existing QtService");
2020-01-27 21:53:51 +01:00
ctx.stopService(new Intent(ctx, AndroidNativeService.class));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.d(TAG,"Friendiqasync startForegroundService");
2020-01-27 21:53:51 +01:00
ctx.startForegroundService(new Intent(ctx, AndroidNativeService.class));
} else {
ctx.startService(new Intent(ctx, AndroidNativeService.class));
}
2019-01-09 22:03:16 +01:00
}
public static void stopQtService(Context ctx) {
Log.d(TAG,"Friendiqasync QtServiceStop");
2019-01-09 22:03:16 +01:00
ctx.stopService(new Intent(ctx, AndroidNativeService.class));
}
2020-01-27 21:53:51 +01:00
@Override
public void onCreate()
{
Context context;
context = this.getApplicationContext();
Log.d(TAG,"Friendiqa onCreate");
2020-01-27 21:53:51 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(context,FriendiqaActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
2020-01-27 21:53:51 +01:00
String CHANNEL_ID = "channel_02";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"Sync Channel",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setSound(null,null);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
2020-05-24 21:14:23 +02:00
Log.d(TAG,"Friendiqa onCreate Notification");
2020-01-27 21:53:51 +01:00
Notification notification = new Notification.Builder(context,CHANNEL_ID)
.setSmallIcon(R.drawable.friendiqanotification)
.setContentTitle("Friendiqa")
.setContentText("Background Sync")
.setContentIntent(pendingIntent).build();
startForeground(1, notification);}
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.cancel(1);
super.onCreate();
}
2019-01-09 22:03:16 +01:00
}