一、Intent类的六个属性
action – The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
data – The data to operate on, such as a person record in the contacts database, expressed as a Uri.
category – Gives additional information about the action to execute. For example, CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application, while CATEGORY_ALTERNATIVE means it should be included in a list of alternative actions the user can perform on a piece of data.
type – Specifies an explicit type (a MIME type) of the intent data. Normally the type is inferred from the data itself. By setting this attribute, you disable that evaluation and force an explicit type.
component – Specifies an explicit name of a component class to use for the intent. Normally this is determined by looking at the other information in the intent (the action, data/type, and categories) and matching that with a component that can handle it. If this attribute is set then none of the evaluation is performed, and this component is used exactly as is. By specifying this attribute, all of the other Intent attributes become optional.
extras – This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.
----参考自andriod开发者文档-------------------------------------------------------------------------------------------
通过这些属性的简单配合属性,我结合andriod的开发者文档来分别实现几个简单的功能,代码如下:
- 打开系统联系人
public class IntentDemoActivity extends Activity {
private static final int REQUEST_SELECT_CONTACT = 1;
private static final String TAG = IntentDemoActivity.class.getSimpleName();
private View.OnClickListener mOnClickListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent;
switch(v.getId()){
//其实该功能有待拓展,通过选择某一联系人,应可以查看他的详细信息等
case R.id.contact_list_button:
intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.setData(ContactsContract.Contacts.CONTENT_URI);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_SELECT_CONTACT);
//startActivity(intent);
}
break;
case R.id.phone_call_button:
intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
if(intent.resolveActivity(getPackageManager())!= null){
startActivity(intent);
}
break;
case R.id.sms_send_button:
intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:10086"));
if(intent.resolveActivity(getPackageManager())!=null){
startActivity(intent);
}
break;
case R.id.browser_open_button:
intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
if(intent.resolveActivity(getPackageManager())!=null){
startActivity(intent);
}
break;
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Button contact_list_button;
Button phone_call_button;
Button sms_send_button;
Button browser_open_button;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_demo);
contact_list_button = (Button)findViewById(R.id.contact_list_button);
phone_call_button = (Button)findViewById(R.id.phone_call_button);
sms_send_button = (Button)findViewById(R.id.sms_send_button);
browser_open_button = (Button)findViewById(R.id.browser_open_button);
contact_list_button.setOnClickListener(mOnClickListener);
phone_call_button.setOnClickListener(mOnClickListener);
sms_send_button.setOnClickListener(mOnClickListener);
browser_open_button.setOnClickListener(mOnClickListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) {
Uri contactUri = data.getData();
// Do something with the selected contact at contactUri
Log.i(TAG,"after click"+requestCode);
}
}
}
- 打电话
- 发短信
- 打开浏览器
二、向activity传递信息
1.1.延时后跳转到另一界面
- 要进行跳转的页面SplashActivity声明一个Handler的实例mhandler
- 调用mhandler的postDelayed方法,
- 重写runnable对象的run方法
- Intent对象,然后调用startActivity方法
1.2.跳转时携带信息
- 声明Intent对象实例后,调用putExtra方法,然后调用startActivity(Intent)
- 另一个页面Intent intent = getIntent(),然后intent.getSerializable方法,,再setTitle
1.3返回时要携带信息
startActivityforResult(),此时需要定义一个RequestCode,返回时会带回来一个resultCode
回调函数onActivityResult()中根据requstcode和resultcode是否对应,可以处理带回来的信息。
这是一个1s后跳转的小demo
public class SplashActivity extends Activity {
public static final String USER = "user";
public static final int REQUEST_CODE = 9999;
private static final String TAG = SplashActivity.class.getSimpleName();
private Handler mhandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mhandler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
}
},1000);
}
}
tips:
快捷键:ctrl+Alt+C:将字符串提出来变成常量
快捷键:alt+enter展开错误提示
快捷键:生成构造方法:alt+insert(重写父类方法等)
快捷键:ctrl+alt+F