android.app.PendingIntent#getForegroundService ( )源码实例Demo

下面列出了android.app.PendingIntent#getForegroundService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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);
    }
}
 
源代码2 项目: DevUtils   文件: AlarmUtils.java
/**
 * 开启 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;
}
 
源代码3 项目: DevUtils   文件: AlarmUtils.java
/**
 * 关闭 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;
}
 
源代码4 项目: Music-Player   文件: BaseAppWidget.java
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);
    }
}
 
源代码5 项目: MusicPlayer   文件: BaseAppWidget.java
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);
    }
}
 
源代码6 项目: RetroMusicPlayer   文件: BaseAppWidget.java
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);
    }
}
 
源代码7 项目: VinylMusicPlayer   文件: BaseAppWidget.java
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);
    }
}
 
源代码8 项目: Phonograph   文件: BaseAppWidget.java
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);
    }
}
 
源代码9 项目: Hentoid   文件: PendingIntentCompat.java
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);
    }
}
 
源代码10 项目: tracker-control-android   文件: ServiceSinkhole.java
@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);
}
 
源代码11 项目: FairEmail   文件: PendingIntentCompat.java
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);
}
 
源代码12 项目: NetGuard   文件: ServiceSinkhole.java
@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);
}