【问题标题】:How to DeepLink in my application - Android如何在我的应用程序中进行 DeepLink - Android
【发布时间】:2016-10-14 10:52:36
【问题描述】:

我想将这篇文章内容(标题、摘要、图片等...)加载到我的activity发布链接: Link
我可以将此数据加载到activity,对于这项工作,我使用Retrofit作为连接方式并将adapter的数据加载到Recyclerview,当点击RecyclerView项目时,将Bundle的帖子内容发送到其他activity!

活动代码:(使用此代码,我提供来自 recyclerview 项目的数据。)

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();
        for (String key : intent.getExtras().keySet()) {
            Log.e("Deep", key);

        }

        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));

                }
            }
        });

        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();
            }
        });
    }

如果你打开上面的链接,你会在这篇文章中看到另一个链接,(另一个链接到上面的帖子链接LINK),我想要点击其他链接进入帖子。打开此链接进入我的应用程序不是网络浏览器

对于这份工作,我知道我应该使用 Deep Link,我将此代码添加到我的 manifestactivity 中。

<activity
            android:name=".Activities.PostShow_page"
            android:screenOrientation="portrait"
            android:theme="@style/AppThemeWithStatus">

            <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:host="www.kolony.ir"
                    android:scheme="http" />

                <data
                    android:host="kolony.ir"
                    android:scheme="http" />

            </intent-filter>

        </activity>

我想在 PostShow_page 活动中显示帖子内容,当再次点击链接时,将帖子内容打开到 activity

我的 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;

我该怎么办?如何打开链接,进入我的应用程序而不是网络浏览器?

请帮助我亲爱的兄弟们,我真的需要这个。谢谢大家

【问题讨论】:

    标签: android retrofit2 deep-linking android-json gson


    【解决方案1】:

    要使您的应用与网站完美的 DeepLink,您必须遵循一些步骤。我做到了并且成功了。您需要在 manifest.xml 文件中的相应(要打开的活动)块内添加几行。

    1 步:-

        <activity android:name="com.recipe_app.client.RecipeActivity"
                  android:label="@string/app_name" >
            <intent-filter android:label="@string/app_name">
                <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"
                      android:host="recipe-app.com"
                      android:pathPrefix="/recipe" />
            </intent-filter>
        </activity>
    

    2 步:-

    在 .java 文件中添加几行。哪个加载该视图。在 onCreate() 中添加以下代码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe);
        onNewIntent(getIntent());
    
         try {
           Intent intent = getIntent();
           String action = intent.getAction();
           Uri data = intent.getData();
           if (Intent.ACTION_VIEW.equals(action) && data != null) {
               String articleId = data.getLastPathSegment();
               data.getPathSegments();
               String value = data.getPath();
                /* Here you will receive that complete URL from which currently opend URL you come.*/
    
           }
       }catch (Exception e){e.printStackTrace();}
    
    
    }
    

    如需更清晰的细节,您可以访问此 http://search-codelabs.appspot.com/codelabs/android-deep-linking#1 。这对我帮助很大。非常好的文章,并且为初学者很好地解释了。

    如果此答案对您有帮助,请为我的答案投票。谢谢。

    【讨论】:

    • 谢谢我的兄弟,但你能帮我解决我上面的代码吗?因为我是业余爱好者,我真的需要这个。请
    • 欢迎@Jounjle。我浏览了您的代码,这是完全可以理解的。是的,如果应用程序安装在移动设备中,您可以在应用程序中打开您的链接。我认为您必须阅读我在上面分享的那篇文章(search-codelabs.appspot.com/codelabs/android-deep-linking#1)。因为你的答案在那里可用。很好的阐述。
    • 谢谢兄弟@Rana,我读了这个,但我有问题:(你能把你的电子邮件地址发给我吗?拜托
    • 是的@Jounjle 当然是rana@wittyfeed.com。我将在这里 24/7。
    • 谢谢我亲爱的兄弟。我给你发邮件,请回复我。谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 2013-12-16
    • 1970-01-01
    • 2016-06-04
    • 2012-01-26
    • 2020-02-27
    • 1970-01-01
    • 2016-08-26
    相关资源
    最近更新 更多