通过短信打开手机应用

最近在做毕业设计的时候,要通过短信打开手机上特定的APP,于是搜索了一些资料。通过短信中的链接打开应用最主要的是对manifest文件的配置,比如希望通过短信中的 http://qicaiz.com链接打开特定的APP,要对manifest文件进行以下配置:

<activity
            android:name="com.qicaiz.openapp.MainActivity"
            android:label="@string/app_name" >
           
            <intent-filter >
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <span style="color:#FF0000;"><!-- 在manifest中加入以下intent-filter的内容 --></span>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                 <span style="color:#FF0000;"><data android:scheme="http"
                    android:host="qicaiz.com"/></span>
            </intent-filter>
 </activity>
其中,data属性接受一个Uri对象,Uri总是满足以下格式:

scheme://host:port/path

在manifest文件中为组件声明data属性通过<data.../>元素,其格式如下:

<data 
    android:mimeType=""
    android:scheme=""
    android:host=""
    android:port=""
    android:path=""
    android:pathPrefix=""
    android:pathPattern=""
    />
上面的<data.../>元素支持如下属性:
  • mimeType:用于声明该组件所能匹配的Intent的Type属性
  • scheme:用于声明该组件所能匹配的Intent的Data属性的scheme部分,比如上述中的android:scheme="http"
  • host:用于声明该组件所能匹配的Intent的Data属性的host部分,比如上述中的ndroid:host="qicaiz.com"
  • port:用于声明该组件所能匹配的Intent的Data属性的port部分
  • path:用于声明该组件所能匹配的Intent的Data属性的path部分
  • pathPrefix:用于声明该组件所能匹配的Intent的Data属性的path前缀
  • pathPattern:用于声明该组件所能匹配的Intent的Data属性的path字符串模板

为Intent指定Data属性时,Data属性的Uri对象如前所述,可以分为scheme、host、port、path部分,此时并不要求被启动组件的<data.../>子元素的android:scheme,android:host,android:port,android:path完全满足,像上述例子中只是设置了android:scheme="http",android:host="qicaiz.com"两个元素。<data.../>属性的匹配过程是这样的:

  • 如果目标组件的<data.../>子元素只指定了android:scheme属性,那么只要Intent的Data属性的scheme部分与android:scheme属性值相同,即可启动该组件。
  • 如果目标组件的<data.../>子元素只指定了android:scheme,android:host属性,那么只要Intent的Data属性的scheme、host部分与android:scheme、android:host属性值相同,即可启动该组件。
  • 如果目标组件的<data.../>子元素只指定了android:scheme,android:host,android:port属性,那么只要Intent的Data属性的scheme、host、port部分与android:scheme、android:host、android:port属性值相同,即可启动该组件。

在activity中通过以下语句可以得到短信链接传进来的内容。

public class MainActivity extends Activity
{
	TextView show;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		show = (TextView)findViewById(R.id.show);
		Intent intent = getIntent();
		final Uri data = intent.getData();
		//如果有数据传入应用。这里要判断是否有数据传入,如果没有数据传入而进行数据的读取,程序运行时会报错。
		if(data!=null)
		{
			String scheme = data.getScheme();
			String host = data.getHost();
			//同理,如果短信链接中没有path的内容,而要进行path的读取和显示,程序运行时也会报错。
			List<String> params = data.getPathSegments();
			String first = params.get(0);
			String second = params.get(1);
			show.setText(scheme+host+first+second);
		}
	}
}
下面用图片展示一下上述过程:

首先自己给自己发送一条短信,短信内容是:http://qicaiz.com/firstp/secondp

点击短信中的链接,依次会弹出以下画面

首先会提示是否打开浏览器,点击确定,会出现以下画面

点击“打开应用”(本例工程名为“打开应用”),就是我们想要启动的APP。

现在的问题是,当点击短信中的链接时,要经过两次选择界面才能代开特定的应用,不知道如何设置,使其点击该链接时直接打开特定的应用。我的推测是,UC浏览器和自带的浏览器中,包含android:scheme="http"这个data属性,所以点击链接也能触发浏览器。如果对data属性做一个独一无二的配置,或许在点击短信中的链接时可以直接打开特定的应用。


猜你喜欢

转载自blog.csdn.net/tongxin082/article/details/45332941