一、显示意图
1、跳转页面,只带过去数据,不返回数据。
Intent intent=new Intent(this,SecondActivity.class); intent.putExtra("String", "字符串"); intent.putExtra("int", 250); intent.putExtra("img", BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); startActivity(intent);
2、返回多个数据。
1、例如跳转A、B界面
public void goa(View v) { Intent intent = new Intent(this, A.class); startActivityForResult(intent, 3); } public void gob(View v) { Intent intent = new Intent(this, B.class); startActivityForResult(intent, 4); }
2、AB界面代码。其中setResult(1, intent);后面必须加finish();resultCode的参数暂时不起作用
Intent intent=new Intent(A.this,MainActivity.class); intent.putExtra("data", str[position]); setResult(1, intent); finish();
3、主界面得到返回数据。int requestCode参数用于辨别哪个界面传来的。主要是主界面跳转时,所写的标记。
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==3){ String text = data.getStringExtra("data"); Toast.makeText(getApplicationContext(),text+"|"+requestCode, 0).show(); }else if(requestCode==4){ String text = data.getStringExtra("data"); Toast.makeText(getApplicationContext(),text+"|"+requestCode, 0).show(); } super.onActivityResult(requestCode, resultCode, data); }
二、隐式意图。没有明确的指定跳转的界面。
1.在配置文件中做如下操作。
代码:
Intent intent=new Intent(); intent.setAction("com.itheima.xxx"); intent.addCategory("android.intent.category.DEFAULT"); intent.putExtra("String", "字符串"); intent.putExtra("int", 250); intent.putExtra("img", BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); startActivity(intent);
附《Intent调用常见系统组件方法》
// 调用浏览器 Uri webViewUri = Uri.parse("http://blog.csdn.net/zuolongsnail"); Intent intent = new Intent(Intent.ACTION_VIEW, webViewUri); // 调用地图 Uri mapUri = Uri.parse("geo:100,100"); Intent intent = new Intent(Intent.ACTION_VIEW, mapUri); // 播放mp3 Uri playUri = Uri.parse("file:///sdcard/test.mp3"); Intent intent = new Intent(Intent.ACTION_VIEW, playUri); intent.setDataAndType(playUri, "audio/mp3"); // 调用拨打电话 Uri dialUri = Uri.parse("tel:10086"); Intent intent = new Intent(Intent.ACTION_DIAL, dialUri); // 直接拨打电话,需要加上权限<uses-permission id="android.permission.CALL_PHONE" /> Uri callUri = Uri.parse("tel:10086"); Intent intent = new Intent(Intent.ACTION_CALL, callUri); // 调用发邮件(这里要事先配置好的系统Email,否则是调不出发邮件界面的) Uri emailUri = Uri.parse("mailto:[email protected]"); Intent intent = new Intent(Intent.ACTION_SENDTO, emailUri); // 直接发邮件 Intent intent = new Intent(Intent.ACTION_SEND); String[] tos = { "[email protected]" }; String[] ccs = { "[email protected]" }; intent.putExtra(Intent.EXTRA_EMAIL, tos); intent.putExtra(Intent.EXTRA_CC, ccs); intent.putExtra(Intent.EXTRA_TEXT, "the email text"); intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); intent.setType("text/plain"); Intent.createChooser(intent, "Choose Email Client"); // 发短信 Intent intent = new Intent(Intent.ACTION_VIEW); intent.putExtra("sms_body", "the sms text"); intent.setType("vnd.android-dir/mms-sms"); // 直接发短信 Uri smsToUri = Uri.parse("smsto:10086"); Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri); intent.putExtra("sms_body", "the sms text"); // 发彩信 Uri mmsUri = Uri.parse("content://media/external/images/media/23"); Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra("sms_body", "the sms text"); intent.putExtra(Intent.EXTRA_STREAM, mmsUri); intent.setType("image/png"); // 卸载应用 Uri uninstallUri = Uri.fromParts("package", "com.app.test", null); Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri); // 安装应用 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File("/sdcard/test.apk"), "application/vnd.android.package-archive"); // 在Android Market中查找应用 Uri uri = Uri.parse("market://search?q=愤怒的小鸟"); Intent intent = new Intent(Intent.ACTION_VIEW, uri);
时间: 2024-10-21 05:04:03