Activity深入研究

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
• The name of the component to start. • This field of the Intent is a ComponentName object, which you can specify using a fully qualified class name of the target component, including the package name of the app. For example, com.example.ExampleActivity. You can set the component name with setComponent(), setClass(), setClassName(), or with the Intent constructor.
• • // 调用地图 • Uri mapUri = Uri.parse("geo:100,100");
• Intent intent = new Intent(Intent.ACTION 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);
• Intent intent = new Intent(Intent.ACTION_VIEW, uri);
Intent-filter
• // 调用浏览器 • Uri webViewUri = Uri.parse("/zuolongsnail");
• Intent intent = new Intent(Intent.ACTION_VIEW, webViewUri);
• App内以及之间的Activity们可以相互调用
Activity的栈式管理
Task
生 命 周 期
Intent对象
• Activity利用它进行消息的接收和发送
• 其封装了下一步操作的“意图”(实现消息发送者与接收者的藕合解除)
• 发送者利用 :Activity.startActivity(Intent)完成消息发送的操作
Intent中的Data
• The URI (a Uri object) that references the data to be acted on and/or the MIME type of that data. The type of data supplied is generally dictated by the intent's action. For example, if the action is ACTION_EDIT, the data should contain the URI of the document to edit. • To set only the data URI, call setData(). To set only the MIME type, call setType(). If necessary, you can set both explicitly with setDataAndType(). • Caution: If you want to set both the URI and MIME type, do not call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.
法进行取出数据(实际上使用的是内置的Bundle对象)
启动下一个Activity并获取返回结果
• 启动下一个:startActivityForResult(Intent,int requestCode) • Override回调方法:onActivityResult(int reqCode,int resCode,Intent)
• 目标Activity设定结果:setResult(int resCode,Intent); • 目标Activity操作完成:Finish( );
再谈Intent
• 完成了Activity,Service,BroadcastReceiver三大组件的消息传递
Intent中的Component name
• intent.setDataAndType(Uri.fromFile(new File("/sdcard/test.apk"), "application/vnd.android.packagearchive");
• • // 在Android Market中查找应用
• Uri uri = Uri.parse("market://search?q=愤怒的小鸟");
• 接收者利用:Activity.getIntent()方法完成“意图”的接收
• “ 接收者是谁”的信息,在Intent中指定(见下文)
• 数据也封装在Intent中,以Bundle进行了打包处理
• 也可以使用简化的putExtra(key,value)的方法置入数据,使用getXXX(key)的方
• // 发短信 • nmj • // 直接发短信
• "Uri smsToUri = Uri.parse("smsto:10086");
• Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri); • intent.putExtra("sms_body", "the sms text); • // 发彩信
• 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);
• 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中的Category
• A string containing additional information about the kind of component that should handle the intent. Any number of category descriptions can be placed in an intent, but most intents do not require a category. • You can specify a category with addCategory().
• // 调用发邮件(这里要事先配置好的系统Email,否则是调不出发邮件界面的) • Uri emailUri = Uri.parse("mailto:zuolongsnail@"); • Intent intent = new Intent(Intent.ACTION_SENDTO, emailUri); • // 直接发邮件 • Intent intent = new Intent(Intent.ACTION_SEND); • String[] tos = { "zuolongsnail@" }; • String[] ccs = { "zuolongsnail@" }; • 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中的Action
• A string that specifies the generic action to perform (such as view or pick). • You can specify the action for an intent with setAction() or with an Intent constructor.
Activity深入研究
Android App 的四大组件
• Activity • Service • ContentProvider • BroadcastReciever
简介
• 表示App的一个界面,屏幕的一“篇儿”,类似于Web开发的一个网页
• 其生命周期由Android系统来管理
• 每个Activity必须隶属于一个具体的App(每个App有多个Activity)
相关文档
最新文档