intent和intentfilter

intent 和intent Filters


startActivity()的机制

用到了IBinder ipc 用到了进程间通讯机制

activity有四种LaunchMode

当startActivity()的时候不知道启动的是不是和自己的activity在一个

进程中,所以要用 IPC 进程间通讯来调用


简单的用法 

1

A.class中

1
2
3
Intent intent =  new  Intent(A. this , B. class );
intent.putExtra( "sundy.demo" "你好" );
startActivity(intent);

        B.class中

        

1
2
3
Intent intent =  this .getIntent();
String  value = intent.getExtras().getString( "key" );
Toast.makeText( this , value,  1 ).show();

2

    A.class中

1
2
3
4
Intent intent =  new  Intent();
intent.putExtra( "key" "123" );
intent.setAction( "com.wang.cn" );
startActivity(intent);

    B。class中

.

1
2
3
Intent intent =  this .getIntent();
String  value = intent.getExtras().getString( "key" );
Toast.makeText( this , value,  1 ).show();

            要在mainfest中设置B。clas的activity中的intent-filter的action中设置

1
2
3
4
5
6
7
<activity android:name= ".B"  >
             <intent-filter>
                 <action android:name= "com.wang.cn"  />
 
                 <category android:name= "android.intent.category.DEFAULT"  />
             </intent-filter>
         </activity>


必须写上 <category android:name="android.intent.category.DEFAULT" />这一句不然会报错。。

3. 简单的打电话 代码

1
2
3
4
5
Intent intent =  new  Intent();
 
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse( "tel:12345645555" ));
startActivity(intent);

    setAction和setData都是系統定義好 。這裡只說下用法

4.获取data中的值

    A。class中

1
2
3
4
Intent intent =  new  Intent();
intent.setAction( "com.wang.cn" );
intent.setData(Uri.parse( "tel:12345645555" ));
startActivity(intent);

    B。class中

1
2
3
4
Intent intent =  this .getIntent();
 
String  uri = intent.getDataString();
Toast.makeText( this , uri,  1 ).show();

 setAction和setData都是系統定義好 。這裡只說下用法

1
2
3
4
5
6
7
8
9
10
<activity android:name= ".Rose"  >
             <intent-filter>
                 <action android:name= "com.wang.cn"  />
 
                 <category android:name= "android.intent.cat  egory.DEFAULT"  />
 
                 <data android:scheme= "tel"  >
                 </data>
             </intent-filter>
         </activity>

5.startActivityForResult 方法

    A。class中

1
2
3
Intent intent =  new  Intent();
intent.setClass(A. this ,B. class );
startActivityForResult(intent,  123 );

        

    在A。clas的activity中 导入系统的onActivityResult方法

1
2
3
4
5
6
7
8
9
@Override
     protected  void  onActivityResult( int  requestCode,  int  resultCode, Intent data) {
         // TODO Auto-generated method stub
         super .onActivityResult(requestCode, resultCode, data);
         if  (resultCode ==  321 ) {
             String  value = data.getExtras().getString( "name" );
             Toast.makeText( this , value,  1 ).show();
         }
     }

    


B.class中


1
2
3
4
5
6
7
8
9
10
11
button.setOnClickListener( new  OnClickListener() {
 
             @Override
             public  void  onClick(View arg0) {
 
                 Intent intent = this .getIntent();
                 intent.putExtra( "name" "111111111" );
                 setResult( 321 , intent);
                 finish();
             }
         });


当resultCode一样的时候 回传值成功。。

6.intent 传递 对象 类  等等


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值