简体   繁体   English

如何在Android上的应用程序中设置DeepLink

[英]How to set DeepLink in my application on Android

I want when click on my website post, open this post (content) inside my application and not open in web browser. 我想点击我的网站帖子,在我的应用程序内打开这篇文章(内容),而不是在网络浏览器中打开。

For example : When click on www.example.com/365 open this post into my application. 例如:点击www.example.com/365将此帖子打开到我的应用程序中。

For this issue i know should DeepLink , and i added this parameters into my application. 对于这个问题,我知道DeepLink ,我将这些参数添加到我的应用程序中。 but i don't know how can set this and sow my post content (title, image, description and more ...) into my application. 但我不知道如何设置这个并将我的帖子内容(标题,图像,描述等...)播放到我的应用程序中。

My API Request : Link 我的API请求: 链接

I added this codes into manifest : 我将这些代码添加到清单中:

<activity android:name=".Activities.PostShow_page">

    <intent-filter android:autoVerify="true">

        <action android:name="android.intent.action.VIEW" />

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

        <data
            android:host="www.example.com"
            android:scheme="http" />

    </intent-filter>

</activity>

And i add this codes in activity : 我在活动中添加此代码:

Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();

Activity codes: 活动代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.post_show_page);

        bindActivity();

        // Initialize
        context = PostShow_page.this;
        favDB = new FavHelper(context);
        postShow_favPost = (ShineButton) mToolbar.findViewById(R.id.post_FavImage);
        post_cover = (ImageView) findViewById(R.id.postShow_cover_image);
        postShow_title = (TextView) findViewById(R.id.postShow_title);
        postShow_title2 = (TextView) findViewById(R.id.postShow_titleBig);
        //postShow_content = (TextView) findViewById(R.id.postShow_content_text);
        postShow_dateTime = (TextView) findViewById(R.id.postShow_man_date_text);
        postShow_author = (TextView) findViewById(R.id.postShow_man_author_text);
        postShow_category = (TextView) findViewById(R.id.postShow_man_category_text);
        title_sliding = (TextView) findViewById(R.id.post_sliding_title);
        comment_Recyclerview = (RecyclerView) findViewById(R.id.comment_recyclerView);
        post_content_web = (WebView) findViewById(R.id.postShow_content_web);
        mLayoutManager = new LinearLayoutManager(this);

        mAdaper = new CommentAdapter2(context, models);

        //Give Data
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            postID = bundle.getInt("postID");
            title = bundle.getString("title");
            image = bundle.getString("image");
            content = bundle.getString("content");
            dateTime = bundle.getString("dateTime");
            author = bundle.getString("author");
            category = bundle.getString("category");
            categoryID = bundle.getString("categoryID");
        }

        // Deep Linking
        Intent intent = getIntent();
        String action = intent.getAction();
        Uri data = intent.getData();

        mAppBarLayout.addOnOffsetChangedListener(this);
        // Setup comment RecyclerView
        comment_Recyclerview.setLayoutManager(mLayoutManager);
        comment_Recyclerview.setHasFixedSize(true);
        postShow_favPost.init(this);
        postShow_favPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (postFavState == 0) {
                    /// Add to Database
                    favDB.insertFAV(context, title, image, content, dateTime, author, category);

                    postFavState++;

                } else {

                    postShow_favPost.setBtnColor(ContextCompat.getColor(context, R.color.favColorON));
                    postShow_favPost.setBtnFillColor(ContextCompat.getColor(context, R.color.favColorON));

                    TastyToast.makeText(context, "برای حذفش به علاقه مندی ها برو", TastyToast.LENGTH_LONG, TastyToast.WARNING);
                }
            }
        });

        if (favDB.checkFavPost(title)) {

            postShow_favPost.setBtnColor(ContextCompat.getColor(context, R.color.favColorON));
            postShow_favPost.setBtnFillColor(ContextCompat.getColor(context, R.color.favColorOFF));

        } else {
            postShow_favPost.setBtnColor(ContextCompat.getColor(context, R.color.favColorOFF));
            postShow_favPost.setBtnFillColor(ContextCompat.getColor(context, R.color.favColorON));
        }


        //mToolbar.inflateMenu(R.menu.post_menu);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.menu_share:

                        break;
                }
                return true;
            }
        });

        startAlphaAnimation(mTitle, 0, View.INVISIBLE);

        // Set Data into views
        if (title != null) {
            postShow_title.setText(title);
            postShow_title2.setText(title);
            title_sliding.setText(getResources().getString(R.string.comment_title) + " " + title);
        }
        loadPostProgressDialog.createAndShow(this);
        if (image != null) {
            Glide.with(this)
                    .load(image)
                    .placeholder(R.drawable.post_image)
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target,
                                                       boolean isFromMemoryCache, boolean isFirstResource) {
                            loadPostProgressDialog.dissmis();
                            return false;
                        }
                    })
                    .into(post_cover);
        }

        if (content != null) {
            //postShow_content.setText(Html.fromHtml(content));

            post_content_web.getSettings().setJavaScriptEnabled(true);

            WebSettings settings = post_content_web.getSettings();
            settings.setDefaultTextEncodingName("utf-8");

            post_content_web.loadData(content, "text/html; charset=utf-8", "utf-8");
        }
        if (dateTime != null) {
            postShow_dateTime.setText(dateTime);
        }
        if (author != null) {
            postShow_author.setText(author);
        }
        if (category != null) {
            postShow_category.setText(category);
        }

        post_cover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                BlurBehind.getInstance().execute(PostShow_page.this, new OnBlurCompleteListener() {
                    @Override
                    public void onBlurComplete() {
                        startActivity(new Intent(PostShow_page.this, DialogImage_page.class)
                                .setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
                                .putExtra("imageCover", image));

                    }
                });
            }
        });
        postShow_category.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (categoryID != null) {
                    startActivity(new Intent(PostShow_page.this, Category_page.class)
                            .putExtra("categoryID", categoryID)
                            .putExtra("categoryTitle", category));
                }
            }
        });
        //Sliding Up
        slideHandleButton = (ImageView) findViewById(R.id.image_sliding);
        slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
        slidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {

            @Override
            public void onDrawerOpened() {
                slideHandleButton.setImageResource(R.drawable.ic_down_arrow_sliding);
                title_sliding.setText(getResources().getString(R.string.comment_title) + " " + title);

                // Load Comment data
                bindData();

            }
        });
        slidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {

            @Override
            public void onDrawerClosed() {
                slideHandleButton.setImageResource(R.drawable.ic_up_arrow_sliding);
                title_sliding.setText(getResources().getString(R.string.comment_title) + " " + title);
            }
        });
    }

    private void bindActivity() {
        mToolbar = (Toolbar) findViewById(R.id.postShow_toolbar);
        mTitle = (TextView) findViewById(R.id.postShow_title);
        mTitleContainer = (LinearLayout) findViewById(R.id.postShow_linearlayout_title);
        mAppBarLayout = (AppBarLayout) findViewById(R.id.postShow_appBar);
    }

    private void bindData() {

        // Setup Connect
        Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
        Call<R_CatModelResponse> call = apiInterface.getCatResponse(postID);

        Log.d("PostID", "Post : " + postID);

        call.enqueue(new Callback<R_CatModelResponse>() {
            @Override
            public void onResponse(Call<R_CatModelResponse> call, Response<R_CatModelResponse> response) {

                if (response != null) {

                    models.addAll(response.body().getCat_posts().get(0).getComments());
                    mAdaper.notifyDataSetChanged();

                    Toast.makeText(PostShow_page.this, "GoTo Adapter", Toast.LENGTH_SHORT).show();
                    comment_Recyclerview.setAdapter(mAdaper);

                }
            }

            @Override
            public void onFailure(Call<R_CatModelResponse> call, Throwable t) {
                Toast.makeText(PostShow_page.this, "Failed", Toast.LENGTH_SHORT).show();
            }
        });
    }

With bundle i give data from adapter (position) , but in deep link i don't know how can i give data from post id (news/365) and set into views ! 使用bundle i从adapter (position)提供数据,但在深层链接中,我不知道如何从post id (news / 365)提供数据并设置为视图!

Now when I click on this link : www.example.com/365 , it open into my application, but it does not show the post contents (title, image, description and more ...). 现在,当我点击此链接时: www.example.com/365 ,它会打开我的应用程序,但它不会显示帖子内容(标题,图片,说明等等)。

POJO class : (with setter and getter) POJO类:(有setter和getter)

public class R_CatModel {

    @SerializedName("id")
    public Integer id;
    @SerializedName("type")
    public String type;
    @SerializedName("slug")
    public String slug;
    @SerializedName("url")
    public String url;
    @SerializedName("status")
    public String status;
    @SerializedName("title")
    public String title;
    @SerializedName("title_plain")
    public String title_plain;
    @SerializedName("content")
    public String content;
    @SerializedName("excerpt")
    public String excerpt;
    @SerializedName("date")
    public String date;
    @SerializedName("modified")
    public String modified;
    @SerializedName("comment_count")
    public int comment_count;
    @SerializedName("comment_status")
    public String comment_status;
    @SerializedName("thumbnail")
    public String thumbnail;
    @SerializedName("thumbnail_images")
    public R_CatThumbnailImages thumbnail_images;
    @SerializedName("categories")
    public List<R_CatCategory> categories;
    @SerializedName("author")
    public R_CatAuthor catAuthor;
    @SerializedName("comments")
    public List<R_PostComment> comments;

How can I do it? 我该怎么做? Please help me. 请帮我。

Thanks in advance. 提前致谢。

You have to use the URI you get from the intent to fetch and parse the content. 您必须使用从intent获取的URI来获取和解析内容。

Uri data = intent.getData();
// Fetch data using URI
//Use the fetched data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM