Intent()用法简介

来源:互联网 发布:程序源码网 编辑:程序博客网 时间:2024/05/22 08:19

1. 先说在setClass启动一个Activity的方法吧:

    Intent intent = new Intent();

    intent.setClass(this, CreatePlaylist.class)    //参数一为当前Package的context,t当前Activity的context就是this,其他Package可能用到createPackageContex()参数二为你要打开的Activity的类名

    startActivity(intent);

   2. 通过Component Name来打开的方式

       Intent intent = new Intent();
       intent.setAction(Intent.ACTION_MAIN);          //添加一些特性,具体可以查看Intent文档,相关属性的介绍
       intent.addCategory(Intent.CATEGORY_LAUNCHER);
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

      

      //通过Bundle向要打开的的Activity传递一些数据

       Bundle bundle = new Bundle();
       bundle.putString("data", new String(" Hello World"));
       intent.putExtras(bundle);
    
       intent.setComponent(new ComponentName(
           new String("com.android.testActivity"), new String("com.android.testActivity.testActivity")));
       startActivity(intent);

0 0
原创粉丝点击