FLAG_ACTIVITY_NEW_TASK

来源:互联网 发布:哪个软件英语口语测试 编辑:程序博客网 时间:2024/06/05 21:47

在红皮书第七章(p127)Using Intents and the Phone Dialer中
   有个程序是用intent打开Dialer这个application其中有几行代码是这样写的
        
    Intent DialIntent = new Intent(Intent.DIAL_ACTION,Uri.parse("tel:5551212"));
    DialIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH );

      其中第一行中的Intent.DIAL_ACTION在eclipse中出现错误(Intent.DIAL_ACTION can not be resolved),
  需要将其改为Intent.ACTION_DIAL.
     
      第二行代码中的Intent.NEW_TASK_LAUNCH也有错误(Intent.NEW_TASK_LAUNCH can not be resolved)
  需要将其改为 DialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        
       关于Uri.parse(String uristring)方法
         public static Uri  parse(String uriString)
         Creates a Uri which parses the given encoded URI string.
         Parameters
         uriString     an RFC 3296-compliant, encoded URI
         Returns

        * Uri for this given uri string

        Throws
        NullPointerException     if uriString is null
       这个方法是将uristring转化为Uri形式,并返回Uri形式的引用。如果Uristring为null将产生异常。

 
       在android文档中,没有setLaunchFlags(int flags)这个方法,但却有setFlags(int flags)方法。
       通过查android文档我们可以看到setfFlags(int flags)方法的定义如下:

      public Intent setFlags(int flags)

   Set special flags controlling how this intent is handled. Most values here depend on the type of component being executed by the Intent,
   specifically the FLAG_ACTIVITY_* flags are all for use with Context.startActivity() and the FLAG_RECEIVER_* flags are all for use
   with Context.sendBroadcast().

  See the Application Model documentation for important information on how some of these options impact the behavior of your application.
  Parameters
  flags     The desired flags.
  Returns

    * Returns the same Intent object, for chaining multiple calls into a single statement.

    See Also

    * getFlags()
    * addFlags(int)
    * FLAG_GRANT_READ_URI_PERMISSION
    * FLAG_GRANT_WRITE_URI_PERMISSION
    * FLAG_DEBUG_LOG_RESOLUTION
    * FLAG_FROM_BACKGROUND
    * FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
    * FLAG_ACTIVITY_BROUGHT_TO_FRONT
    * FLAG_ACTIVITY_CLEAR_TOP
    * FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    * FLAG_ACTIVITY_FORWARD_RESULT
    * FLAG_ACTIVITY_MULTIPLE_TASK
    * FLAG_ACTIVITY_NEW_TASK
    * FLAG_ACTIVITY_NO_HISTORY
    * FLAG_ACTIVITY_SINGLE_TOP
    * FLAG_RECEIVER_REGISTERED_ONLY


可以看出在以上的flags常量中没有NEW_TASK_LAUNCH.


public static final int FLAG_ACTIVITY_NEW_TASK

If set, this activity will become the start of a new task on this history stack.
A task (from the activity that started it to the next task activity) defines an
atomic group of activities that the user can move to. Tasks can be moved to the
 foreground and background; all of the activities inside of a particular task
always remain in the same order. See the Application Model documentation for
more details on tasks.

This flag is generally used by activities that want to present a "launcher" style
 behavior: they give the user a list of separate things that can be done, which
otherwise run completely independently of the activity launching them.

When using this flag, if a task is already running for the activity you are now
starting, then a new activity will not be started; instead, the current task will
simply be brought to the front of the screen with the state it was last in. See
 FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

This flag can not be used when the caller is requesting a result from the activity being launched.
Constant Value: 268435456 (0x10000000)


最终代码:
package com.android.AndroidPhoneDalier;

import android.app.Activity;
import android.os.Bundle;
import  android.content.Intent;
import android.net.Uri;

public class AndroidPhoneDalier extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Intent DialIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:88161644"));  
        DialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(DialIntent);
    }
}

各种FlAG定义

public static final int FLAG_ACTIVITY_BROUGHT_TO_FRONT

This flag is not normally set by application code, but set for
 you by the system as described in the launchMode documentation for the singleTask mode.
Constant Value: 4194304 (0x00400000)


public static final int FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in
the current task, then instead of launching a new instance of
that activity, all of the other activities on top of it will
be closed and this Intent will be delivered to the (now on top)
 old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls
 startActivity() with an Intent that resolves to the component of activity B,
then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

The currently running instance of task B in the above example will either
 receiving the new intent you are starting here in its onNewIntent() method,
 or be itself finished and restarting with the new intent. If it has declared
 its launch mode to be "multiple" (the default) it will be finished and
re-created; for all other launch modes it will receive the Intent in the current instance.

This launch mode can also be used to good effect in conjunction
with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task,
 it will bring any currently running instance of that task to the foreground,
 and then clear it to its root state. This is especially useful, for example,
 when launching an activity from the notification manager.

See the Application Model documentation for more details on tasks.
Constant Value: 67108864 (0x04000000)


public static final int FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

If set, the new activity is not kept in the list of recently
 launched activities.
Constant Value: 8388608 (0x00800000)


public static final int FLAG_ACTIVITY_FORWARD_RESULT

If set and this intent is being used to launch a new activity
 from an existing one, then the reply target of the existing
 activity will be transfered to the new activity. This way the
new activity can call setResult(int) and have that result sent
back to the reply target of the original activity.
Constant Value: 33554432 (0x02000000)

public static final int FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

If set, this activity is being launched from history (longpress home key).
Constant Value: 1048576 (0x00100000)

public static final int FLAG_ACTIVITY_MULTIPLE_TASK

Do not use this flag unless you are implementing your own top-level
 application launcher. Used in conjunction with FLAG_ACTIVITY_NEW_TASK
 to disable the behavior of bringing an existing task to the foreground.
 When set, a new task is always started to host the Activity for the Intent,
regardless of whether there is already an existing task running the same thing.

Because the default system does not include graphical task management,
 you should not use this flag unless you provide some way for a user to
return back to the tasks you have launched.

This flag is ignored if FLAG_ACTIVITY_NEW_TASK is not set.

See the Application Model documentation for more details on tasks.
Constant Value: 134217728 (0x08000000)


public static final int FLAG_ACTIVITY_NEW_TASK

If set, this activity will become the start of a new
 task on this history stack. A task (from the activity that
started it to the next task activity) defines an atomic group
 of activities that the user can move to. Tasks can be moved to
 the foreground and background; all of the activities inside of a
particular task always remain in the same order. See the Application
 Model documentation for more details on tasks.

This flag is generally used by activities that want to present
 a "launcher" style behavior: they give the user a list of separate
 things that can be done, which otherwise run completely independently
of the activity launching them.

When using this flag, if a task is already running for the activity you
are now starting, then a new activity will not be started; instead, the
 current task will simply be brought to the front of the screen with the
 state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

This flag can not be used when the caller is requesting a result from the activity being launched.
Constant Value: 268435456 (0x10000000)


public static final int FLAG_ACTIVITY_NO_HISTORY

If set, the new activity is not kept in the history stack.
Constant Value: 1073741824 (0x40000000)


public static final int FLAG_ACTIVITY_PREVIOUS_IS_TOP

If set and this intent is being used to launch a new activity
 from an existing one, the current activity will not be counted
as the top activity for deciding whether the new intent should be
delivered to the top instead of starting a new one. The previous
activity will be used as the top, with the assumption being that
the current activity will finish itself immediately.
Constant Value: 16777216 (0x01000000)


public static final int FLAG_ACTIVITY_RESET_TASK_IF_NEEDED


If set, and this activity is either being started in a new task or
bringing to the top an existing task, then it will be launched as
 the front door of the task. This will result in the application of
 any affinities needed to have that task in the proper state
 (either moving activities to or from it), or simply resetting that
task to its initial state if needed.
Constant Value: 2097152 (0x00200000)


public static final int FLAG_ACTIVITY_SINGLE_TOP

If set, the activity will not be launched if it is
already running at the top of the history stack.
Constant Value: 536870912 (0x20000000)


public static final int FLAG_DEBUG_LOG_RESOLUTION

A flag you can enable for debugging: when set, log messages
 will be printed during the resolution of this intent to show
you what has been found to create the final resolved list.
Constant Value: 8 (0x00000008)


public static final int FLAG_FROM_BACKGROUND

Can be set by the caller to indicate that this Intent is
coming from a background operation, not from direct user interaction.
Constant Value: 4 (0x00000004)


public static final int FLAG_GRANT_READ_URI_PERMISSION

If set, the recipient of this Intent will be granted permission
 to perform read operations on the Uri in the Intent's data.
Constant Value: 1 (0x00000001)

public static final int FLAG_GRANT_WRITE_URI_PERMISSION

If set, the recipient of this Intent will be granted permission
 to perform write operations on the Uri in the Intent's data.
Constant Value: 2 (0x00000002)

public static final int FLAG_RECEIVER_REGISTERED_ONLY
If set, when sending a broadcast only registered receivers will be
called -- no BroadcastReceiver components will be launched.
Constant Value: 1073741824 (0x40000000)

 

 

http://blog.chinaunix.net/u2/77727/showart_1683109.html

原创粉丝点击