下面列出了怎么用android.support.v4.content.IntentCompat的API类实例代码及写法,或者点击链接到github查看源代码。
public static Intent getParentActivityIntent(Context context, ComponentName componentname)
{
String s = getParentActivityName(context, componentname);
if (s == null)
{
return null;
}
ComponentName componentname1 = new ComponentName(componentname.getPackageName(), s);
if (getParentActivityName(context, componentname1) == null)
{
return IntentCompat.makeMainActivity(componentname1);
} else
{
return (new Intent()).setComponent(componentname1);
}
}
public static Intent getParentActivityIntent(Context context, Class class1)
{
String s = getParentActivityName(context, new ComponentName(context, class1));
if (s == null)
{
return null;
}
ComponentName componentname = new ComponentName(context, s);
if (getParentActivityName(context, componentname) == null)
{
return IntentCompat.makeMainActivity(componentname);
} else
{
return (new Intent()).setComponent(componentname);
}
}
/**
* Start the task stack constructed by this builder. The Context used to obtain
* this builder must be an Activity.
*
* <p>On devices that do not support API level 11 or higher the topmost activity
* will be started as a new task. On devices that do support API level 11 or higher
* the new task stack will be created in its entirety.</p>
*
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)
*/
public void startActivities(Bundle options) {
if (mIntents.isEmpty()) {
throw new IllegalStateException(
"No intents added to TaskStackBuilder; cannot startActivities");
}
Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
intents[0] = new Intent(intents[0]).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK |
IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
if (!ContextCompat.startActivities(mSourceContext, intents, options)) {
Intent topIntent = new Intent(intents[intents.length - 1]);
topIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mSourceContext.startActivity(topIntent);
}
}
@Override
public Intent getParentActivityIntent(Activity activity) {
String parentName = NavUtils.getParentActivityName(activity);
if (parentName == null) return null;
// If the parent itself has no parent, generate a main activity intent.
final ComponentName target = new ComponentName(activity, parentName);
try {
final String grandparent = NavUtils.getParentActivityName(activity, target);
final Intent parentIntent = grandparent == null
? IntentCompat.makeMainActivity(target)
: new Intent().setComponent(target);
return parentIntent;
} catch (NameNotFoundException e) {
Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
"' in manifest");
return null;
}
}
/**
* Start the task stack constructed by this builder. The Context used to obtain
* this builder must be an Activity.
*
* <p>On devices that do not support API level 11 or higher the topmost activity
* will be started as a new task. On devices that do support API level 11 or higher
* the new task stack will be created in its entirety.</p>
*
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)
*/
public void startActivities(Bundle options) {
if (mIntents.isEmpty()) {
throw new IllegalStateException(
"No intents added to TaskStackBuilder; cannot startActivities");
}
Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
intents[0] = new Intent(intents[0]).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK |
IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
if (!ContextCompat.startActivities(mSourceContext, intents, options)) {
Intent topIntent = new Intent(intents[intents.length - 1]);
topIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mSourceContext.startActivity(topIntent);
}
}
@Override
public Intent getParentActivityIntent(Activity activity) {
String parentName = NavUtils.getParentActivityName(activity);
if (parentName == null) return null;
// If the parent itself has no parent, generate a main activity intent.
final ComponentName target = new ComponentName(activity, parentName);
try {
final String grandparent = NavUtils.getParentActivityName(activity, target);
final Intent parentIntent = grandparent == null
? IntentCompat.makeMainActivity(target)
: new Intent().setComponent(target);
return parentIntent;
} catch (NameNotFoundException e) {
Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
"' in manifest");
return null;
}
}
/**
* Start the task stack constructed by this builder. The Context used to obtain
* this builder must be an Activity.
*
* <p>On devices that do not support API level 11 or higher the topmost activity
* will be started as a new task. On devices that do support API level 11 or higher
* the new task stack will be created in its entirety.</p>
*
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)
*/
public void startActivities(Bundle options) {
if (mIntents.isEmpty()) {
throw new IllegalStateException(
"No intents added to TaskStackBuilder; cannot startActivities");
}
Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
intents[0] = new Intent(intents[0]).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK |
IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
if (!ContextCompat.startActivities(mSourceContext, intents, options)) {
Intent topIntent = new Intent(intents[intents.length - 1]);
topIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mSourceContext.startActivity(topIntent);
}
}
@Override
public Intent getParentActivityIntent(Activity activity) {
String parentName = NavUtils.getParentActivityName(activity);
if (parentName == null) return null;
// If the parent itself has no parent, generate a main activity intent.
final ComponentName target = new ComponentName(activity, parentName);
try {
final String grandparent = NavUtils.getParentActivityName(activity, target);
final Intent parentIntent = grandparent == null
? IntentCompat.makeMainActivity(target)
: new Intent().setComponent(target);
return parentIntent;
} catch (NameNotFoundException e) {
Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
"' in manifest");
return null;
}
}
/**
* Start the task stack constructed by this builder. The Context used to obtain
* this builder must be an Activity.
*
* <p>On devices that do not support API level 11 or higher the topmost activity
* will be started as a new task. On devices that do support API level 11 or higher
* the new task stack will be created in its entirety.</p>
*
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)
*/
public void startActivities(Bundle options) {
if (mIntents.isEmpty()) {
throw new IllegalStateException(
"No intents added to TaskStackBuilder; cannot startActivities");
}
Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
intents[0] = new Intent(intents[0]).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK |
IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
if (!ContextCompat.startActivities(mSourceContext, intents, options)) {
Intent topIntent = new Intent(intents[intents.length - 1]);
topIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mSourceContext.startActivity(topIntent);
}
}
@Override
public Intent getParentActivityIntent(Activity activity) {
String parentName = NavUtils.getParentActivityName(activity);
if (parentName == null) return null;
// If the parent itself has no parent, generate a main activity intent.
final ComponentName target = new ComponentName(activity, parentName);
try {
final String grandparent = NavUtils.getParentActivityName(activity, target);
final Intent parentIntent = grandparent == null
? IntentCompat.makeMainActivity(target)
: new Intent().setComponent(target);
return parentIntent;
} catch (NameNotFoundException e) {
Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
"' in manifest");
return null;
}
}
/**
* Start the task stack constructed by this builder. The Context used to obtain
* this builder must be an Activity.
*
* <p>On devices that do not support API level 11 or higher the topmost activity
* will be started as a new task. On devices that do support API level 11 or higher
* the new task stack will be created in its entirety.</p>
*
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)
*/
public void startActivities(Bundle options) {
if (mIntents.isEmpty()) {
throw new IllegalStateException(
"No intents added to TaskStackBuilder; cannot startActivities");
}
Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
intents[0] = new Intent(intents[0]).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK |
IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
if (!ContextCompat.startActivities(mSourceContext, intents, options)) {
Intent topIntent = new Intent(intents[intents.length - 1]);
topIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mSourceContext.startActivity(topIntent);
}
}
@Override
public Intent getParentActivityIntent(Activity activity) {
String parentName = NavUtils.getParentActivityName(activity);
if (parentName == null) return null;
// If the parent itself has no parent, generate a main activity intent.
final ComponentName target = new ComponentName(activity, parentName);
try {
final String grandparent = NavUtils.getParentActivityName(activity, target);
final Intent parentIntent = grandparent == null
? IntentCompat.makeMainActivity(target)
: new Intent().setComponent(target);
return parentIntent;
} catch (NameNotFoundException e) {
Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
"' in manifest");
return null;
}
}
private void confirm_stealth(String password) {
final View dialogView = View.inflate(context, R.layout.dialog_confirm_stealth, null);
((TextView) dialogView
.findViewById(R.id.stealth_keycode))
.append(password);
new AlertDialog.Builder(context)
.setInverseBackgroundForced(true)
.setView(dialogView)
.setMessage(R.string.Settings__try_once_before_hide)
.setPositiveButton(getString(R.string.OK), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor
= PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putBoolean(Config.SHOW_STEALTH_MODE_TUTORIAL, true);
editor.apply();
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:"));
dial.setFlags(IntentCompat.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dial);
getActivity().finish();
}
})
.show();
}
@Override
public void onReceive(final Context context, Intent intent) {
// Gets the intent, check if it matches our secret code
if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction()) &&
intent.getExtras() != null) {
Intent launcher = new Intent(context, MainActivity.class);
//These flags are added to make the new mainActivity in the home stack.
//i.e. back button returns to home not dialer.
launcher.addFlags(IntentCompat.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
String phoneNumber = intent.getExtras().getString(android.content.Intent.EXTRA_PHONE_NUMBER);
String openPin = PreferenceManager.getDefaultSharedPreferences(context)
.getString(Config.STEALTH_MODE_PASSWORD, "");
if (!openPin.equals("")) {
if (("*#" + openPin).equals(phoneNumber)) {
// Launch the main app!!
launchActivity(context, launcher);
}
}
}
}
public IntentBuilder setHtmlText(String htmlText) {
this.mIntent.putExtra(IntentCompat.EXTRA_HTML_TEXT, htmlText);
if (!this.mIntent.hasExtra("android.intent.extra.TEXT")) {
setText(Html.fromHtml(htmlText));
}
return this;
}
public String getHtmlText() {
String result = this.mIntent.getStringExtra(IntentCompat.EXTRA_HTML_TEXT);
if (result != null) {
return result;
}
CharSequence text = getText();
if (text instanceof Spanned) {
return Html.toHtml((Spanned) text);
}
if (text != null) {
return ShareCompat.IMPL.escapeHtml(text);
}
return result;
}
public Intent getParentActivityIntent(Activity activity) {
Intent intent = null;
String parentName = NavUtils.getParentActivityName(activity);
if (parentName != null) {
ComponentName target = new ComponentName(activity, parentName);
try {
intent = NavUtils.getParentActivityName(activity, target) == null ? IntentCompat.makeMainActivity(target) : new Intent().setComponent(target);
} catch (NameNotFoundException e) {
Log.e(NavUtils.TAG, "getParentActivityIntent: bad parentActivityName '" + parentName + "' in manifest");
}
}
return intent;
}
public static Intent getParentActivityIntent(Context context, Class<?> sourceActivityClass) throws NameNotFoundException {
String parentActivity = getParentActivityName(context, new ComponentName(context, sourceActivityClass));
if (parentActivity == null) {
return null;
}
ComponentName target = new ComponentName(context, parentActivity);
return getParentActivityName(context, target) == null ? IntentCompat.makeMainActivity(target) : new Intent().setComponent(target);
}
public static Intent getParentActivityIntent(Context context, ComponentName componentName) throws NameNotFoundException {
String parentActivity = getParentActivityName(context, componentName);
if (parentActivity == null) {
return null;
}
ComponentName target = new ComponentName(componentName.getPackageName(), parentActivity);
return getParentActivityName(context, target) == null ? IntentCompat.makeMainActivity(target) : new Intent().setComponent(target);
}
static void restartApp(Activity activity) {
Intent newIntent = new Intent(activity, MainActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
activity.startActivity(newIntent);
activity.overridePendingTransition(0, 0);
activity.finish();
}
private void restartApp() {
Intent newIntent = new Intent(getActivity(), MainActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(newIntent);
getActivity().overridePendingTransition(0, 0);
getActivity().finish();
}
public void openIntentWithClear(Intent intent) {
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
mContext.startActivity(mainIntent);
try {
((Activity) mContext).finish();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Get the styled HTML text shared with the target activity.
* If no HTML text was supplied but {@link Intent#EXTRA_TEXT} contained
* styled text, it will be converted to HTML if possible and returned.
* If the text provided by {@link Intent#EXTRA_TEXT} was not styled text,
* it will be escaped by {@link android.text.Html#escapeHtml(CharSequence)}
* and returned. If no text was provided at all, this method will return null.
*
* @return Styled text provided by the sender as HTML.
*/
public String getHtmlText() {
String result = mIntent.getStringExtra(IntentCompat.EXTRA_HTML_TEXT);
if (mIntent == null) {
CharSequence text = getText();
if (text instanceof Spanned) {
result = Html.toHtml((Spanned) text);
} else if (text != null) {
result = IMPL.escapeHtml(text);
}
}
return result;
}
public PendingIntent getPendingIntent(Context context, Intent[] intents, int requestCode,
int flags, Bundle options) {
intents[0] = new Intent(intents[0]).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK |
IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
return TaskStackBuilderHoneycomb.getActivitiesPendingIntent(context, requestCode,
intents, flags);
}
public PendingIntent getPendingIntent(Context context, Intent[] intents, int requestCode,
int flags, Bundle options) {
intents[0] = new Intent(intents[0]).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK |
IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
return TaskStackBuilderJellybean.getActivitiesPendingIntent(context, requestCode,
intents, flags, options);
}
/**
* Obtain a {@link PendingIntent} for launching the task constructed by this builder so far.
*
* @param requestCode Private request code for the sender
* @param flags May be {@link PendingIntent#FLAG_ONE_SHOT},
* {@link PendingIntent#FLAG_NO_CREATE}, {@link PendingIntent#FLAG_CANCEL_CURRENT},
* {@link PendingIntent#FLAG_UPDATE_CURRENT}, or any of the flags supported by
* {@link Intent#fillIn(Intent, int)} to control which unspecified parts of the
* intent that can be supplied when the actual send happens.
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)
* @return The obtained PendingIntent
*/
public PendingIntent getPendingIntent(int requestCode, int flags, Bundle options) {
if (mIntents.isEmpty()) {
throw new IllegalStateException(
"No intents added to TaskStackBuilder; cannot getPendingIntent");
}
Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
intents[0] = new Intent(intents[0]).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK |
IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
// Appropriate flags will be added by the call below.
return IMPL.getPendingIntent(mSourceContext, intents, requestCode, flags, options);
}
/**
* Return an array containing the intents added to this builder. The intent at the
* root of the task stack will appear as the first item in the array and the
* intent at the top of the stack will appear as the last item.
*
* @return An array containing the intents added to this builder.
*/
public Intent[] getIntents() {
Intent[] intents = new Intent[mIntents.size()];
if (intents.length == 0) return intents;
intents[0] = new Intent(mIntents.get(0)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK |
IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
for (int i = 1; i < intents.length; i++) {
intents[i] = new Intent(mIntents.get(i));
}
return intents;
}
/**
* Obtain an {@link Intent} that will launch an explicit target activity
* specified by sourceActivityClass's {@link #PARENT_ACTIVITY} <meta-data>
* element in the application's manifest.
*
* @param context Context for looking up the activity component for sourceActivityClass
* @param sourceActivityClass {@link java.lang.Class} object for an Activity class
* @return a new Intent targeting the defined parent activity of sourceActivity
* @throws NameNotFoundException if the ComponentName for sourceActivityClass is invalid
*/
public static Intent getParentActivityIntent(Context context, Class<?> sourceActivityClass)
throws NameNotFoundException {
String parentActivity = getParentActivityName(context,
new ComponentName(context, sourceActivityClass));
if (parentActivity == null) return null;
// If the parent itself has no parent, generate a main activity intent.
final ComponentName target = new ComponentName(context, parentActivity);
final String grandparent = getParentActivityName(context, target);
final Intent parentIntent = grandparent == null
? IntentCompat.makeMainActivity(target)
: new Intent().setComponent(target);
return parentIntent;
}
/**
* Obtain an {@link Intent} that will launch an explicit target activity
* specified by sourceActivityClass's {@link #PARENT_ACTIVITY} <meta-data>
* element in the application's manifest.
*
* @param context Context for looking up the activity component for the source activity
* @param componentName ComponentName for the source Activity
* @return a new Intent targeting the defined parent activity of sourceActivity
* @throws NameNotFoundException if the ComponentName for sourceActivityClass is invalid
*/
public static Intent getParentActivityIntent(Context context, ComponentName componentName)
throws NameNotFoundException {
String parentActivity = getParentActivityName(context, componentName);
if (parentActivity == null) return null;
// If the parent itself has no parent, generate a main activity intent.
final ComponentName target = new ComponentName(
componentName.getPackageName(), parentActivity);
final String grandparent = getParentActivityName(context, target);
final Intent parentIntent = grandparent == null
? IntentCompat.makeMainActivity(target)
: new Intent().setComponent(target);
return parentIntent;
}
/**
* Get the styled HTML text shared with the target activity.
* If no HTML text was supplied but {@link Intent#EXTRA_TEXT} contained
* styled text, it will be converted to HTML if possible and returned.
* If the text provided by {@link Intent#EXTRA_TEXT} was not styled text,
* it will be escaped by {@link android.text.Html#escapeHtml(CharSequence)}
* and returned. If no text was provided at all, this method will return null.
*
* @return Styled text provided by the sender as HTML.
*/
public String getHtmlText() {
String result = mIntent.getStringExtra(IntentCompat.EXTRA_HTML_TEXT);
if (result == null) {
CharSequence text = getText();
if (text instanceof Spanned) {
result = Html.toHtml((Spanned) text);
} else if (text != null) {
result = IMPL.escapeHtml(text);
}
}
return result;
}