1. 程式人生 > >自定義URL用scheme喚起Activity

自定義URL用scheme喚起Activity

1.在AndroidManifest.xml中定義shceme:

  

<activity
    android:name="..SchemeActivity"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" 
/> <category android:name="android.intent.category.BROWSABLE" />

        <!--協議定義-->
        <data android:scheme="如http" />
    </intent-filter>
</activity>

2.解析:

public class SchemeActivity extends Activity {

    @Override
    protected void onCreate
(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initIntent(); } private void initIntent() { Uri uri = Uri.parse(getIntent().getDataString());
        //獲取param
        String id = uri.getQueryParameter("id");
        String typeStr = uri.getQueryParameter("type");
Intent intent ; switch (typeStr) { case "0":
                 //處理
                
                break;
             default:
                
                break;
        }
        startActivity(intent);
        finish();
    }
}