android.app.NotificationChannel#setDescription ( )源码实例Demo

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

源代码1 项目: user-interface-samples   文件: NotificationUtil.java
public static String createNotificationChannel(
        Context context,
        MockDatabase.MockNotificationData mockNotificationData) {

    // NotificationChannels are required for Notifications on O (API 26) and above.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        // The id of the channel.
        String channelId = mockNotificationData.getChannelId();

        // The user-visible name of the channel.
        CharSequence channelName = mockNotificationData.getChannelName();
        // The user-visible description of the channel.
        String channelDescription = mockNotificationData.getChannelDescription();
        int channelImportance = mockNotificationData.getChannelImportance();
        boolean channelEnableVibrate = mockNotificationData.isChannelEnableVibrate();
        int channelLockscreenVisibility =
                mockNotificationData.getChannelLockscreenVisibility();

        // Initializes NotificationChannel.
        NotificationChannel notificationChannel =
                new NotificationChannel(channelId, channelName, channelImportance);
        notificationChannel.setDescription(channelDescription);
        notificationChannel.enableVibration(channelEnableVibrate);
        notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);

        // Adds NotificationChannel to system. Attempting to create an existing notification
        // channel with its original values performs no operation, so it's safe to perform the
        // below sequence.
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);

        return channelId;
    } else {
        // Returns null for pre-O (26) devices.
        return null;
    }
}
 
源代码2 项目: DeviceConnect-Android   文件: NotificationUtil.java
/**
 * DConnectServiceがOFF時にstartForegroundService()が行われた時にキャンセルする.
 */
public static void fakeStartForeground(final Service service,
                                       final String channelId,
                                       final String title,
                                       final String description,
                                       final int notificationId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Notification.Builder builder = new Notification.Builder(service.getApplicationContext(),
                                channelId)
                .setContentTitle("").setContentText("");
        NotificationChannel channel = new NotificationChannel(
                channelId,
                title,
                NotificationManager.IMPORTANCE_LOW);
        channel.setDescription(description);
        int iconType = Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ?
                R.drawable.icon : R.drawable.on_icon;
        builder.setSmallIcon(iconType);
        NotificationManager mNotification = (NotificationManager) service.getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotification.createNotificationChannel(channel);
        builder.setChannelId(channelId);
        service.startForeground(notificationId, builder.build());
        service.stopForeground(true);
        service.stopSelf();
    }
}
 
源代码3 项目: klingar   文件: MediaNotificationManager.java
/**
 * Creates Notification Channel. This is required in Android O+ to display notifications.
 */
@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
  if (notificationManager.getNotificationChannel(CHANNEL_ID) == null) {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
        service.getString(R.string.notification_channel), NotificationManager.IMPORTANCE_LOW);
    channel.setDescription(service.getString(R.string.notification_channel_description));
    notificationManager.createNotificationChannel(channel);
  }
}
 
源代码4 项目: AndroidSamples   文件: NotificationActivity.java
/**
 * 创建渠道
 */
private void initNotification() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        /**
         * IMPORTANCE_UNSPECIFIED(值为-1)意味着用户没有表达重要性的价值。此值用于保留偏好设置,不应与实际通知关联。
         * IMPORTANCE_NONE(值为0)不重要的通知:不会在阴影中显示。
         * IMPORTANCE_MIN(值为1)最低通知重要性:只显示在阴影下,低于折叠。这不应该与Service.startForeground一起使用,因为前台服务应该是用户关心的事情,所以它没有语义意义来将其通知标记为最低重要性。如果您从Android版本O开始执行此操作,系统将显示有关您的应用在后台运行的更高优先级通知。
         * IMPORTANCE_LOW(值为2)低通知重要性:无处不在,但不侵入视觉。
         * IMPORTANCE_DEFAULT (值为3):默认通知重要性:随处显示,产生噪音,但不会在视觉上侵入。
         * IMPORTANCE_HIGH(值为4)更高的通知重要性:随处显示,造成噪音和窥视。可以使用全屏的Intent。
         */
        //只在Android O之上需要渠道
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,
                CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        // 配置通知渠道的属性
        notificationChannel.setDescription("渠道的描述");
        // 设置通知出现时的闪灯(如果 android 设备支持的话)
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        // 设置通知出现时的震动(如果 android 设备支持的话)
        notificationChannel.enableVibration(true);
        //如上设置使手机:静止1秒,震动2秒,静止1秒,震动3秒
        notificationChannel.setVibrationPattern(new long[]{1000, 2000, 1000, 3000});
        //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,
        //通知才能正常弹出
        mManager.createNotificationChannel(notificationChannel);
    }
}
 
源代码5 项目: ZadakNotification   文件: Sample.java
private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "My app notification channel";
        String description = "Description for this channel";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
 
源代码6 项目: rn-heartbeat   文件: HeartbeartService.java
private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "HEARTBEAT", importance);
        channel.setDescription("CHANEL DESCRIPTION");
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
 
源代码7 项目: rcloneExplorer   文件: MoveService.java
private void setNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
        channel.setDescription(getString(R.string.background_service_notification_channel_description));
        // Register the channel with the system
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
}
 
源代码8 项目: wear-os-samples   文件: NotificationUtil.java
/**
 * Creates Notification Channel used for Notifications on O (26) and higher.
 *
 * @param context
 * @param mockNotificationData
 * @return
 */
public static String createNotificationChannel(
        Context context,
        MockDatabase.MessagingStyleCommsAppData mockNotificationData) {

    // NotificationChannels are required for Notifications on O (API 26) and above.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        // The id of the channel.
        String channelId = mockNotificationData.getChannelId();

        // The user-visible name of the channel.
        CharSequence channelName = mockNotificationData.getChannelName();
        // The user-visible description of the channel.
        String channelDescription = mockNotificationData.getChannelDescription();
        int channelImportance = mockNotificationData.getChannelImportance();
        boolean channelEnableVibrate = mockNotificationData.isChannelEnableVibrate();
        int channelLockscreenVisibility =
                mockNotificationData.getChannelLockscreenVisibility();

        // Initializes NotificationChannel.
        NotificationChannel notificationChannel =
                new NotificationChannel(channelId, channelName, channelImportance);
        notificationChannel.setDescription(channelDescription);
        notificationChannel.enableVibration(channelEnableVibrate);
        notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);

        // Adds NotificationChannel to system. Attempting to create an existing notification
        // channel with its original values performs no operation, so it's safe to perform the
        // below sequence.
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);

        return channelId;
    } else {
        // Returns null for pre-O (26) devices.
        return null;
    }
}
 
源代码9 项目: grblcontroller   文件: NotificationHelper.java
public void createChannels(){

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannelOne = new NotificationChannel(CHANNEL_GENERAL_ID, CHANNEL_GENERAL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            mChannelOne.setDescription(CHANNEL_GENERAL_ABOUT);
            mChannelOne.enableLights(false);
            mChannelOne.setLightColor(getColor(R.color.colorPrimary));
            mChannelOne.setShowBadge(true);
            mChannelOne.enableVibration(false);
            mChannelOne.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getNotificationManager().createNotificationChannel(mChannelOne);

            NotificationChannel mChannelTwo = new NotificationChannel(CHANNEL_BUG_TRACKER_ID, CHANNEL_BUG_TRACKER_NAME, NotificationManager.IMPORTANCE_HIGH);
            mChannelTwo.setDescription(CHANNEL_BUG_TRACKER_ABOUT);
            mChannelTwo.enableLights(true);
            mChannelTwo.enableVibration(true);
            mChannelTwo.setLightColor(getColor(R.color.colorPrimary));
            mChannelTwo.setShowBadge(true);
            getNotificationManager().createNotificationChannel(mChannelTwo);

            NotificationChannel mChannelThree = new NotificationChannel(CHANNEL_SERVICE_ID, CHANNEL_SERVICE_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            mChannelThree.setDescription(CHANNEL_SERVICE_ABOUT);
            mChannelThree.enableLights(false);
            mChannelThree.enableVibration(false);
            mChannelThree.setLightColor(getColor(R.color.colorPrimary));
            mChannelThree.setShowBadge(true);
            mChannelThree.setSound(null, null);
            getNotificationManager().createNotificationChannel(mChannelThree);
        }
    }
 
源代码10 项目: ns-usbloader-mobile   文件: NsBroadcastReceiver.java
private void showNotification(Context context, UsbDevice usbDevice){
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NsConstants.NOTIFICATION_NS_CONNECTED_CHAN_ID);
    notification.setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(context.getString(R.string.ns_connected_info))
            //.setAutoCancel(true)
            .setOngoing(true)       // Prevent swipe-notification-to-remove
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class).putExtra(UsbManager.EXTRA_DEVICE, usbDevice), 0));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence notificationChanName = context.getString(R.string.notification_chan_name_usb);
        String notificationChanDesc = context.getString(R.string.notification_chan_desc_usb);

        NotificationChannel channel = new NotificationChannel(
                NsConstants.NOTIFICATION_NS_CONNECTED_CHAN_ID,
                notificationChanName,
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(notificationChanDesc);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
        notificationManager.notify(NsConstants.NOTIFICATION_NS_CONNECTED_ID, notification.build());
    }
    else {
        NotificationManagerCompat.from(context).notify(NsConstants.NOTIFICATION_NS_CONNECTED_ID, notification.build());
    }
}
 
源代码11 项目: deltachat-android   文件: KeepAliveService.java
@TargetApi(Build.VERSION_CODES.O)
static private void createFgNotificationChannel(Context context) {
    if(!ch_created) {
        ch_created = true;
        NotificationChannel channel = new NotificationChannel(NotificationCenter.CH_PERMANENT,
            "Receive messages in background.", NotificationManager.IMPORTANCE_MIN); // IMPORTANCE_DEFAULT will play a sound
        channel.setDescription("Ensure reliable message receiving.");
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
 
源代码12 项目: RoMote   文件: NotificationService.java
@Override
public void onCreate() {
    super.onCreate();

    setUpMediaSession();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Constants.UPDATE_DEVICE_BROADCAST);
    registerReceiver(mUpdateReceiver, intentFilter);

    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(getString(R.string.app_name), getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW);
        channel.setDescription(TAG);
        channel.enableLights(false);
        channel.enableVibration(false);
        mNM.createNotificationChannel(channel);
    }

    //startForeground(NotificationService.NOTIFICATION, notification);

    mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean enableNotification = mPreferences.getBoolean("notification_checkbox_preference", false);

    mPreferences.registerOnSharedPreferenceChangeListener(mPreferencesChangedListener);

    try {
        mDevice = PreferenceUtils.getConnectedDevice(this);

        if (enableNotification && mDevice != null) {
            notification = NotificationUtils.buildNotification(NotificationService.this, null, null, null, mediaSession.getSessionToken());

            mNM.notify(NOTIFICATION, notification);
            sendStatusCommand();
        }
    } catch (Exception ex) {
    }
}
 
源代码13 项目: fitnotifications   文件: HomeActivity.java
private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        try {
            manager.deleteNotificationChannel(Constants.NOTIFICATION_CHANNEL_ID_OLD);
            manager.deleteNotificationChannel(Constants.NOTIFICATION_CHANNEL_ID_FIX);

        } catch (NullPointerException e) {
            Log.e("FitNotificationErrors", "Error deleting notification channel. Error = " + e.getMessage());
        }

        String id = Constants.NOTIFICATION_CHANNEL_ID;
        CharSequence name = getString(R.string.notification_channel_name);
        String desc = getString(R.string.notification_channel_desc);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(id, name, importance);
        channel.setShowBadge(false);
        channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + "raw/silent.ogg"),
                new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION)
                                             .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
                                             .build());
        channel.setDescription(desc);
        channel.enableLights(false);
        channel.enableVibration(false);
        manager.createNotificationChannel(channel);
    }

}
 
源代码14 项目: hackerskeyboard   文件: LatinIME.java
private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.notification_channel_name);
        String description = getString(R.string.notification_channel_description);
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
 
源代码15 项目: Intra   文件: IntraVpnService.java
public void signalStopService(boolean userInitiated) {
  LogWrapper.log(
      Log.INFO,
      LOG_TAG,
      String.format("Received stop signal. User initiated: %b", userInitiated));

  if (!userInitiated) {
    final long[] vibrationPattern = {1000}; // Vibrate for one second.
    // Show revocation warning
    Notification.Builder builder;
    NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (VERSION.SDK_INT >= VERSION_CODES.O) {
      CharSequence name = getString(R.string.warning_channel_name);
      String description = getString(R.string.warning_channel_description);
      int importance = NotificationManager.IMPORTANCE_HIGH;
      NotificationChannel channel = new NotificationChannel(WARNING_CHANNEL_ID, name, importance);
      channel.setDescription(description);
      channel.enableVibration(true);
      channel.setVibrationPattern(vibrationPattern);

      notificationManager.createNotificationChannel(channel);
      builder = new Notification.Builder(this, WARNING_CHANNEL_ID);
    } else {
      builder = new Notification.Builder(this);
      builder.setVibrate(vibrationPattern);
      if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
        // Only available in API >= 16.  Deprecated in API 26.
        builder = builder.setPriority(Notification.PRIORITY_MAX);
      }
    }

    PendingIntent mainActivityIntent = PendingIntent.getActivity(
        this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setSmallIcon(R.drawable.ic_status_bar)
        .setContentTitle(getResources().getText(R.string.warning_title))
        .setContentText(getResources().getText(R.string.notification_content))
        .setFullScreenIntent(mainActivityIntent, true)  // Open the main UI if possible.
        .setAutoCancel(true);

    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
      builder.setCategory(Notification.CATEGORY_ERROR);
    }

    notificationManager.notify(0, builder.getNotification());
  }

  stopVpnAdapter();
  stopSelf();

  updateQuickSettingsTile();
}
 
源代码16 项目: Beginner-Level-Android-Studio-Apps   文件: App.java
private void CreateNotificationChannels() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            //Channel 1 Configuration
            NotificationChannel channel1 = new NotificationChannel(CHANNEL_1_ID,
                    "channel 1",NotificationManager.IMPORTANCE_HIGH);
            channel1.setDescription("Test Channel 1");

            //Channel 2 Configuration
            NotificationChannel channel2 = new NotificationChannel(CHANNEL_2_ID,
                    "channel 2",NotificationManager.IMPORTANCE_LOW);
            channel2.setDescription("Test Channel 2");

            //Channel 3 Configuration
            NotificationChannel channel3 = new NotificationChannel(CHANNEL_3_ID,
                    "channel 3",NotificationManager.IMPORTANCE_DEFAULT);
            channel3.setDescription("Test Channel 3");

            //Channel 4 Configuration
            NotificationChannel channel4 = new NotificationChannel(CHANNEL_4_ID,
                    "channel 4",NotificationManager.IMPORTANCE_HIGH);
            channel4.setDescription("Test Channel 4");

            //Channel 5 Configuration
            NotificationChannel channel5 = new NotificationChannel(CHANNEL_5_ID,
                    "channel 5",NotificationManager.IMPORTANCE_HIGH);
            channel5.setDescription("Test Channel 5");




            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel1);
            manager.createNotificationChannel(channel2);
            manager.createNotificationChannel(channel3);
            manager.createNotificationChannel(channel4);
            manager.createNotificationChannel(channel5);

        }

    }
 
源代码17 项目: MediaSDK   文件: NotificationUtil.java
/**
 * Creates a notification channel that notifications can be posted to. See {@link
 * NotificationChannel} and {@link
 * NotificationManager#createNotificationChannel(NotificationChannel)} for details.
 *
 * @param context A {@link Context}.
 * @param id The id of the channel. Must be unique per package. The value may be truncated if it's
 *     too long.
 * @param nameResourceId A string resource identifier for the user visible name of the channel.
 *     The recommended maximum length is 40 characters. The string may be truncated if it's too
 *     long. You can rename the channel when the system locale changes by listening for the {@link
 *     Intent#ACTION_LOCALE_CHANGED} broadcast.
 * @param descriptionResourceId A string resource identifier for the user visible description of
 *     the channel, or 0 if no description is provided. The recommended maximum length is 300
 *     characters. The value may be truncated if it is too long. You can change the description of
 *     the channel when the system locale changes by listening for the {@link
 *     Intent#ACTION_LOCALE_CHANGED} broadcast.
 * @param importance The importance of the channel. This controls how interruptive notifications
 *     posted to this channel are. One of {@link #IMPORTANCE_UNSPECIFIED}, {@link
 *     #IMPORTANCE_NONE}, {@link #IMPORTANCE_MIN}, {@link #IMPORTANCE_LOW}, {@link
 *     #IMPORTANCE_DEFAULT} and {@link #IMPORTANCE_HIGH}.
 */
public static void createNotificationChannel(
    Context context,
    String id,
    @StringRes int nameResourceId,
    @StringRes int descriptionResourceId,
    @Importance int importance) {
  if (Util.SDK_INT >= 26) {
    NotificationManager notificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel =
        new NotificationChannel(id, context.getString(nameResourceId), importance);
    if (descriptionResourceId != 0) {
      channel.setDescription(context.getString(descriptionResourceId));
    }
    notificationManager.createNotificationChannel(channel);
  }
}
 
源代码18 项目: YTPlayer   文件: IntentDownloadService.java
@Override
public void onCreate() {
    context = getApplicationContext();
    pendingJobs = new ArrayList<>();

    SharedPreferences preferences = getSharedPreferences("appSettings",MODE_PRIVATE);
    useFFMPEGmuxer = preferences.getBoolean("pref_muxer",true);

    PRDownloader.initialize(context);

    /** Create notification channel if not present */
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
        CharSequence name = context.getString(R.string.channel_name);
        String description = context.getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel notificationChannel = new NotificationChannel("channel_01", name, importance);
        notificationChannel.setDescription(description);
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(notificationChannel);

        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,"Download",importance);
        notificationManager.createNotificationChannel(channel);
    }


    /** Setting Power Manager and Wakelock */

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "app:Wakelock");
    wakeLock.acquire();

    Intent notificationIntent = new Intent(context, DownloadActivity.class);
    contentIntent = PendingIntent.getActivity(context,
            0, notificationIntent, 0);

    Intent newintent = new Intent(context, SongBroadCast.class);
    newintent.setAction("com.kpstv.youtube.STOP_SERVICE");
    cancelIntent =
            PendingIntent.getBroadcast(context, 5, newintent, 0);

    setUpdateNotificationTask();
    super.onCreate();
}
 
源代码19 项目: Intra   文件: IntraVpnService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  synchronized (vpnController) {
    Log.i(LOG_TAG, String.format("Starting DNS VPN service, url=%s", url));
    url = PersistentState.getServerUrl(this);

    // Registers this class as a listener for user preference changes.
    PreferenceManager.getDefaultSharedPreferences(this).
        registerOnSharedPreferenceChangeListener(this);

    if (networkManager != null) {
      spawnServerUpdate();
      return START_REDELIVER_INTENT;
    }

    // If we're online, |networkManager| immediately calls this.onNetworkConnected(), which in turn
    // calls startVpn() to actually start.  If we're offline, the startup actions will be delayed
    // until we come online.
    networkManager = new NetworkManager(IntraVpnService.this, IntraVpnService.this);

    // Mark this as a foreground service.  This is normally done to ensure that the service
    // survives under memory pressure.  Since this is a VPN service, it is presumably protected
    // anyway, but the foreground service mechanism allows us to set a persistent notification,
    // which helps users understand what's going on, and return to the app if they want.
    PendingIntent mainActivityIntent =
        PendingIntent.getActivity(
            this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

    Notification.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      CharSequence name = getString(R.string.channel_name);
      String description = getString(R.string.channel_description);
      // LOW is the lowest importance that is allowed with startForeground in Android O.
      int importance = NotificationManager.IMPORTANCE_LOW;
      NotificationChannel channel = new NotificationChannel(MAIN_CHANNEL_ID, name, importance);
      channel.setDescription(description);

      NotificationManager notificationManager = getSystemService(NotificationManager.class);
      notificationManager.createNotificationChannel(channel);
      builder = new Notification.Builder(this, MAIN_CHANNEL_ID);
    } else {
      builder = new Notification.Builder(this);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // Min-priority notifications don't show an icon in the notification bar, reducing clutter.
        // Only available in API >= 16.  Deprecated in API 26.
        builder = builder.setPriority(Notification.PRIORITY_MIN);
      }
    }

    builder.setSmallIcon(R.drawable.ic_status_bar)
        .setContentTitle(getResources().getText(R.string.notification_title))
        .setContentText(getResources().getText(R.string.notification_content))
        .setContentIntent(mainActivityIntent);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      // Secret notifications are not shown on the lock screen.  No need for this app to show there.
      // Only available in API >= 21
      builder = builder.setVisibility(Notification.VISIBILITY_SECRET);
    }

    startForeground(SERVICE_ID, builder.getNotification());

    updateQuickSettingsTile();

    return START_REDELIVER_INTENT;
  }
}
 
源代码20 项目: AndroidAPS   文件: ListenerService.java
private void notifyChangeRequest(String title, String message, String actionstring) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "AAPS Open Loop";
            String description = "Open Loop request notiffication";//getString(R.string.channel_description);
            NotificationChannel channel = new NotificationChannel(AAPS_NOTIFY_CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription(description);
            channel.enableVibration(true);

            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this, AAPS_NOTIFY_CHANNEL_ID);

    builder = builder.setSmallIcon(R.drawable.notif_icon)
            .setContentTitle(title)
            .setContentText(message)
            .setPriority(Notification.PRIORITY_HIGH)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});

    // Creates an explicit intent for an Activity in your app
    Intent intent = new Intent(this, AcceptActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Bundle params = new Bundle();
    params.putString("title", title);
    params.putString("message", message);
    params.putString("actionstring", actionstring);
    intent.putExtras(params);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    builder = builder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(CHANGE_NOTIF_ID, builder.build());
}