2014-11-5 星期三 12:26:30
跨应用程序访问窗口的原理:
说明
本例子是在WebBrowser应用的定义了若干过滤器(Intent Filter),然后在InvokeOtherActivity程序中使用不同的方式设置Intent对象,然后访问WebBrowser应用。
实验截图
第一个按钮“浏览网页:Action”点击之后会出现下面所示的程序选择列表,原因是符合指定Action的程序是这两个,而后面或直接或间接指定应用程序的package name或指定Context对象,这时点击按钮后会直接调用指定的程序显示窗口。
WebBrowserActivity程序源码
// /WebBrowser/src/mobile/android/web/browser/WebBrowserActivity.javapackage mobile.android.web.browser;import android.app.Activity;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.webkit.WebView;public class WebBrowserActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_web_browser);WebView webview = (WebView) findViewById(R.id.webview);//获取URI对象Uri uri = getIntent().getData();if (uri != null){//在WebView控件中显示Uri指向的页面webview.loadUrl(uri.toString());//在窗口标题栏中显示UrisetTitle(uri.toString());}}}
<!-- /WebBrowser/res/layout/activity_web_browser.xml --><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><WebView android:id="@+id/webview"android:layout_width="match_parent"android:layout_height="match_parent" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="mobile.android.web.browser"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><uses-permission android:name="android.permission.INTERNET"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="mobile.android.web.browser.WebBrowserActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><intent-filter><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><data android:scheme="http" /><data android:scheme="https" /></intent-filter></activity></application></manifest>
InvokeOtherActivity程序源码
///InvokeOtherActivity/src/mobile/android/invoke/other/activity/InvokeOtherActivity.javapackage mobile.android.invoke.other.activity;import java.util.List;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.content.pm.ResolveInfo;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.View;public class InvokeOtherActivity extends Activity {// 由于多处需要Context和Class对象,所以创建成员变量private Context mContext;private Class mClass;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_invoke_other);try {// 创建指向WebBrowser程序的Context对象,其中mobile.android.web.browser为其package// namemContext = createPackageContext("mobile.android.web.browser",Context.CONTEXT_INCLUDE_CODE| Context.CONTEXT_IGNORE_SECURITY);// 动态装载类mClass = mContext.getClassLoader().loadClass("mobile.android.web.browser.WebBrowserActivity");} catch (Exception e) {e.printStackTrace();}}// “浏览网页:Action”按钮的单击事件方法,使用Action方式浏览网页public void onClick_Action(View view) {//指定Action和Uri的dataIntent webIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://blog.youkuaiyun.com/nokiaguy"));startActivity(webIntent);}// “浏览网页:SetClassName”按钮的单击事件方法,使用SetClassName方式直接指定package name和类全名public void onClick_SetClassName(View view) {Intent webIntent = new Intent();//直接指定package name和类全名webIntent.setClassName("mobile.android.web.browser","mobile.android.web.browser.WebBrowserActivity");//设置URI的datawebIntent.setData(Uri.parse("http://nokiaguy.blogjava.net"));startActivity(webIntent);}// “浏览网页:SetClassName_Context”按钮的单击事件方法,指定context对象和类全名public void onClick_SetClassName_Context(View view) {if (mContext == null || mClass == null)return;Intent webIntent = new Intent();webIntent.setClassName(mContext,"mobile.android.web.browser.WebBrowserActivity");webIntent.setData(Uri.parse("http://nokiaguy.cnblogs.com"));startActivity(webIntent);}// “浏览网页:SetClass”按钮的单击事件方法,指定context对象和Class对象public void onClick_SetClass(View view) {if (mContext == null || mClass == null)return;Intent webIntent = new Intent();webIntent.setClass(mContext, mClass);webIntent.setData(Uri.parse("http://nokiaguy.cnblogs.com"));startActivity(webIntent);}// “浏览网页:SetComponentName”按钮的单击事件方法,指定ComponentName对象public void onClick_SetComponentName(View view) {if (mContext == null || mClass == null)return;//通过ComponentName对象指定context对象和class对象ComponentName cn = new ComponentName(mContext, mClass);Intent webIntent = new Intent();webIntent.setComponent(cn);webIntent.setData(Uri.parse("http://nokiaguy.cnblogs.com"));startActivity(webIntent);}// “显示计算器”按钮的单击事件方法,通过指定package name和类全名的方式public void onClick_ShowCalculator(View view) {Intent intent = new Intent();intent.setClassName("com.android.calculator2","com.android.calculator2.Calculator");startActivity(intent);}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="onClick_Action"android:text="浏览网页:Action" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="onClick_SetClassName"android:text="浏览网页:setClassName" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="onClick_SetClassName_Context"android:text="浏览网页:setClassName_Context" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="onClick_SetClass"android:text="浏览网页:setClass" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="onClick_SetComponentName"android:text="浏览网页:setComponentName" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="onClick_ShowCalculator"android:text="显示计算器" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="mobile.android.invoke.other.activity"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="mobile.android.invoke.other.activity.InvokeOtherActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
本文介绍如何在Android应用间通过Intent实现跨应用访问网页的功能,包括使用Action、SetClassName、SetClassName_Context、SetClass、SetComponentName等方式,并通过实例代码演示。
550

被折叠的 条评论
为什么被折叠?



