1.5.42 Android Intent 关于 隐式意图& 显示意图& intent 跳转问题

 

一、android中的显示意图和隐式意图

显示意图要求必须知道被激活组件的包和class

隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件

若A 为主activity  B 接收跳转的activity 跳转步骤:

1.在主配置文件中声明B 至少要声明一个android:name 属性

 

<activity  
           android:name=".DemoActivity"  
           android:label="@string/demoActivity" >  
           <intent-filter>  
               <!-- 隐士意图中指定intent的名字 自己定义 可匹配多项 -->  
               <action android:name="com.itcast.intent.DemoActivity" />  
               <!-- 隐式intent需要指定的activity的类型,可自己定义该值,需要在调用的时候相对应不写该项默认为 android.intent.category.DEFAULT,可匹配多项 -->  
               <category android:name="android.intent.category.DEFAULT" />  
               <!-- 指定传想该activity数值的类型 和主机,如果指定该项,就必须在跳转activity的时候传入还数据和主机名 -->  
               <data  
                   android:host="cn.itcast.demo"  
                   android:scheme="itcast" />  
           </intent-filter>  
       </activity>

 

 

 

 2.在A的布局文件中一个textview和button,并添加点击事件

 

<TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="第一个activity" />  
  
   <Button  
       android:id="@+id/button"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:onClick="open"  
       android:text="跳转" />

 

 

3.点击事件触发的集中intent意图

 

/** 
     * 意图,描述一个动作,激活一个组件,使用其他的activity需要现在主文件中配置activity的名字等属性 
     * intent 要去做一件事 
     * @param view 
     */  
    public void open(View view) {  
        /** 
         * 方法一 
         */  
        Intent intent1 = new Intent();  
        // 1.是当前的包名,2跳转activity的类名,一定要加上包名  
        intent1.setClassName("com.itcast.intent", "com.itcast.intent.DemoActivity");  
        // startActivity(intent1);  
        /** 
         * 方法二 
         */  
        Intent intent2 = new Intent(this, DemoActivity.class);  
        // startActivity(intent2);  
        /** 
         * 方法三 
         */  
        Intent intent3 = new Intent();  
        ComponentName component = new ComponentName("com.itcast.intent", "com.itcast.intent.DemoActivity");  
        intent3.setComponent(component);  
        // startActivity(intent3);  
        /** 
         * 上面三种方法要求必须知道被激活组件的包和class,称为显示意图 
         */  
        // ******************************************************************//  
        /** 
         * 隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件<br> 
         * 如果要激活另外程序的组件 
         */  
        Intent intent = new Intent();  
        intent.setAction("com.itcast.intent.DemoActivity");  
        // 不管在主配置文件中有沒有声明跳转activity的category,都要写该项,不然报错找到activity,不些有系統會以默認的類型  
        intent.addCategory("android.intent.category.DEFAULT");  
        // 如果在声明activity的时候指定了data属性,在跳转的时候就一定要设置他的data属性值,和配置的属性值相等,不然也会报找不到的错误  
        intent.setData(Uri.parse("itcast://cn.itcast.demo"));  
        // startActivity(intent);  
        Intent imageIntent = new Intent();  
        imageIntent.setAction(Intent.ACTION_PICK);  
        imageIntent.setType("image/*");// 设置数据类型  
        startActivity(imageIntent);  
    }

 

 

以上转载:http://blog.csdn.net/hello2me/article/details/8059220一、不需要返回值的跳转

 

二、android intent 跳转的返回值问题

1.不需要返回值的跳转

Intent intent=new Intent();
intent.setClass(目前的acitivy.this, 目标activity.class);
startActivity(intent);
2.需要返回值的跳转
Intent intent=new Intent();
intent.setClass(目前的acitivy.this, 目标activity.class);
startActivity(intent);
startActivityForResult(intent, 状态值(int类型));
状态值作为接受目标返回值的验证。
requestCode 接收返回的状态值
resultCode :RESULT_OK RESULT_CANCELED 可以自己赋值,按返回键时,系统默认赋值为:RESULT_CANCELED
目标activity 关闭才能返回请求activity
例如:
(1)请求页面

 

public class MainActivy extends Activity {  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent=new Intent();
        intent.setClass(MainActivy.this, ReceiveMSG.class);
        startActivityForResult(intent, 0);      
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==0) {
            if (resultCode==RESULT_OK) {
                Toast.makeText(this, "Send SMS RESULT_OK", Toast.LENGTH_SHORT).show();
            }else if (resultCode==RESULT_CANCELED) {
                Toast.makeText(this, "result cancel", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, resultCode, Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(this, "requsetcode not 0", Toast.LENGTH_SHORT).show();
        }
       
    }
}

 

(2)接收返回值界面

 

public class ReceiveMSG extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);        
        ReceiveMSG.this.setResult(RESULT_OK);
        this.finish();       
    }
}

 


以上转载:http://blog.sina.com.cn/s/blog_7309444701014u2d.html
 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值