下面列出了android.app.Service#getSystemService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ShadowsocksNotification(Service service, String profileName, boolean visible) {
this.service = service;
this.profileName = profileName;
this.visible = visible;
keyGuard = (KeyguardManager) service.getSystemService(Context.KEYGUARD_SERVICE);
nm = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
pm = (PowerManager) service.getSystemService(Context.POWER_SERVICE);
// init notification builder
initNotificationBuilder();
style = new NotificationCompat.BigTextStyle(builder);
// init with update action
initWithUpdateAction();
// register lock receiver
registerLockReceiver(service, visible);
}
public ShadowsocksNotification(Service service, String profileName, boolean visible) {
this.service = service;
this.profileName = profileName;
this.visible = visible;
keyGuard = (KeyguardManager) service.getSystemService(Context.KEYGUARD_SERVICE);
nm = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
pm = (PowerManager) service.getSystemService(Context.POWER_SERVICE);
// init notification builder
initNotificationBuilder();
style = new NotificationCompat.BigTextStyle(builder);
// init with update action
initWithUpdateAction();
// register lock receiver
registerLockReceiver(service, visible);
}
private void showFileNotFoundNotification(String uri)
{
if (service.get() == null)
return;
Service s = service.get();
final int notificationId = (int) System.currentTimeMillis();
NotificationCompat.Builder builder = new NotificationCompat.Builder(service.get()).setSmallIcon(R.drawable.ic_notification)
.setTicker(service.get().getString(R.string.upload_error)).setAutoCancel(true)
.setOnlyAlertOnce(true);
NotificationUtil.setDummyIntent(service.get(), builder);
builder.setContentTitle(service.get().getString(R.string.upload_error));
builder.setContentText(service.get().getString(R.string.file_not_found, uri));
final NotificationManager mNotificationManager = (NotificationManager) s
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(AttrUtil.getString(s, R.string.app_name), notificationId, builder.build());
}
private void showNotification()
{
if (service.get() == null)
return;
Service s = service.get();
builder.setTicker(s.getString(R.string.uploading_image_of, total - images.size(), total));
builder.setContentText(s.getString(R.string.uploading_image_of, total - images.size(), total));
builder.setProgress(100, 0, true);
final NotificationManager mNotificationManager = (NotificationManager) s
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(AttrUtil.getString(s, R.string.app_name), notificationId, builder.build());
}
private void showEndNotification()
{
if (service.get() == null)
return;
Service s = service.get();
builder.setTicker(service.get().getString(R.string.upload_complete));
builder.setContentTitle(service.get().getString(R.string.upload_complete));
builder.setContentText(s.getString(R.string.n_uploaded_images, total - errors));
builder.setProgress(0, 0, false);
final NotificationManager mNotificationManager = (NotificationManager) s
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(AttrUtil.getString(s, R.string.app_name), notificationId, builder.build());
}
private void showErrorNotification(String fbError)
{
if (service.get() == null || fbError == null)
return;
Service s = service.get();
final int notificationId = (int) System.currentTimeMillis();
NotificationCompat.Builder builder = new NotificationCompat.Builder(service.get()).setSmallIcon(R.drawable.ic_notification)
.setTicker(service.get().getString(R.string.upload_error)).setAutoCancel(true)
.setOnlyAlertOnce(true);
NotificationUtil.setDummyIntent(service.get(), builder);
builder.setContentTitle(service.get().getString(R.string.upload_error));
builder.setContentText(fbError);
final NotificationManager mNotificationManager = (NotificationManager) s
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(AttrUtil.getString(s, R.string.app_name), notificationId, builder.build());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// 等Injector初始化
if (InjectorService.g() == null) {
return;
}
// 如果有Activity
Activity context = (Activity) loadActivityOnTop();
if (context != null) {
InjectorService.g().pushMessage(SCREEN_ORIENTATION, context.getWindowManager().getDefaultDisplay().getRotation());
return;
}
// 从Service获取
Service service = (Service) loadRunningService();
// 没有就只能去0或90
if (service == null) {
InjectorService.g().pushMessage(SCREEN_ORIENTATION, newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? ROTATION_90 : ROTATION_0);
return;
}
// 找WindowManager
WindowManager wm = (WindowManager) service.getSystemService(WINDOW_SERVICE);
int rotation = wm.getDefaultDisplay().getRotation();
// 推送屏幕方向
if (InjectorService.g() != null) {
InjectorService.g().pushMessage(SCREEN_ORIENTATION, rotation);
}
}
static String isAlive(Service candidate) {
if (candidate == null)
return "Service reference null";
ActivityManager manager = (ActivityManager) candidate.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> services = manager.getRunningServices(Integer.MAX_VALUE);
if (services == null)
return "Could not retrieve services from service manager";
for (ActivityManager.RunningServiceInfo service : services) {
if (candidate.getClass().getName().equals(service.service.getClassName())) {
return null;
}
}
return "Service stopped";
}
public StatusNotification(Service service, Class targetActivityClass) {
this.service = service;
this.targetActivityClass = targetActivityClass;
notificationManager = (NotificationManager) service.getSystemService(service.NOTIFICATION_SERVICE);
pendingIntent = getDefaultPendingIntent();
}
private static ArrayList<String> extractTextFromNotification(Service service, RemoteViews view) {
LayoutInflater inflater = (LayoutInflater) service.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ArrayList<String> result = new ArrayList<String>();
if (view == null) {
Log.d(TAG, "View is empty");
return null;
}
try {
int layoutid = view.getLayoutId();
ViewGroup localView = (ViewGroup) inflater.inflate(layoutid, null);
view.reapply(service.getApplicationContext(), localView);
ArrayList<View> outViews = new ArrayList<View>();
extractViewType(outViews, TextView.class, localView);
for (View ttv: outViews) {
TextView tv = (TextView) ttv;
String txt = tv.getText().toString();
if (!TextUtils.isEmpty(txt) && tv.getId() != TIMESTAMPID) {
result.add(txt);
}
}
} catch (Exception e) {
Log.d(TAG, "FAILED to load notification " + e.toString());
Log.wtf(TAG, e);
return null;
//notification might have dissapeared by now
}
Log.d(TAG, "Return result" + result);
return result;
}
public void changeNotification(Service context, String text, int priority) {
if (builder != null) {
builder.setContentText(text);
builder.setPriority(priority);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(myID, builder.build());
}
}
/**
* @param callingService
* @param vendorId
* @param productId
* @param maxRetries
*/
public USBGpsManager(Service callingService, int vendorId, int productId, int maxRetries) {
this.gpsVendorId = vendorId;
this.gpsProductId = productId;
this.callingService = callingService;
this.maxConnectionRetries = maxRetries + 1;
this.nbRetriesRemaining = maxConnectionRetries;
this.appContext = callingService.getApplicationContext();
this.parser = new NmeaParser(10f, this.appContext);
locationManager = (LocationManager) callingService.getSystemService(Context.LOCATION_SERVICE);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(callingService);
deviceSpeed = sharedPreferences.getString(
USBGpsProviderService.PREF_GPS_DEVICE_SPEED,
callingService.getString(R.string.defaultGpsDeviceSpeed)
);
shouldSetTime = sharedPreferences.getBoolean(USBGpsProviderService.PREF_SET_TIME, false);
timeSetAlready = true;
defaultDeviceSpeed = callingService.getString(R.string.defaultGpsDeviceSpeed);
setDeviceSpeed = !deviceSpeed.equals(callingService.getString(R.string.autoGpsDeviceSpeed));
sirfGps = sharedPreferences.getBoolean(USBGpsProviderService.PREF_SIRF_GPS, false);
notificationManager = (NotificationManager) callingService.getSystemService(Context.NOTIFICATION_SERVICE);
parser.setLocationManager(locationManager);
Intent stopIntent = new Intent(USBGpsProviderService.ACTION_STOP_GPS_PROVIDER);
PendingIntent stopPendingIntent = PendingIntent.getService(appContext, 0, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);
connectionProblemNotificationBuilder = new NotificationCompat.Builder(appContext)
.setContentIntent(stopPendingIntent)
.setSmallIcon(R.drawable.ic_stat_notify);
Intent restartIntent = new Intent(USBGpsProviderService.ACTION_START_GPS_PROVIDER);
PendingIntent restartPendingIntent = PendingIntent.getService(appContext, 0, restartIntent, PendingIntent.FLAG_CANCEL_CURRENT);
serviceStoppedNotificationBuilder = new NotificationCompat.Builder(appContext)
.setContentIntent(restartPendingIntent)
.setSmallIcon(R.drawable.ic_stat_notify)
.setContentTitle(appContext.getString(R.string.service_closed_because_connection_problem_notification_title))
.setContentText(appContext.getString(R.string.service_closed_because_connection_problem_notification));
usbManager = (UsbManager) callingService.getSystemService(Service.USB_SERVICE);
}
public static void show(Service ctx, boolean enable) {
NotificationManager ntfMgr = (NotificationManager)ctx.getSystemService(Service.NOTIFICATION_SERVICE);
if (enable || Cfg.PersistentNotification) {
PendingIntent edit = PendingIntent.getActivity(ctx, 0, new Intent(Intent.ACTION_EDIT, null, ctx, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent cancel = PendingIntent.getService(ctx, 0, new Intent(enable ? Intent.ACTION_DELETE : Intent.ACTION_RUN, null, ctx, FilterService.class), PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent increase = PendingIntent.getService(ctx, 0, new Intent(ctx.getString(R.string.intent_darker), null, ctx, FilterService.class), PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent decrease = PendingIntent.getService(ctx, 0, new Intent(ctx.getString(R.string.intent_brighter), null, ctx, FilterService.class), PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Action.Builder ntf_act_increase = new NotificationCompat.Action.Builder(R.drawable.ic_add_circle_outline,
ctx.getString(R.string.darker), increase);
NotificationCompat.Action.Builder ntf_act_decrease = new NotificationCompat.Action.Builder(R.drawable.ic_remove_circle_outline,
ctx.getString(R.string.brighter), decrease);
NotificationCompat.Action.Builder ntf_act_configure = new NotificationCompat.Action.Builder(R.drawable.ic_build,
ctx.getString(R.string.configure), edit);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
.setContentTitle(ctx.getString(R.string.app_name) +
(enable ? " - " + Grids.PatternNames[Cfg.Pattern] : ""))
.setContentText(enable ? ctx.getString(R.string.tap_to_disable) : ctx.getString(R.string.enable_filter_checkbox))
.setContentInfo(ctx.getString(R.string.swipe_to_configure))
.setContentIntent(cancel)
.addAction(ntf_act_configure.build())
.addAction(ntf_act_increase.build())
.addAction(ntf_act_decrease.build())
.setDeleteIntent(cancel)
.setPriority(Cfg.HideNotification ? NotificationCompat.PRIORITY_MIN : NotificationCompat.PRIORITY_LOW)
.setSmallIcon(R.drawable.notification)
.setSound(null)
.setOngoing(true)
.setLocalOnly(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setTicker(null)
.setShowWhen(false);
Notification ntf = builder.build();
ctx.startForeground(NTF_ID, ntf);
} else {
ctx.stopForeground(true);
ntfMgr.cancel(NTF_ID);
}
}
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();
}
private void sendNotification(List<GraphObject> list)
{
if (service.get() == null)
return;
Service s = service.get();
final NotificationCompat.Builder builder = new NotificationCompat.Builder(service.get()).setSmallIcon(R.drawable.ic_notification)
.setContentTitle(s.getString(R.string.app_large_name)).setContentText(s.getString(R.string.friends_birthday_today, list.size()))
.setTicker(s.getString(R.string.friends_birthday_today, list.size())).setOnlyAlertOnce(true).setAutoCancel(true)
.setOnlyAlertOnce(true);
builder.setDefaults(android.app.Notification.DEFAULT_SOUND | android.app.Notification.DEFAULT_VIBRATE
| android.app.Notification.FLAG_ONLY_ALERT_ONCE);
// Big notification style
if (list.size() > 1)
{
builder.setNumber(list.size());
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(s.getString(R.string.friends_birthday_today, list.size()));
for (int i = 0; i < list.size(); i++)
{
inboxStyle.addLine(((Friend) list.get(i)).getName());
}
builder.setStyle(inboxStyle);
}
TaskStackBuilder stackBuilder = TaskStackBuilder.create(service.get());
Intent resultIntent = new Intent(service.get(), MainActivity.class);
resultIntent.putExtra(KlyphBundleExtras.SHOW_BIRTHDAYS, true);
// stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntentWithParentStack(resultIntent);
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);
if (builder != null)
{
final NotificationManager mNotificationManager = (NotificationManager) s.getSystemService(Context.NOTIFICATION_SERVICE);
final String tag = AttrUtil.getString(service.get(), R.string.app_name) + Math.round(Math.random() * 1000000);
final int id = 0;
// pair (tag, id) must be unique
// because n.getObject_id() may not be converted to an int
// tag is the unique key
mNotificationManager.notify(tag, id, builder.build());
}
}