意图可以调用活动,也常被用来调用内置应用程序,如加载web页面,拨号页面,内置地图应用等等。下面就用例子来说明该用法。
效果图如下:
实现代码如下:
上图中的启动MyBrowser是用意图来调用MyWebBrowserActivity活动外,其他三个都是在MainActivity类中直接通过new一个对应类型的Intent,然后通过startActivity()方法来调用内置程序。
MainActivity类代码:
package com.example.intents; import com.example.intents.R; import android.R.anim;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity { int request_code = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void onClickWebbrowser(View view){
Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://m.baidu.com/"));
startActivity(i);
}
public void onClickCall(View view){
// Toast.makeText(getApplicationContext(), "dddddddd", Toast.LENGTH_SHORT).show();
// Toast.makeText(getApplicationContext(), "dddddddd", Toast.LENGTH_SHORT).show();
// Toast.makeText(getApplicationContext(), "dddddddd", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(android.content.Intent.ACTION_DIAL);
startActivity(intent);
}
public void onClickMap(View view){
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("geo:37.0,-122.0"));
startActivity(intent);
}
public void onClickLaunchMyBrowser(View view){
// Intent intent = new Intent("com.example.intents.MyBrowserActivity");
Intent intent = new Intent("android.intent.action.VIEW");
// Intent intent = new Intent("wrong");
intent.setData(Uri.parse("http://m.baidu.com"));
//startActivity(intent);
intent.addCategory("com.example.intents.apps");
startActivity(Intent.createChooser(intent, "Open URL using..."));
} }
MyBrowserActivity类代码:
public class MyBrowserActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_browser); Uri url = getIntent().getData();
WebView webView = (WebView)findViewById(R.id.WebBrowser1);
webView.setWebViewClient(new Callback());
webView.loadUrl(url.toString());
}
private class Callback extends WebViewClient{
public boolean shouldOverrideUrlLoading(WebView view,String url){
return false;
}
}