下面列出了android.content.Context#getSystemService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Delete push notification channel group.
*
* @param context The application context.
* @param groupId The id of the channel.
*/
private static void deleteNotificationGroup(Context context, String groupId) {
if (context == null || TextUtils.isEmpty(groupId)) {
return;
}
if (BuildUtil.isNotificationChannelSupported(context)) {
try {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
Log.e("Notification manager is null");
return;
}
notificationManager.deleteNotificationChannelGroup(groupId);
} catch (Throwable t) {
Util.handleException(t);
}
}
}
private AudioSlidePlayer(@NonNull Context context,
@Nullable
MasterSecret masterSecret,
@NonNull AudioSlide slide,
@NonNull Listener listener) {
this.context = context;
this.masterSecret = masterSecret;
this.slide = slide;
this.listener = new WeakReference<>(listener);
this.progressEventHandler = new ProgressEventHandler(this);
this.audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
this.sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
this.proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (Build.VERSION.SDK_INT >= 21) {
this.wakeLock = AppUtil.INSTANCE.getPowerManager(context).newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
} else {
this.wakeLock = null;
}
}
public NotificationChannel(Context context, DatabaseReference reference, String channel_Id, int channel_no) {
this.context = context;
this.channel_Id = channel_Id;
this.reference = reference.child(channel_Id);
String channel_Key = context.getString(R.string.shared_preference_key) + channel_Id;
sharedPreferences = context.getSharedPreferences(channel_Key, Context.MODE_PRIVATE);
count = sharedPreferences.getInt("count", 0);
id = channel_no;
messages = new ArrayList<>();
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
public static String getUserCountryCode(Context c) {
final TelephonyManager manager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
//getNetworkCountryIso
final String countryLetterCode = manager.getSimCountryIso().toUpperCase();
String[] rl = c.getResources().getStringArray(R.array.country_codes);
for (String aRl : rl) {
String[] g = aRl.split(",");
if (g[1].trim().equals(countryLetterCode.trim())) {
return g[0];
}
}
return "";
}
/**
* Uses given views to hide soft keyboard and to clear current focus.
*
* @param context
* Context
* @param views
* Currently focused views
*/
public static void hideSoftKeyboard(Context context, View... views) {
if (views == null)
return;
InputMethodManager manager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
for (View currentView : views) {
if (null == currentView)
continue;
manager.hideSoftInputFromWindow(currentView.getWindowToken(), 0);
currentView.clearFocus();
}
}
public static int getScreenHeightPixels(Context mContext) {
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
return height;
}
@SuppressWarnings({"MissingPermission"})
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return networkInfo.isAvailable();
}
}
return false;
}
public static String getDeviceResolution(Context context) {
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getWidth() + "x" + display.getHeight();
}
public void sync(Context context, String packageName) {
try {
Log.d("AptoideTimeline", "Starting timelinePostsService");
SharedPreferences sPref = PreferenceManager.getDefaultSharedPreferences(Aptoide.getContext());
if(sPref.contains("timelineTimestamp") && sPref.getBoolean("socialtimelinenotifications", true)) {
TimelineActivityJson timelineActivityJson = TimelineCheckRequestSync.getRequest("new_installs");
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("fromTimeline", true);
intent.setClassName(packageName, MainActivity.class.getName());
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_aptoide_fb_notification)
.setContentIntent(resultPendingIntent)
.setOngoing(false)
.setAutoCancel(true)
.setContentTitle(context.getString(R.string.notification_timeline_posts))
.setContentText(context.getString(R.string.notification_social_timeline)).build();
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
ArrayList<String> avatarLinks = new ArrayList<String>();
try {
setAvatares(avatarLinks, timelineActivityJson.getNew_installs().getFriends());
if (Build.VERSION.SDK_INT >= 16) {
RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.push_notification_timeline_activity);
expandedView.setTextViewText(R.id.description, context.getString(R.string.notification_timeline_posts));
expandedView.removeAllViews(R.id.linearLayout2);
for (String avatar : avatarLinks) {
Bitmap loadedImage = ImageLoader.getInstance().loadImageSync(avatar);
RemoteViews imageView = new RemoteViews(context.getPackageName(), R.layout.timeline_friend_iv);
imageView.setImageViewBitmap(R.id.friend_avatar, loadedImage);
expandedView.addView(R.id.linearLayout2, imageView);
}
notification.bigContentView = expandedView;
}
if (!avatarLinks.isEmpty()) {
final NotificationManager managerNotification = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
managerNotification.notify(86459, notification);
}
} catch (NullPointerException ignored) {
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public View newCollectionItemView(Context context, int groupId, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.item_image, parent, false);
}
public OrientationMonitor(Compositor compositor, Context context) {
this.compositor = compositor;
powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
listeners = new ArrayList<>();
}
public PointsAdapter( Context context )
{
inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
}
/**
* 获取ConnectivityManager
*/
public static ConnectivityManager getConnectivityManager(Context context) {
return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
public static TelephonyManager getTelephonyManager(Context context) {
return (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
}
public static int getScreenHeight(Context context) {
WindowManager display = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
return display.getDefaultDisplay().getHeight();
}
static public NetworkInfo getConnectedNetworkInfo(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {
return networkInfo;
}
networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;
networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;
networkInfo = connectivityManager.getNetworkInfo(CONNECTIVITY_TYPE_WIMAX);
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;
networkInfo = connectivityManager.getNetworkInfo(CONNECTIVITY_TYPE_ETHERNET);
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;
log.info("Could not find any connected network...");
return null;
}
private void cancelAllNotifications(Context context) {
final NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Activity.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
/**
* Constructor the enables auto-requery.
*
* @deprecated This option is discouraged, as it results in Cursor queries
* being performed on the application's UI thread and thus can cause poor
* responsiveness or even Application Not Responding errors. As an alternative,
* use {@link android.app.LoaderManager} with a {@link android.content.CursorLoader}.
*
* @param context The context where the ListView associated with this adapter is running
* @param layout resource identifier of a layout file that defines the views
* for this list item. Unless you override them later, this will
* define both the item views and the drop down views.
*/
@Deprecated
public ResourceCursorAdapter(Context context, int layout, Cursor c) {
super(context, c);
mLayout = mDropDownLayout = layout;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDropDownInflater = mInflater;
}
/**
* Vibrate with a given pattern.
* <p/>
* <p>
* Pass in an array of ints that are the durations for which to turn on or off
* the vibrator in milliseconds. The first value indicates the number of milliseconds
* to wait before turning the vibrator on. The next value indicates the number of milliseconds
* for which to keep the vibrator on before turning it off. Subsequent values alternate
* between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
* </p><p>
* To cause the pattern to repeat, pass the index into the pattern array at which
* to start the repeat, or -1 to disable repeating.
* </p>
* <p>This method requires the caller to hold the permission
* {@link android.Manifest.permission#VIBRATE}.
*
* @param pattern an array of longs of times for which to turn the vibrator on or off.
* @param repeat the index into pattern at which to repeat, or -1 if
* you don't want to repeat.
*/
public static void vibrate(Context context, long[] pattern, int repeat) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern, repeat);
}
/**
* Constructor.
*
* @param context Context
* @param list The list of book-marks
*/
ApplicationListAdapter(final Context context, final List<KnownApplicationInfo> list) {
super(context, 0, list);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mList = list;
}