下面列出了android.content.Context#getPackageName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
*
* @param context A {@link Context} to use for creating the cache dir.
* @param stack An {@link HttpStack} to use for the network, or null for default.
* @return A started {@link RequestQueue} instance.
*/
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
if (stack == null) {
stack = new HurlStack();
}
Network network = new BasicNetwork(stack);
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
queue.start();
return queue;
}
public static boolean enableNotificationListenerComponent(Context paramContext, boolean paramBoolean) {
try {
if (Build.VERSION.SDK_INT < 18) {
return false;
}
if (paramContext != null) {
PackageManager localPackageManager = paramContext.getPackageManager();
ComponentName componentName = new ComponentName(paramContext.getPackageName(), GetAwayNotificationListenerService.class.getName());
if (paramBoolean) {
localPackageManager.setComponentEnabledSetting(componentName, 1, 1);
} else {
localPackageManager.setComponentEnabledSetting(componentName, 2, 1);
}
}
return true;
} catch (Throwable e) {
e.printStackTrace();
return false;
}
}
@Override
protected RemoteViews buildErrorLayout(AppWidgetManager awm, Context context, int appWidgetId, String error) {
Intent intent = new Intent(Extension.ACTION_MAIN_SCREEN);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Intent retryIntent = new Intent(context, getWidgetClass());
PendingIntent retryPendingIntent = PendingIntent.getBroadcast(context, 0, retryIntent, 0);
RemoteViews rv = new RemoteViews(context.getPackageName(), getWidgetLayout());
rv.setOnClickPendingIntent(R.id.widget_header, pendingIntent);
rv.setOnClickPendingIntent(R.id.btn_retry, retryPendingIntent);
rv.setViewVisibility(R.id.progress_bar, View.GONE);
rv.setViewVisibility(R.id.btn_retry, View.VISIBLE);
rv.setViewVisibility(R.id.prayerlist, View.GONE);
return rv;
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i = 0; i < appWidgetIds.length; i++) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_collection);
Intent intent = new Intent(context, PlacesWidgetRemoteViewsService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(R.id.widget_list, intent);
Intent toastIntent = new Intent(context, PlacesWidgetRemoteViewsService.class);
toastIntent.setAction(PlacesWidgetProvider.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.widget_list, toastPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
com.google.cloud.backend.GCMIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
private static File getExternalCacheDir(Context context) {
File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
if (!appCacheDir.exists()) {
if (!appCacheDir.mkdirs()) {
return null;
}
try {
new File(appCacheDir, ".nomedia").createNewFile();
} catch (IOException e) {
}
}
return appCacheDir;
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
String mTimezoneCode = ClockWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clock_widget);
TimeZone targetTimezone = TimeZone.getTimeZone(mTimezoneCode);
views.setTextViewText(R.id.tView_timezone, targetTimezone.getID());
views.setString(R.id.txtClockDigitalClock, "setTimeZone", mTimezoneCode);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private static String getVersion(final Context context) {
final String packageName = context.getPackageName();
try {
return context.getPackageManager().getPackageInfo(packageName, 0).versionName;
} catch (PackageManager.NameNotFoundException | RuntimeException e) {
return UNKNOWN;
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
appWidgetManager.getAppWidgetInfo(appWidgetId);
// Get the layout for the App Widget and update fields
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_clementine);
switch (mCurrentClementineAction) {
case DEFAULT:
case CONNECTION_STATUS:
updateViewsOnConnectionStatusChange(context, views);
break;
case STATE_CHANGE:
updateViewsOnStateChange(context, views);
break;
}
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
public static boolean isListeningAuthorized(Context context) {
ContentResolver contentResolver = context.getContentResolver();
String enabledNotificationListeners = Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
String packageName = context.getPackageName();
return !(enabledNotificationListeners == null || !enabledNotificationListeners.contains(packageName));
}
private static String obtainApplicationIdentifier(Context context) {
try {
String packageName = context.getPackageName();
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
String appIdentifier = String.format(DEFAULT_LOCALE, THREE_STRING_FORMAT, packageName,
packageInfo.versionName, packageInfo.versionCode);
return appIdentifier;
} catch (Exception exception) {
return EMPTY_STRING;
}
}
protected void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.left_right_widget);
views.setOnClickPendingIntent(R.id.left_btn,
PendingIntent.getBroadcast(context, 0, new Intent(ArcadeCommon.ACTION_LEFT), 0));
views.setOnClickPendingIntent(R.id.right_btn,
PendingIntent.getBroadcast(context, 0, new Intent(ArcadeCommon.ACTION_RIGHT), 0));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private static SessionEventsState getSessionEventsState(Context context, AccessTokenAppIdPair accessTokenAppId) {
synchronized (staticLock) {
SessionEventsState state = stateMap.get(accessTokenAppId);
if (state == null) {
// Retrieve attributionId, but we will only send it if attribution is supported for the app.
String attributionId = Settings.getAttributionId(context.getContentResolver());
state = new SessionEventsState(attributionId, context.getPackageName(), hashedDeviceAndAppId);
stateMap.put(accessTokenAppId, state);
}
return state;
}
}
public void run(Context context, Intent intent, int componentState, boolean onAutostart) {
// Enable or Disable UserPresentReceiver (or bypass the modification)
//Log.d("Cordova AppStarter", "UserPresentReceiver component, new state:" + String.valueOf(componentState));
if( componentState != BYPASS_USERPRESENT_MODIFICATION ) {
ComponentName receiver = new ComponentName(context, UserPresentReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver, componentState, PackageManager.DONT_KILL_APP);
}
// Starting your app...
//Log.d("Cordova AppStarter", "STARTING APP...");
SharedPreferences sp = context.getSharedPreferences(AutoStart.PREFS, Context.MODE_PRIVATE);
String packageName = context.getPackageName();
String activityClassName = sp.getString(AutoStart.ACTIVITY_CLASS_NAME, "");
if( !activityClassName.equals("") ){
//Log.d("Cordova AppStarter", className);
Intent activityIntent = new Intent();
activityIntent.setClassName(
context, String.format("%s.%s", packageName, activityClassName));
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (onAutostart) {
activityIntent.putExtra(CORDOVA_AUTOSTART, true);
}
context.startActivity(activityIntent);
}
// Start a service in the background.
String serviceClassName = sp.getString(AutoStart.SERVICE_CLASS_NAME, "");
String servicePackageName = serviceClassName.substring(0, serviceClassName.lastIndexOf("."));
if ( !serviceClassName.equals("") ) {
Intent serviceIntent = new Intent();
serviceIntent.setClassName(servicePackageName, serviceClassName);
if ( onAutostart ) {
serviceIntent.putExtra(CORDOVA_AUTOSTART, true);
}
context.startService(serviceIntent);
}
}
/**
* 得到软件显示版本信息
* @param context 上下文
* @return 当前版本信息
*/
public static String getVerName(Context context) {
if (context == null) return "";
String verName = "";
try {
String packageName = context.getPackageName();
verName = context.getPackageManager().getPackageInfo(packageName, 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
Logger.e(e);
}
return verName;
}
/**
* Check if the IME specified by the context is enabled.
* CAVEAT: This may cause a round trip IPC.
*
* @param context package context of the IME to be checked.
* @param imm the {@link InputMethodManager}.
* @return true if this IME is enabled.
*/
public static boolean isThisImeEnabled(final Context context,
final InputMethodManager imm) {
final String packageName = context.getPackageName();
for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
if (packageName.equals(imi.getPackageName())) {
return true;
}
}
return false;
}
/**
* 获取引擎加密H5文件的读取协议头,与ACEContentProvider对应
* 形如:content://com.appcan.packagename.sp/android_asset/
*/
public static String getACEContentProviderPrefix(Context context){
String packageName = context.getPackageName();
String contentPrefix = "content://" + packageName + ".sp/android_asset/";
return contentPrefix;
}
private RemoteViews configSmall(Context context, SparseArray<PendingIntent> pendingIntentsMap) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout_small);
views.setOnClickPendingIntent(R.id.iv_launch_app, pendingIntentsMap.get(R.id.iv_launch_app));
return views;
}
public FakeR(Context context) {
this.context = context;
packageName = context.getPackageName();
}
/**
* 获取插件宿主的包名
*
* @param context 插件context
* @return 宿主包名,如果插件是独立安装运行,则返回插件自身包名
*/
public static String getHostPackageName(Context context) {
String pkgName = null;
if (context == null) {
return pkgName;
}
Context appContext = getHostApplicationContextPrivate(context);
pkgName = appContext.getPackageName();
return pkgName;
}