安卓开发日记---2017.10.23
来源:互联网 发布:阳江网络问政平台官网 编辑:程序博客网 时间:2024/06/06 01:13
继续积累吧。
Intent
这周我主要学习了Android中的Intent
模块,做了一些小的练习,熟悉了这个模块的使用方法。从直观上理解,Intent
可以看作一种消息机制,从一个Activity
到另一个Activity
,或者从一个app到另一个app。
创建一个新的Activity
在Android中,我们需要从一个Activity中,创建另一个Activity,来实现UI的切换。实现之一目标的方法是startActivity
方法。在startActivity
方法中,我们需要一个Intent
类型的参数,这个参数可以携带一些key-value数据,并将其传入到新创建的Activity中。
String textEntered = mNameEntry.getText().toString(); /* * Storing the Context in a variable in this case is redundant since we could have * just used "this" or "MainActivity.this" in the method call below. However, we * wanted to demonstrate what parameter we were using "MainActivity.this" for as * clear as possible. */ Context context = MainActivity.this; /* This is the class that we want to start (and open) when the button is clicked. */ Class destinationActivity = ChildActivity.class; /* * Here, we create the Intent that will start the Activity we specified above in * the destinationActivity variable. The constructor for an Intent also requires a * context, which we stored in the variable named "context". */ Intent startChildActivityIntent = new Intent(context, destinationActivity); // COMPLETED (2) Use the putExtra method to put the String from the EditText in the Intent /* * We use the putExtra method of the Intent class to pass some extra stuff to the * Activity that we are starting. Generally, this data is quite simple, such as * a String or a number. However, there are ways to pass more complex objects. */ startChildActivityIntent.putExtra(Intent.EXTRA_TEXT, textEntered); /* * Once the Intent has been created, we can use Activity's method, "startActivity" * to start the ChildActivity. */ startActivity(startChildActivityIntent);
隐式Intent
隐式Intent的主要目的是使用Android系统中其他App提供的公共服务,例如:浏览器,地图等工具。这些隐式Intent的信息可以在Android的官方开发文档中找到。
打开一个网页
private void openWebPage(String url) { /* * We wanted to demonstrate the Uri.parse method because its usage occurs frequently. You * could have just as easily passed in a Uri as the parameter of this method. */ Uri webpage = Uri.parse(url); /* * Here, we create the Intent with the action of ACTION_VIEW. This action allows the user * to view particular content. In this case, our webpage URL. */ Intent intent = new Intent(Intent.ACTION_VIEW, webpage); /* * This is a check we perform with every implicit Intent that we launch. In some cases, * the device where this code is running might not have an Activity to perform the action * with the data we've specified. Without this check, in those cases your app would crash. */ if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
打开地图,并进行定位
public void onClickOpenAddressButton(View v) { String addressString = "1600 Amphitheatre Parkway, CA"; Uri.Builder builder = new Uri.Builder(); builder.scheme("geo") .path("0,0") .query(addressString); Uri addressUri = builder.build(); showMap(addressUri); } private void showMap(Uri geoLocation) { /* * Again, we create an Intent with the action, ACTION_VIEW because we want to VIEW the * contents of this Uri. */ Intent intent = new Intent(Intent.ACTION_VIEW); /* * Using setData to set the Uri of this Intent has the exact same affect as passing it in * the Intent's constructor. This is simply an alternate way of doing this. */ intent.setData(geoLocation); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
共享消息
在Android中,我们会将一些内容进行共享,例如:文本,音频,视频等。这时候我们需要ShareCompat
创建一个共享的Intent,并提供给可以处理的App。例如:将文本信息交给剪贴板。
void shareTest(String textToShare){ String mineType = "text/plain"; String title = "Learning how to share"; ShareCompat.IntentBuilder.from(this) .setType(mineType) .setChooserTitle(title) .setText(textToShare) .startChooser(); }
在这里,如果我们将一个Intent绑定到一个MenuItem
中,可以使用setIntent
方法。在点击这个MenuItem
之后,就会自动使用这个共享Intent分享到其他的应用中。
private Intent createShareForecastIntent() { Intent shareIntent = ShareCompat.IntentBuilder.from(this) .setType("text/plain") .setText(mForecast + FORECAST_SHARE_HASHTAG) .getIntent(); return shareIntent; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.detail, menu); MenuItem menuItem = menu.findItem(R.id.action_share); menuItem.setIntent(createShareForecastIntent()); return true; }
阅读全文
0 0
- 安卓开发日记---2017.10.23
- 安卓开发日记--2017.10.5
- 安卓开发日记--2017.10.10
- 安卓开发日记--2017.10.17
- 安卓开发日记--2017.10.31
- 【安卓开发日记】前言
- 7.17安卓日记
- 安卓程序-日记
- 安卓开发日记1——虫虫新闻
- 安卓开发日记2——思念计数器
- 安卓开发日记3——IP推送器
- 安卓开发日记1——基本环境搭建
- 安卓开发日记2——eclipse配置
- 安卓编程日记【一】开发环境配置篇
- 安卓学习日记20160328
- 安卓开发日记(1) - 安装 Android 开发环境和 first app
- 安卓日记——超简单的开发框架afinal
- 移动端开发踩坑日记——安卓4.4.2不触发touchend事件
- 在阿里AI实验室做NLP高级算法工程师是一种什么样的体验?
- Java高并发秒杀API之DAO层实现(一)
- L1-001. Hello World
- jQuery ajax数据请求
- 欢迎使用CSDN-markdown编辑器
- 安卓开发日记---2017.10.23
- 17.10.23日报
- IOS 申请相机权限崩溃
- 远离中国式辩论
- JQuery ajax标准格式写法
- java 解析 xml 笔记
- Delphi版 环形无锁缓冲(二)
- 快速字符串模糊匹配--基于Horspool的模糊匹配算法
- 数学家破解婚恋网站配对程序,90天找到灵魂伴侣