Scheme协议在Android中使用场景
- H5跳转到native页面
- 客户端获取push消息中后,点击消息跳转到APP内部页面
- APP根据URL跳转到另外一个APP指定页面
利用scheme协议跳转的一个完整实例
- 在Mainefest配置文件中配置需要用scheme协议跳转的Activity
<activity android:name=".SchemeActivity" android:label="@string/app_name"> <!-- 要想在别的App上能成功调起App,必须添加intent过滤器 --> <!-- 协议部分,随便设置 --> <intent-filter> <!--协议部分,随便设置--> <data android:scheme="scheme" android:host="mtime" android:path="/goodsDetail" /> <!--下面这几行也必须得设置--> <category android:name="android.intent.category.DEFAULT"/> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.BROWSABLE"/> </intent-filter> </activity>
- 模拟从网络中获取scheme协议的url
public class MainActivity extends AppCompatActivity { private TextView btnSchemeTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSchemeTv = (TextView) findViewById(R.id.btn_scheme_tv); btnSchemeTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { /** * (1)在manifest配置文件中配置了scheme参数 * (2)网络端获取url * (3)跳转 */ String url = "scheme://mtime/goodsDetail?goodsId=10011002"; Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); } }); } }
- 获取scheme协议参数
public class SchemeActivity extends Activity { private static final String TAG = "SchemeActivity"; private TextView schemeTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scheme); schemeTv = (TextView) findViewById(R.id.scheme_tv); Uri data = getIntent().getData(); Log.i(TAG, "host = " + data.getHost() + " path = " + data.getPath() + " query = " + data.getQuery()); String param = data.getQueryParameter("goodsId"); schemeTv.setText("获取的参数为:" + param); } }
参考文献