下面列出了android.content.Intent#makeMainActivity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Add the activity parent chain as specified by the
* {@link android.R.attr#parentActivityName parentActivityName} attribute of the activity
* (or activity-alias) element in the application's manifest to the task stack builder.
*
* @param sourceActivityName Must specify an Activity component. All parents of
* this activity will be added
* @return This TaskStackBuilder for method chaining
*/
public TaskStackBuilder addParentStack(ComponentName sourceActivityName) {
final int insertAt = mIntents.size();
PackageManager pm = mSourceContext.getPackageManager();
try {
ActivityInfo info = pm.getActivityInfo(sourceActivityName, 0);
String parentActivity = info.parentActivityName;
while (parentActivity != null) {
final ComponentName target = new ComponentName(info.packageName, parentActivity);
info = pm.getActivityInfo(target, 0);
parentActivity = info.parentActivityName;
final Intent parent = parentActivity == null && insertAt == 0
? Intent.makeMainActivity(target)
: new Intent().setComponent(target);
mIntents.add(insertAt, parent);
}
} catch (NameNotFoundException e) {
Log.e(TAG, "Bad ComponentName while traversing activity parent metadata");
throw new IllegalArgumentException(e);
}
return this;
}
/**
* Launches an activity based on its component name.
*
* @param activity Current foreground activity.
* @param componentName Component name of the app to be launched.
*/
static void quickLaunch(Activity activity, String componentName) throws ActivityNotFoundException {
// When receiving 'none', it's probably a gesture that hasn't been registered.
if ("none".equals(componentName)) {
return;
}
ComponentName component = ComponentName.unflattenFromString(componentName);
// Forcibly end if we can't unflatten the string.
if (component == null) {
return;
}
Intent intent = Intent.makeMainActivity(component);
intent.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
activity.startActivity(intent);
}
/**
* Launches an activity based on its component name.
*
* @param activity Current foreground activity.
* @param componentName Component name of the app to be launched.
*/
static void quickLaunch(Activity activity, String componentName) throws ActivityNotFoundException {
// When receiving 'none', it's probably a gesture that hasn't been registered.
if ("none".equals(componentName)) {
return;
}
ComponentName component = ComponentName.unflattenFromString(componentName);
// Forcibly end if we can't unflatten the string.
if (component == null) {
return;
}
Intent intent = Intent.makeMainActivity(component);
intent.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
activity.startActivity(intent);
}
private NotificationItem(int id, String title, String desc, String channelId) {
super(id, title, desc);
final Intent[] intents = new Intent[2];
intents[0] = Intent.makeMainActivity(
new ComponentName(DemoApplication.CONTEXT, MainActivity.class));
intents[1] = new Intent(DemoApplication.CONTEXT, NotificationSampleActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivities(
DemoApplication.CONTEXT, 0, intents,
PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new NotificationCompat.Builder(
FileDownloadHelper.getAppContext(),
channelId);
} else {
//noinspection deprecation
builder = new NotificationCompat.Builder(FileDownloadHelper.getAppContext())
.setDefaults(Notification.DEFAULT_LIGHTS)
.setPriority(NotificationCompat.PRIORITY_MIN);
}
builder.setContentTitle(getTitle())
.setContentText(desc)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = Intent.makeMainActivity(new ComponentName(this, MainActivity.class));
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
TaskStackBuilder.create(this)
.addNextIntent(upIntent).startActivities();
finish();
} else {
// This activity is part of the application's task, so simply
// navigate up to the hierarchical parent activity.
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(upIntent);
finish();
}
return true;
}
return super.onOptionsItemSelected(item);
}
public Intent getLaunchIntent() {
ContentResolver resolver = this.mPluginContext.getContentResolver();
Intent launcher = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
for (PackageParser.Activity activity : this.mPackage.activities) {
for (PackageParser.ActivityIntentInfo intentInfo : activity.intents) {
if (intentInfo.match(resolver, launcher, false, TAG) > 0) {
return Intent.makeMainActivity(activity.getComponentName());
}
}
}
return null;
}
/**
* Finds an intent to start an Activity of the given class. It looks up target context first and
* fallback to instrumentation context.
*/
default Intent getIntentForActivity(Class<? extends Activity> activityClass) {
Intent intent =
Intent.makeMainActivity(
new ComponentName(getInstrumentation().getTargetContext(), activityClass));
if (getInstrumentation().getTargetContext().getPackageManager().resolveActivity(intent, 0)
!= null) {
return intent;
}
return Intent.makeMainActivity(
new ComponentName(getInstrumentation().getContext(), activityClass));
}
public static Intent getDefaultAlarmsIntent(Context context) {
PackageManager pm = context.getPackageManager();
for (String packageName : DESK_CLOCK_PACKAGES) {
try {
ComponentName cn = new ComponentName(packageName, "com.android.deskclock.AlarmClock");
pm.getActivityInfo(cn, 0);
return Intent.makeMainActivity(cn);
} catch (PackageManager.NameNotFoundException ignored) {
}
}
return getDefaultClockIntent(context);
}
public static Intent a(ComponentName componentname)
{
return Intent.makeMainActivity(componentname);
}
public static Intent makeMainActivity(ComponentName mainActivity) {
return Intent.makeMainActivity(mainActivity);
}
public static Intent makeMainActivity(ComponentName mainActivity) {
return Intent.makeMainActivity(mainActivity);
}
public static Intent makeMainActivity(ComponentName mainActivity) {
return Intent.makeMainActivity(mainActivity);
}
public static Intent makeMainActivity(ComponentName mainActivity) {
return Intent.makeMainActivity(mainActivity);
}