本文整理汇总了Java中android.app.Service.startForeground方法的典型用法代码示例。如果您正苦于以下问题:Java Service.startForeground方法的具体用法?Java Service.startForeground怎么用?Java Service.startForeground使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.app.Service
的用法示例。
在下文中一共展示了Service.startForeground方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createNotification
import android.app.Service; //导入方法依赖的package包/类
public static void createNotification(Service context) {
// Create Pending Intents.
PendingIntent piLaunchMainActivity = getLaunchActivityPI(context);
PendingIntent piStopService = getStopServicePI(context);
// Action to stop the service.
NotificationCompat.Action stopAction =
new NotificationCompat.Action.Builder(
STOP_ACTION_ICON,
getNotificationStopActionText(context),
piStopService)
.build();
// Create a notification.
Notification mNotification =
new NotificationCompat.Builder(context)
.setContentTitle(getNotificationTitle(context))
.setContentText(getNotificationContent(context))
.setSmallIcon(SMALL_ICON)
.setContentIntent(piLaunchMainActivity)
.addAction(stopAction)
.setStyle(new NotificationCompat.BigTextStyle())
.build();
context.startForeground(ONGOING_NOTIFICATION_ID, mNotification);
}
示例2: createStopNotification
import android.app.Service; //导入方法依赖的package包/类
public static void createStopNotification(MediaSessionCompat mediaSession, Service context, Class<?> serviceClass, int NOTIFICATION_ID) {
PendingIntent stopIntent = PendingIntent
.getService(context, 0, getIntent(MediaRecorderService.REQUEST_TYPE_STOP, context, serviceClass),
PendingIntent.FLAG_CANCEL_CURRENT);
MediaControllerCompat controller = mediaSession.getController();
MediaMetadataCompat mediaMetadata = controller.getMetadata();
MediaDescriptionCompat description = mediaMetadata.getDescription();
// Start foreground service to avoid unexpected kill
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setSubText(description.getDescription())
.setLargeIcon(description.getIconBitmap())
.setDeleteIntent(stopIntent)
// Add a pause button
.addAction(new android.support.v7.app.NotificationCompat.Action(
R.drawable.ic_stop_black_24dp, context.getString(R.string.stop),
MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_STOP)))
.setStyle(new android.support.v7.app.NotificationCompat.MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(0)
.setShowCancelButton(true)
.setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_STOP)))
.setSmallIcon(R.drawable.ic_album_black_24dp)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
context.startForeground(NOTIFICATION_ID, notification);
}
示例3: show
import android.app.Service; //导入方法依赖的package包/类
public static void show(final Service service, MediaSessionCompat session, Station station, int stationID, String stationMetadata) {
// save service and session
mService = service;
mSession = session;
mStationMetadata = stationMetadata;
// build notification
station.setPlaybackState(true);
mNotification = getNotificationBuilder(station, stationID, mStationMetadata).build(); // TODO: change -> Station object contains metadata, too
// display notification
service.startForeground(PLAYER_SERVICE_NOTIFICATION_ID, mNotification);
}
示例4: show
import android.app.Service; //导入方法依赖的package包/类
public static void show(final Service service, MediaSessionCompat session, Station station, int stationID, String stationMetadata) {
// save service and session
mService = service;
mSession = session;
mStationMetadata = stationMetadata;
// build notification
////comment unneeded setPlaybackState
//station.setPlaybackState(true);
mNotification = getNotificationBuilder(station, stationID, mStationMetadata).build(); // TODO: change -> Station object contains metadata, too
// display notification
service.startForeground(TransistorKeys.PLAYER_SERVICE_NOTIFICATION_ID, mNotification);
}
示例5: showRunningNotification
import android.app.Service; //导入方法依赖的package包/类
@Override
public void showRunningNotification(Service context) {
context.startForeground(ONLINE_NOTIFICATION, newRunningNotification(context));
}