Java Code Examples for android.app.PendingIntent#getForegroundService()

The following examples show how to use android.app.PendingIntent#getForegroundService() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static PendingIntent getForegroundService(@NonNull Context context, int requestCode, @NonNull Intent serviceIntent, int flag) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return PendingIntent.getForegroundService(context, requestCode, serviceIntent, flag);
    } else {
        return PendingIntent.getService(context, requestCode, serviceIntent, flag);
    }
}
 
Example 2
Source Project: DevUtils   File: AlarmUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 开启 ForegroundService 闹钟
 * @param context         {@link Context}
 * @param triggerAtMillis 执行时间
 * @param intent          {@link Intent}
 * @return {@code true} success, {@code false} fail
 */
@RequiresApi(Build.VERSION_CODES.O)
public static boolean startAlarmForegroundService(final Context context, final long triggerAtMillis, final Intent intent) {
    try {
        PendingIntent pendingIntent = PendingIntent.getForegroundService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return startAlarmIntent(triggerAtMillis, pendingIntent);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "startAlarmForegroundService");
    }
    return false;
}
 
Example 3
Source Project: DevUtils   File: AlarmUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 关闭 ForegroundService 闹钟
 * @param context {@link Context}
 * @param intent  {@link Intent}
 * @return {@code true} success, {@code false} fail
 */
@RequiresApi(Build.VERSION_CODES.O)
public static boolean stopAlarmForegroundService(final Context context, final Intent intent) {
    try {
        PendingIntent pendingIntent = PendingIntent.getForegroundService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return stopAlarmIntent(pendingIntent);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "stopAlarmForegroundService");
    }
    return false;
}
 
Example 4
protected PendingIntent buildPendingIntent(Context context, final String action, final ComponentName serviceName) {
    Intent intent = new Intent(action);
    intent.setComponent(serviceName);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return PendingIntent.getForegroundService(context, 0, intent, 0);
    } else {
        return PendingIntent.getService(context, 0, intent, 0);
    }
}
 
Example 5
protected PendingIntent buildPendingIntent(Context context, final String action, final ComponentName serviceName) {
    Intent intent = new Intent(action);
    intent.setComponent(serviceName);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return PendingIntent.getForegroundService(context, 0, intent, 0);
    } else {
        return PendingIntent.getService(context, 0, intent, 0);
    }
}
 
Example 6
protected PendingIntent buildPendingIntent(Context context, final String action, final ComponentName serviceName) {
    Intent intent = new Intent(action);
    intent.setComponent(serviceName);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return PendingIntent.getForegroundService(context, 0, intent, 0);
    } else {
        return PendingIntent.getService(context, 0, intent, 0);
    }
}
 
Example 7
protected PendingIntent buildPendingIntent(Context context, final String action, final ComponentName serviceName) {
    Intent intent = new Intent(action);
    intent.setComponent(serviceName);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return PendingIntent.getForegroundService(context, 0, intent, 0);
    } else {
        return PendingIntent.getService(context, 0, intent, 0);
    }
}
 
Example 8
protected PendingIntent buildPendingIntent(Context context, final String action, final ComponentName serviceName) {
    Intent intent = new Intent(action);
    intent.setComponent(serviceName);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return PendingIntent.getForegroundService(context, 0, intent, 0);
    } else {
        return PendingIntent.getService(context, 0, intent, 0);
    }
}
 
Example 9
Source Project: Hentoid   File: PendingIntentCompat.java    License: Apache License 2.0 5 votes vote down vote up
public static PendingIntent getForegroundService(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return PendingIntent.getForegroundService(context, 0, intent, 0);
    } else {
        return PendingIntent.getService(context, 0, intent, 0);
    }
}
 
Example 10
@Override
public void onCreate() {
    Log.i(TAG, "Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this));
    startForeground(NOTIFY_WAITING, getWaitingNotification());

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    if (jni_context != 0) {
        Log.w(TAG, "Create with context=" + jni_context);
        jni_stop(jni_context);
        synchronized (jni_lock) {
            jni_done(jni_context);
            jni_context = 0;
        }
    }

    // Native init
    jni_context = jni_init(Build.VERSION.SDK_INT);
    Log.i(TAG, "Created context=" + jni_context);
    boolean pcap = prefs.getBoolean("pcap", false);
    setPcap(pcap, this);

    prefs.registerOnSharedPreferenceChangeListener(this);

    Util.setTheme(this);
    super.onCreate();

    HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command", Process.THREAD_PRIORITY_FOREGROUND);
    HandlerThread logThread = new HandlerThread(getString(R.string.app_name) + " log", Process.THREAD_PRIORITY_BACKGROUND);
    HandlerThread statsThread = new HandlerThread(getString(R.string.app_name) + " stats", Process.THREAD_PRIORITY_BACKGROUND);
    commandThread.start();
    logThread.start();
    statsThread.start();

    commandLooper = commandThread.getLooper();
    logLooper = logThread.getLooper();
    statsLooper = statsThread.getLooper();

    commandHandler = new CommandHandler(commandLooper);
    logHandler = new LogHandler(logLooper);
    statsHandler = new StatsHandler(statsLooper);

    // Listen for user switches
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        IntentFilter ifUser = new IntentFilter();
        ifUser.addAction(Intent.ACTION_USER_BACKGROUND);
        ifUser.addAction(Intent.ACTION_USER_FOREGROUND);
        registerReceiver(userReceiver, ifUser);
        registeredUser = true;
    }

    // Listen for idle mode state changes
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        IntentFilter ifIdle = new IntentFilter();
        ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
        registerReceiver(idleStateReceiver, ifIdle);
        registeredIdleState = true;
    }

    // Listen for added/removed applications
    IntentFilter ifPackage = new IntentFilter();
    ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
    ifPackage.addAction(Intent.ACTION_PACKAGE_REMOVED);
    ifPackage.addDataScheme("package");
    registerReceiver(packageChangedReceiver, ifPackage);
    registeredPackageChanged = true;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        try {
            listenNetworkChanges();
        } catch (Throwable ex) {
            Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
            listenConnectivityChanges();
        }
    else
        listenConnectivityChanges();

    // Monitor networks
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.registerNetworkCallback(
            new NetworkRequest.Builder()
                    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build(),
            networkMonitorCallback);

    // Setup house holding
    Intent alarmIntent = new Intent(this, ServiceSinkhole.class);
    alarmIntent.setAction(ACTION_HOUSE_HOLDING);
    PendingIntent pi;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        pi = PendingIntent.getForegroundService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    else
        pi = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + 60 * 1000, AlarmManager.INTERVAL_HALF_DAY, pi);
}
 
Example 11
static PendingIntent getForegroundService(Context context, int requestCode, @NonNull Intent intent, int flags) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
        return PendingIntent.getService(context, requestCode, intent, flags);
    else
        return PendingIntent.getForegroundService(context, requestCode, intent, flags);
}
 
Example 12
@Override
public void onCreate() {
    Log.i(TAG, "Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this));
    startForeground(NOTIFY_WAITING, getWaitingNotification());

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    if (jni_context != 0) {
        Log.w(TAG, "Create with context=" + jni_context);
        jni_stop(jni_context);
        synchronized (jni_lock) {
            jni_done(jni_context);
            jni_context = 0;
        }
    }

    // Native init
    jni_context = jni_init(Build.VERSION.SDK_INT);
    Log.i(TAG, "Created context=" + jni_context);
    boolean pcap = prefs.getBoolean("pcap", false);
    setPcap(pcap, this);

    prefs.registerOnSharedPreferenceChangeListener(this);

    Util.setTheme(this);
    super.onCreate();

    HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command", Process.THREAD_PRIORITY_FOREGROUND);
    HandlerThread logThread = new HandlerThread(getString(R.string.app_name) + " log", Process.THREAD_PRIORITY_BACKGROUND);
    HandlerThread statsThread = new HandlerThread(getString(R.string.app_name) + " stats", Process.THREAD_PRIORITY_BACKGROUND);
    commandThread.start();
    logThread.start();
    statsThread.start();

    commandLooper = commandThread.getLooper();
    logLooper = logThread.getLooper();
    statsLooper = statsThread.getLooper();

    commandHandler = new CommandHandler(commandLooper);
    logHandler = new LogHandler(logLooper);
    statsHandler = new StatsHandler(statsLooper);

    // Listen for user switches
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        IntentFilter ifUser = new IntentFilter();
        ifUser.addAction(Intent.ACTION_USER_BACKGROUND);
        ifUser.addAction(Intent.ACTION_USER_FOREGROUND);
        registerReceiver(userReceiver, ifUser);
        registeredUser = true;
    }

    // Listen for idle mode state changes
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        IntentFilter ifIdle = new IntentFilter();
        ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
        registerReceiver(idleStateReceiver, ifIdle);
        registeredIdleState = true;
    }

    // Listen for added/removed applications
    IntentFilter ifPackage = new IntentFilter();
    ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
    ifPackage.addAction(Intent.ACTION_PACKAGE_REMOVED);
    ifPackage.addDataScheme("package");
    registerReceiver(packageChangedReceiver, ifPackage);
    registeredPackageChanged = true;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        try {
            listenNetworkChanges();
        } catch (Throwable ex) {
            Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
            listenConnectivityChanges();
        }
    else
        listenConnectivityChanges();

    // Monitor networks
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.registerNetworkCallback(
            new NetworkRequest.Builder()
                    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build(),
            networkMonitorCallback);

    // Setup house holding
    Intent alarmIntent = new Intent(this, ServiceSinkhole.class);
    alarmIntent.setAction(ACTION_HOUSE_HOLDING);
    PendingIntent pi;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        pi = PendingIntent.getForegroundService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    else
        pi = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + 60 * 1000, AlarmManager.INTERVAL_HALF_DAY, pi);
}