Android中Intent的详解(显式,隐式,系统,startActivityForResult);

 //自定义隐性Intent


Intent intent2 =new Intent("zhh.by");
intent2.addCategory("android.intent.category.DEFAULT");//这个是默认的可以不写
intent2.addCategory("zhh.byh");
startActivity(intent2);


去匹配主清单中的
 
           
               


               
               
           

       

startActivityForResult

(FirstActivity中)startActivityForResult(intent5, 1);----->(SecondActivity中)setResult(RESULT_OK, intent);------>(FirstActivity中)onActivityResult();

项目结构:

Android中Intent的详解(显式,隐式,系统,startActivityForResult);_第1张图片

核心代码

FirstActivity



public class FirstActivity extends Activity implements OnClickListener {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);

button1=(Button) findViewById(R.id.button_1);
button2=(Button) findViewById(R.id.button_2);
button3=(Button) findViewById(R.id.button_3);
button4=(Button) findViewById(R.id.button_4);
button5=(Button) findViewById(R.id.button_5);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_1:
//显性Intent
Intent intent1=new Intent(FirstActivity.this,
SecondActivity.class);
startActivity(intent1);


break;
case R.id.button_2:
             //自定义隐性Intent
Intent intent2 =new Intent("zhh.by");
intent2.addCategory("android.intent.category.DEFAULT");//这个是默认的可以不写
intent2.addCategory("zhh.byh");
startActivity(intent2);


break;
case R.id.button_3:
//调用系统浏activity览器打开网页
Intent intent3 =new Intent(Intent.ACTION_VIEW);
intent3.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent3);



break;
case R.id.button_4:
// 调用系统打电话activity
Intent intent4 = new Intent(Intent.ACTION_DIAL);
intent4.setData(Uri.parse("tel:10086"));
startActivity(intent4);


break;
case R.id.button_5:
// startActivity()的用法
// 返回数据给上一个activity,把SecondActivity的数据发挥给FirstActivity
Intent intent5 = new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent5, 1);



break;


}


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode==RESULT_OK) {
String returnedData=data.getStringExtra("data_return");
//SecondActivity返回的数据
Toast.makeText(this, "返回的数据"+returnedData, 10).show();
Log.i("1","返回的数据"+returnedData );

}

break;


}
}


}//class

SecondActivity

public class SecondActivity extends Activity{
private Button second_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
second_button=(Button) findViewById(R.id.second_button);
second_button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("data_return", "Hello FirstActivity");
setResult(RESULT_OK, intent);
finish();

}
});

}


}//class

主清单


              android:name="com.zhh.android.FirstActivity"
            android:label="@string/app_name" >
           
               


               
           

       
        
       
           
               


               
               
           

       


下载我的例子

http://download.csdn.net/detail/zhaihaohao1/8278383


你可能感兴趣的:(intent)