继续积累吧。
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;
}