下面列出了android.app.Service#stopSelf ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void stopForeground(WeakReference<AdamantLocalMessagingService> serviceWeakReference) {
Service service = serviceWeakReference.get();
if (service != null) {
service.stopForeground(true);
service.stopSelf();
}
}
private static void stopForeground(WeakReference<Service> serviceWeakReference) {
Service service = serviceWeakReference.get();
if (service != null) {
service.stopForeground(true);
service.stopSelf();
}
}
void quitApp(boolean force, boolean notifyHost) {
if (force) {
PluginDebugLog.runtimeLog(TAG, "quitapp with " + mPluginPackageName);
mActivityStackSupervisor.clearActivityStack();
PActivityStackSupervisor.clearLoadingIntent(mPluginPackageName);
PActivityStackSupervisor.removeLoadingIntent(mPluginPackageName);
for (Map.Entry<String, PluginServiceWrapper> entry : PServiceSupervisor.getAliveServices().entrySet()) {
PluginServiceWrapper serviceWrapper = entry.getValue();
if (serviceWrapper != null) {
if (!TextUtils.isEmpty(mPluginPackageName) &&
TextUtils.equals(mPluginPackageName, serviceWrapper.getPkgName())) {
String identity = PluginServiceWrapper.
getIdentify(mPluginPackageName, serviceWrapper.getServiceClassName());
if (!TextUtils.isEmpty(identity)) {
PluginDebugLog.runtimeLog(TAG, mPluginPackageName + " quitapp with service: " + identity);
ServiceConnection connection = PServiceSupervisor.getConnection(identity);
if (connection != null && mPluginAppContext != null) {
try {
PluginDebugLog.runtimeLog(TAG, "quitapp unbindService" + connection);
mPluginAppContext.unbindService(connection);
} catch (Exception e) {
// ignore
}
}
}
Service service = entry.getValue().getCurrentService();
if (service != null) {
service.stopSelf();
}
}
}
}
}
if (notifyHost) {
PluginManager.doExitStuff(mPluginPackageName, force);
}
}
/**
* 在插件服务中停止服务。近似于Service.stopSelf
* 注意:此方法应在插件服务中被调用
*
* @param s 要停止的插件服务
* @see android.app.Service#stopSelf()
*/
public static void stopSelf(Service s) {
if (!RePluginFramework.mHostInitialized) {
s.stopSelf();
return;
}
try {
ProxyRePluginServiceClientVar.stopSelf.call(null, s);
} catch (Exception e) {
if (LogDebug.LOG) {
e.printStackTrace();
}
}
}
public static void StopAll() {
for (Service service : mServiceList) {
if (service != null) {
service.stopSelf();
}
}
}
/**
* 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();
}
}
/**
* 当内存不足时,需要清除已打开的Activity及Service
*
* @see android.app.Application#onLowMemory()
*/
public void clearActivitiesAndServices() {
for (Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
for (Service service : services) {
service.stopSelf();
}
}
private void onRequestSuccess(List<GraphObject> list)
{
Log.d("FriendRequestService", "Num friend request : " + list.size());
Log.d("FriendRequestService", "Service : " + service.get());
if (service.get() == null)
return;
Service s = service.get();
if (list.size() > 0)
{
FriendRequest fq = (FriendRequest) list.get(0);
KlyphPreferences.setFriendRequestServiceOffset(fq.getTime());
final Builder builder = KlyphNotification.getBuilder(s, true);
builder.setContentTitle(fq.getUid_from_name()).setContentText(
s.getString(R.string.notification_friendrequest_message, fq.getUid_from_name()));
if (KlyphPreferences.mustGroupNotifications() && list.size() > 1)
{
sendNotification(list);
}
else
{
boolean isFirst = true;
for (GraphObject graphObject : list)
{
FriendRequest fr = (FriendRequest) graphObject;
TaskStackBuilder stackBuilder = TaskStackBuilder.create(service.get());
Intent intent = Klyph.getIntentForGraphObject(service.get(), fr);
// stackBuilder.addParentStack(UserActivity.class);
Intent mainIntent = new Intent(service.get(), MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
stackBuilder.addNextIntent(mainIntent);
stackBuilder.addNextIntent(intent);
int intentCode = (int) Math.round(Math.random() * 1000000);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(intentCode, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
builder.setContentTitle(fr.getUid_from_name());
builder.setContentText(s.getString(R.string.notification_friendrequest_message, fr.getUid_from_name()));
builder.setTicker(s.getString(R.string.notification_friendrequest_message, fr.getUid_from_name()));
if (isFirst == false)
{
KlyphNotification.setNoSound(builder);
KlyphNotification.setNoVibration(builder);
}
KlyphNotification.sendNotification(s, builder);
isFirst = false;
}
}
service.get().stopSelf();
}
else
{
s.stopSelf();
}
}
private void onRequestSuccess(List<GraphObject> list)
{
Log.d("BirthdayService", "onRequestSuccess " + list.size() + " " + service.get());
if (service.get() == null)
return;
Service s = service.get();
if (list.size() > 0)
{
final NotificationCompat.Builder builder = new NotificationCompat.Builder(s)
.setSmallIcon(R.drawable.ic_notification)
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.setDefaults(
android.app.Notification.DEFAULT_SOUND | android.app.Notification.DEFAULT_VIBRATE
| android.app.Notification.FLAG_ONLY_ALERT_ONCE);
final NotificationManager mNotificationManager = (NotificationManager) s.getSystemService(Context.NOTIFICATION_SERVICE);
if (KlyphPreferences.mustGroupNotifications() && list.size() > 1)
{
sendNotification(list);
}
else
{
boolean isFirst = true;
for (GraphObject graphObject : list)
{
Friend friend = (Friend) graphObject;
TaskStackBuilder stackBuilder = TaskStackBuilder.create(service.get());
Intent intent = Klyph.getIntentForGraphObject(service.get(), friend);
// stackBuilder.addParentStack(UserActivity.class);
Intent mainIntent = new Intent(service.get(), MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
stackBuilder.addNextIntent(mainIntent);
stackBuilder.addNextIntent(intent);
int intentCode = (int) Math.round(Math.random() * 1000000);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(intentCode, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
builder.setContentTitle(friend.getName());
builder.setContentText(s.getString(R.string.notification_birthday_today, friend.getName()));
builder.setTicker(s.getString(R.string.notification_birthday_today, friend.getName()));
if (isFirst == false)
{
builder.setDefaults(android.app.Notification.DEFAULT_VIBRATE | android.app.Notification.FLAG_ONLY_ALERT_ONCE);
builder.setSound(null);
}
final String tag = AttrUtil.getString(service.get(), R.string.app_name) + friend.getUid();
final int id = (int) System.currentTimeMillis();
mNotificationManager.notify(tag, id, builder.build());
isFirst = false;
}
}
}
s.stopSelf();
}