本文整理汇总了Java中android.content.ComponentName类的典型用法代码示例。如果您正苦于以下问题:Java ComponentName类的具体用法?Java ComponentName怎么用?Java ComponentName使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ComponentName类属于android.content包,在下文中一共展示了ComponentName类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: disableComponent
import android.content.ComponentName; //导入依赖的package包/类
private void disableComponent() {
PackageManager packageManager = getPackageManager();
ComponentName componentName = new ComponentName(this, MainActivity.class);
// packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
// PackageManager.DONT_KILL_APP);
int res = packageManager.getComponentEnabledSetting(componentName);
if (res == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
|| res == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
// 隐藏应用图标
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
} else {
// 显示应用图标
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
PackageManager.DONT_KILL_APP);
}
}
示例2: openApplication
import android.content.ComponentName; //导入依赖的package包/类
public void openApplication(Context context, String packageName) {
PackageInfo pi;
try {
pi = context.getPackageManager().getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
pi = null;
e.printStackTrace();
}
if (pi != null) {
Intent resolveIntent = new Intent("android.intent.action.MAIN", null);
resolveIntent.setPackage(pi.packageName);
ResolveInfo ri = (ResolveInfo) context.getPackageManager().queryIntentActivities(resolveIntent, 0).iterator().next();
if (ri != null) {
packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent("android.intent.action.MAIN");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(new ComponentName(packageName, className));
context.startActivity(intent);
}
}
}
示例3: isAppIsInBackground
import android.content.ComponentName; //导入依赖的package包/类
private boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
return isInBackground;
}
示例4: getParentActivityIntent
import android.content.ComponentName; //导入依赖的package包/类
/**
* Obtain an {@link Intent} that will launch an explicit target activity specified by
* this activity's logical parent. The logical parent is named in the application's manifest
* by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
* Activity subclasses may override this method to modify the Intent returned by
* super.getParentActivityIntent() or to implement a different mechanism of retrieving
* the parent intent entirely.
*
* @return a new Intent targeting the defined parent of this activity or null if
* there is no valid parent.
*/
@Nullable
public Intent getParentActivityIntent() {
final String parentName = mActivityInfo.parentActivityName;
if (TextUtils.isEmpty(parentName)) {
return null;
}
// If the parent itself has no parent, generate a main activity intent.
final ComponentName target = new ComponentName(this, parentName);
try {
final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
final String parentActivity = parentInfo.parentActivityName;
final Intent parentIntent = parentActivity == null
? Intent.makeMainActivity(target)
: new Intent().setComponent(target);
return parentIntent;
} catch (NameNotFoundException e) {
Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
"' in manifest");
return null;
}
}
示例5: setComponentEnabled
import android.content.ComponentName; //导入依赖的package包/类
/**
* 切换桌面图标
*
* @param componentDisabledName
* @param componentEnabledName
*/
private void setComponentEnabled(ComponentName componentDisabledName
, ComponentName componentEnabledName) {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(componentDisabledName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(componentEnabledName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
snackBarShow(mCoordinatorLayout, getString(R.string.launcher_icon_had_change));
// Find launcher and kill it (restart launcher)
// MIUI8 Android6.0.6:android.content.pm.PackageManager$NameNotFoundException
// ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
// Intent i = new Intent(Intent.ACTION_MAIN);
// i.addCategory(Intent.CATEGORY_HOME);
// i.addCategory(Intent.CATEGORY_DEFAULT);
// List<ResolveInfo> resolves = pm.queryIntentActivities(i, 0);
// for (ResolveInfo res : resolves) {
// if (res.activityInfo != null) {
// am.killBackgroundProcesses(res.activityInfo.packageName);
// }
// }
}
示例6: createMediaSession
import android.content.ComponentName; //导入依赖的package包/类
private MediaSessionCompat createMediaSession() {
MediaSessionCompat mediaSession = new MediaSessionCompat(
mContext,
mContext.getString(R.string.app_name),
new ComponentName(mContext.getPackageName(),
getButtonReceiverClassName()),
null);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
| MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setCallback(mMediaSessionCallback);
// TODO(mlamouri): the following code is to work around a bug that hopefully
// MediaSessionCompat will handle directly. see b/24051980.
try {
mediaSession.setActive(true);
} catch (NullPointerException e) {
// Some versions of KitKat do not support AudioManager.registerMediaButtonIntent
// with a PendingIntent. They will throw a NullPointerException, in which case
// they should be able to activate a MediaSessionCompat with only transport
// controls.
mediaSession.setActive(false);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setActive(true);
}
return mediaSession;
}
示例7: dismissKeyboardShortcutsHelper
import android.content.ComponentName; //导入依赖的package包/类
/**
* Dismiss the Keyboard Shortcuts screen.
*/
public final void dismissKeyboardShortcutsHelper() {
Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
intent.setComponent(new ComponentName(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME,
KEYBOARD_SHORTCUTS_RECEIVER_CLASS_NAME));
sendBroadcast(intent);
}
示例8: showFileContent
import android.content.ComponentName; //导入依赖的package包/类
@Override
public void showFileContent(ActivityInfo activityInfo, File file,
int requestCode) throws AppChooserException {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setComponent(new ComponentName(activityInfo.getPkg(), activityInfo.getCls()));
intent.setDataAndType(Uri.fromFile(file), activityInfo.getMimeType());
ComponentName componentName = intent.resolveActivity(getActivity().getPackageManager());
if (componentName != null) {
try {
if (requestCode == ResolversConsts.DEFAULT_REQUEST_CODE) {
getActivity().startActivity(intent);
} else {
getActivity().startActivityForResult(intent, requestCode);
}
} catch (ActivityNotFoundException e) {
throw new AppChooserException(e);
}
} else {
throw new AppChooserException();
}
}
示例9: openAppByPackageName
import android.content.ComponentName; //导入依赖的package包/类
/**
* 根据包名打开第三方应用
*
* @param context
* @param packageName
* @throws PackageManager.NameNotFoundException
*/
public static void openAppByPackageName(Context context, String packageName) throws PackageManager.NameNotFoundException {
PackageInfo pi;
try {
pi = MyApplication.getContext().getPackageManager().getPackageInfo(packageName, 0);
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.setPackage(pi.packageName);
PackageManager pManager = MyApplication.getContext().getPackageManager();
List<ResolveInfo> apps = pManager.queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null) {
packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//重点是加这个
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
context.startActivity(intent);
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
示例10: LauncherAppWidgetInfo
import android.content.ComponentName; //导入依赖的package包/类
public LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
if (appWidgetId == CUSTOM_WIDGET_ID) {
itemType = LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
} else {
itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
}
this.appWidgetId = appWidgetId;
this.providerName = providerName;
// Since the widget isn't instantiated yet, we don't know these values. Set them to -1
// to indicate that they should be calculated based on the layout and minWidth/minHeight
spanX = -1;
spanY = -1;
// We only support app widgets on current user.
user = Process.myUserHandle();
restoreStatus = RESTORE_COMPLETED;
}
示例11: requestShowKeyboardShortcuts
import android.content.ComponentName; //导入依赖的package包/类
/**
* Request the Keyboard Shortcuts screen to show up. This will trigger
* {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
*/
public final void requestShowKeyboardShortcuts() {
Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
intent.setComponent(new ComponentName(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME,
KEYBOARD_SHORTCUTS_RECEIVER_CLASS_NAME));
sendBroadcast(intent);
}
示例12: isAppIsInBackground
import android.content.ComponentName; //导入依赖的package包/类
/**
* Method checks if the app is in background or not
*/
public static boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
return isInBackground;
}
示例13: onAppPicked
import android.content.ComponentName; //导入依赖的package包/类
public void onAppPicked(ResolveInfo info) {
final Intent intent = new Intent(getIntent());
intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.setComponent(new ComponentName(
info.activityInfo.applicationInfo.packageName, info.activityInfo.name));
startActivityForResult(intent, CODE_FORWARD);
}
示例14: onClick
import android.content.ComponentName; //导入依赖的package包/类
@Override
public void onClick(View view) {
int i = view.getId();
if (i == R.id.gprsACBtn) {
context.startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)
.addCategory(Intent.ACTION_MAIN)
.setComponent(new ComponentName("com.android.settings"
, "com.android.settings.Settings$DataUsageSummaryActivity"))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
dismiss();
} else if (i == R.id.wifiACBtn) {
context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
dismiss();
}
}
示例15: onAttach
import android.content.ComponentName; //导入依赖的package包/类
@Override
public void onAttach(Activity activity) {
logd("onAttach(%s)", activity.getClass().getSimpleName());
super.onAttach(activity);
mContext = activity;
mPm = mContext.getPackageManager();
mNoMan = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
try {
mListener.registerAsSystemService(mContext, new ComponentName(mContext.getPackageName(),
this.getClass().getCanonicalName()), ActivityManager.getCurrentUser());
} catch (RemoteException e) {
Log.e(TAG, "Cannot register listener", e);
}
}