1.打电话:
1 Intent intent = new Intent(); 2 intent.setAction(Intent.ACTION_CALL); 3 intent.setData(Uri.parse("tel:159xxxxxxxx")); 4 startActivity(intent);
必须在AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
2.发短信:
1 Intent intentsms = new Intent(); 2 intentsms.setAction(Intent.ACTION_SENDTO); 3 intentsms.setData(Uri.parse("smsto:159xxxxxxxx")); 4 intentsms.putExtra("sms_body", "xxxxxx"); 5 startActivity(intentsms);
同样必须在AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.SEND_SMS" />
3.通过Intent之间进行Activity值传递
1 public class IntentDemoActivity extends Activity { 2 3 private Button mainBtn = null; 4 private final static Integer REQUEST_CODE = 1; 5 /** Called when the activity is first created. */ 6 @Override 7 public void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.main); 10 mainBtn = (Button)findViewById(R.id.mainBtn); 11 mainBtn.setOnClickListener(new OnClickListener() { 12 @Override 13 public void onClick(View v) { 14 // TODO Auto-generated method stub 15 Intent intent = new Intent(); 16 intent.setClass(IntentDemoActivity.this, SecondActivity.class); 17 intent.putExtra("str", "hello"); 18 //传对象 19 // intent.putExtra("obj", 序列化对象); 20 //启动activity 21 // startActivity(intent); 22 //可以接受返回参数,通过onActivityResult()接收 23 startActivityForResult(intent, REQUEST_CODE); 24 } 25 }); 26 } 27 @Override 28 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 29 // super.onActivityResult(requestCode, resultCode, data); 30 if(requestCode == REQUEST_CODE){ 31 if(resultCode == SecondActivity.RESULT_CODE){ 32 Bundle bundle = data.getExtras(); 33 String backStr = bundle.getString("backData"); 34 Toast.makeText(IntentDemoActivity.this, backStr, Toast.LENGTH_LONG).show(); 35 } 36 } 37 } 38 }
上面的效果是点击IntentDemoActivity中的按钮时向SecondActivity中传递hello,并且通过onActivityResult接收返回信息。
1 public class SecondActivity extends Activity { 2 3 private TextView secondTxt = null; 4 private Button btn = null; 5 public static final Integer RESULT_CODE = 1; 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 9 super.onCreate(savedInstanceState); 10 //设置布局文件 11 setContentView(R.layout.second); 12 Intent intent = getIntent(); 13 Bundle bundle = intent.getExtras(); 14 String str = bundle.getString("str"); 15 16 secondTxt = (TextView)findViewById(R.id.tv); 17 secondTxt.setText(str); 18 19 btn = (Button)findViewById(R.id.sendBtn); 20 btn.setOnClickListener(new OnClickListener() { 21 22 @Override 23 public void onClick(View v) { 24 Intent intent = new Intent(); 25 intent.putExtra("backData", "back data"); 26 setResult(RESULT_CODE, intent); 27 finish(); 28 } 29 }); 30 } 31 32 }
接收传过来的str并且点击button时返回字符串back data给IntentDemoActivity
关于Intent说明http://developer.android.com/reference/android/content/Intent.html
网上找的一些资料:
1 1,掉web浏览器 2 Uri myBlogUri = Uri.parse("http://kuikui.iteye.com"); 3 returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri); 4 2,地图 5 Uri mapUri = Uri.parse("geo:38.899533,-77.036476"); 6 returnIt = new Intent(Intent.ACTION_VIEW, mapUri); 7 3,调拨打电话界面 8 Uri telUri = Uri.parse("tel:100861"); 9 returnIt = new Intent(Intent.ACTION_DIAL, telUri); 10 4,直接拨打电话 11 Uri callUri = Uri.parse("tel:100861"); 12 returnIt = new Intent(Intent.ACTION_CALL, callUri); 13 5,卸载 14 Uri uninstallUri = Uri.fromParts("package", "xxx", null); 15 returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri); 16 6,安装 17 Uri installUri = Uri.fromParts("package", "xxx", null); 18 returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri); 19 7,播放 20 Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3"); 21 returnIt = new Intent(Intent.ACTION_VIEW, playUri); 22 8,掉用发邮件 23 Uri emailUri = Uri.parse("mailto:shenrenkui@gmail.com"); 24 returnIt = new Intent(Intent.ACTION_SENDTO, emailUri); 25 9,发邮件 26 returnIt = new Intent(Intent.ACTION_SEND); 27 String[] tos = { "shenrenkui@gmail.com" }; 28 String[] ccs = { "shenrenkui@gmail.com" }; 29 returnIt.putExtra(Intent.EXTRA_EMAIL, tos); 30 returnIt.putExtra(Intent.EXTRA_CC, ccs); 31 returnIt.putExtra(Intent.EXTRA_TEXT, "body"); 32 returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject"); 33 returnIt.setType("message/rfc882"); 34 Intent.createChooser(returnIt, "Choose Email Client"); 35 10,发短信 36 Uri smsUri = Uri.parse("tel:100861"); 37 returnIt = new Intent(Intent.ACTION_VIEW, smsUri); 38 returnIt.putExtra("sms_body", "shenrenkui"); 39 returnIt.setType("vnd.android-dir/mms-sms"); 40 11,直接发邮件 41 Uri smsToUri = Uri.parse("smsto://100861"); 42 returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri); 43 returnIt.putExtra("sms_body", "shenrenkui"); 44 12,发彩信 45 Uri mmsUri = Uri.parse("content://media/external/images/media/23"); 46 returnIt = new Intent(Intent.ACTION_SEND); 47 returnIt.putExtra("sms_body", "shenrenkui"); 48 returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri); 49 returnIt.setType("image/png"); 50 用获取到的Intent直接调用startActivity(returnIt)就ok了。