Intent相关知识(2)

1.4种类
Intent中还可以包含组件类型信息 它用来被执行动作的附加信息 开发人员可以在一个Intent对象中指定任意数量的种类描述

使用addCategory方法添加一种类到Intent对象中
使用removeCategory方法删除之前添加的种类
使用getCategory方法获取Intent对象中的所有种类

例子:创建Intent对象 并为其设置种类常量
Intent intent = new Intent();
intent.addCategory(Intent.CAREGORY_LAUNCHER);

Intent.CAREGORY_LAUNCHER 表示Intent的接受者应该在Launcher中作为顶级应用出现

1.5附加信息
附加信息可以作为一个Bundle对象使用putExtras方法和getExtras方法安装和读取

putExtras方法  public Intent putExtra (String name,String value)
name:附加信息的名称
value:附加信息的值
返回值:Intent对象

getExtras方法  public Bundle gerExtras()
返回值为Bundle对象 用来存储获取到的Intent附加信息

例子:使用putExtras方法和getExtras方法实现为Intent添加附加信息、读取信息的功能。

Step1:在布局文件main.xml中添加一个Button
< Button
         android:id = "@+id/btn"
         android:layout_width = "60dp"
         android:layout_height = "40dp"
         android:text = "跳转"
         />
Step2:创建一个link.xml文件 作为AcceptdataActivity的布局文件

< TextView  
          android:id = "@+id/txt"
         android:layout_width = "fill_parent"
         android:layout_height = "wrap_content"
         android:text = "链接页面"
         />
Step3:在MainActivity中设置一个int常量 用来作为请求标志
private final static int REQUEST_CODE = 1;

Step4:MainActivity中 
  public  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
        setContentView(R.layout.  main );
        Button btnButton=(Button) findViewById(R.id. btn  ); //获取Button按钮
        btnButton. setOnClickListener( listener ); //为Button按钮设置监听事件
    }
     private  OnClickListener  listener =  new  OnClickListener() { //创建监听对象
          @Override
          public  void  onClick(View v) {
              //  TODO  Auto-generated method stub
            Intent intent=  new  Intent(); //创建Intent对象
              //设置要访问的Activity
            intent.setClass(MainActivity.  this , AcceptdataActivity. class  );
            intent.putExtra(  "str" ,  "第一个Activity传过来的值"  ); //设置要传递的值
            startActivityForResult(intent,  REQUEST_CODE ); //启动Activity
        }
    };

Step5:AcceptdataActivity中
protected  void  onCreate(Bundle savedInstanceState) {
          //  TODO  Auto-generated method stub
          super .onCreate(savedInstanceState);
        setContentView(R.layout.  link );
        Intent intent=getIntent();  //创建Intent对象
        Bundle bundle=intent.getExtras();  //获取传递值,并用Bundle接收
        String str=bundle.getString(  "str" ); //获取传递的字符串值
        TextView txt=(TextView)findViewById(R.id. txt  ); //获取TextView组件
        txt.setText(str);  //设置文本
    }

1.6 标志

标志主要用来指示Android程序如何去启动一个活动和启动后如何对待它 所有的标志都定义在Intent类中

使用setFlags方法和addFlags方法添加一个标志到Intent对象中 使用getFlags方法获取Intent对象在的所有标志 

例子:
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_NEW_TASK);
// 系统会检查当前所有已创建的Task中是否有需要启动的Activity的Task 如果有则在该Task上创建Activity 如果没有 则新建具有该Activity属性的Task 并在该新建的Task上创建Activity 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值