博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android系列4.Intent学习
阅读量:5907 次
发布时间:2019-06-19

本文共 5378 字,大约阅读时间需要 17 分钟。

1.打电话:

1 Intent intent = new Intent();2 intent.setAction(Intent.ACTION_CALL);3 intent.setData(Uri.parse("tel:159xxxxxxxx"));4 startActivity(intent);

必须在AndroidManifest.xml中添加权限:

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中添加权限:

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             @Override13             public void onClick(View v) {14                 // TODO Auto-generated method stub15                 Intent intent = new Intent();16                 intent.setClass(IntentDemoActivity.this, SecondActivity.class);17                 intent.putExtra("str", "hello");18                 //传对象19 //                intent.putExtra("obj", 序列化对象);20                 //启动activity21 //                startActivity(intent);22                 //可以接受返回参数,通过onActivityResult()接收23                 startActivityForResult(intent, REQUEST_CODE);24             }25         });26     }27     @Override28     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             @Override23             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说明

网上找的一些资料:

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了。

转载于:https://www.cnblogs.com/atyou/archive/2012/10/18/2729571.html

你可能感兴趣的文章
AnyProxy 使用备忘
查看>>
JavaScript继承详解(五)
查看>>
POJ 3693 Maximum repetition substring
查看>>
ELF全解析
查看>>
.NET中JSON数据进行序列化和反序列化
查看>>
Linux下的svn命令
查看>>
Windows SP 什么意思? Service Pack什么意思?
查看>>
基于字典的【正向最大减字】分词算法实现
查看>>
员工借款及还款场景演练
查看>>
HBase 性能优化笔记
查看>>
天籁数学——数列篇(3)
查看>>
Java之线程(1) - 传统线程机制的回顾
查看>>
算法代码hdu 1025(最大上升子序列的n*logn解法)
查看>>
PLSQL数据导入
查看>>
自动添加注释—VS2010宏的使用
查看>>
在CI中集成phpmailer,方便使用SMTP发送邮件
查看>>
GMM简单解释
查看>>
如何让ios app支持32位和64位?
查看>>
进制间转换
查看>>
Android的HTTP方式网络通信----HttpClient
查看>>