1

我正在开发一个安卓应用程序。一些数据正在从 中检索FirebaseDatabase并显示RecyclerView在我的应用程序中。

里面有RecyclerViewcards在每张卡片中,都会显示图像和文字。卡片上有一个“分享”按钮,点击它dynamic-link会生成并与任何人分享。当我点击共享的动态链接时,应用程序会打开,并且会显示与单击共享按钮相同的卡片上的图像,但始终显示最后保存的数据的文本。我希望你明白我的意思。

这是我的生成方式dynamic-link

        Button btnShare = (Button) holder.itemView.findViewById(R.id.btn_share);
        btnShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Uri BASE_URL = Uri.parse("http://www.appwebsite.com/");
                holder.APP_URI = BASE_URL.buildUpon().path(imageUIDh).build();

                packageName = holder.itemView.getContext().getPackageName();

                deepLinkTS = Uri.parse("https://u9p25.app.goo.gl/?link="+holder.APP_URI+"&apn="+packageName+"&amv="+16+"&ad="+0);

                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "Please help this needy: " + deepLinkTS.toString() + "\n-shared through app.");

                holder.itemView.getContext().startActivity(Intent.createChooser(intent, "Share via..."));

            }
        });

这是我dynamic-linkonCreate()方法中的处理方式MainActivity.class

boolean autoLaunchDeepLink = false;
        AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, MainActivity.this, autoLaunchDeepLink)
                .setResultCallback(
                        new ResultCallback<AppInviteInvitationResult>() {
                            @Override
                            public void onResult(@NonNull AppInviteInvitationResult result) {
                                if (result.getStatus().isSuccess()) {
                                    // Extract deep link from Intent
                                    Intent intent = result.getInvitationIntent();
                                    final String deepLink = AppInviteReferral.getDeepLink(intent);

                                    // Handle the deep link. For example, open the linked
                                    // content, or apply promotional credit to the user's
                                    // account.

                                    final ProgressDialog openingSharedRequest = new ProgressDialog(MainActivity.this);
                                    openingSharedRequest.setMessage("Opening shared help-request...");
                                    openingSharedRequest.show();

                                    Handler handler = new Handler();
                                    handler.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            Intent helpRequestIntent = new Intent(MainActivity.this, HelpRequestThroughDeepLink.class);
                                            helpRequestIntent.putExtra("deepLink", deepLink);
//                                            helpRequestIntent.putExtra("deepLinkTS", HelpRequest.deepLinkTS.toString());
//                                    helpRequestIntent.putExtra("APP_URI", HelpRequest.ViewHolder.APP_URI.toString());
                                            helpRequestIntent.putExtra("postedFrom", postedFromS);
//                                    new AlertDialog.Builder(MainActivity.this)
//                                            .setMessage(deepLink)
//                                            .setPositiveButton("OK", null)
//                                            .create()
//                                            .show();
                                            startActivity(helpRequestIntent);
                                            openingSharedRequest.dismiss();
                                        }
                                    }, 1200);

                                    // [START_EXCLUDE]
                                    // Display deep link in the UI
//                                        ((TextView) findViewById(R.id.link_view_receive)).setText(deepLink);
                                    // [END_EXCLUDE]
                                } else {
//                                        Log.d(TAG, "getInvitation: no deep link found.");
//                                    Toast.makeText(getBaseContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
        // [END get_deep_link]

这是来自的代码HelpRequestThroughDeepLink.java

public class HelpRequestThroughDeepLink extends AppCompatActivity {

    String deepLink, deepLinkTS, APP_URI, imageUID, imageUIDFD, hDescription;
    Button btn_accept, btn_reject;
    ImageView hImageHDL;
    TextView imageUIDHDL, hDescriptionHDL;
    DatabaseReference databaseReference1, databaseReferenceUsers;
    MapView mapView;
    ProgressBar progressBar;
    CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_request_through_deep_link);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.myCoordinatorLayout);

        databaseReference1 = FirebaseDatabase.getInstance().getReferenceFromUrl("https://humanehelper-e8a22.firebaseio.com/");
        databaseReferenceUsers = FirebaseDatabase.getInstance().getReferenceFromUrl("https://humanehelper-e8a22.firebaseio.com/users");

        if (getIntent().getExtras() != null) {
            deepLink = getIntent().getExtras().getString("deepLink");
            deepLinkTS = getIntent().getExtras().getString("deepLinkTS");
            APP_URI = getIntent().getExtras().getString("APP_URI");
            postedFrom = getIntent().getExtras().getString("postedFrom");
            String imageUIDD = deepLink.substring(28);
            try {
                imageUID = URLDecoder.decode(imageUIDD, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            if (isNetworkAvailable()) {
                operateDeepLinkRequest();
            } else {
                Snackbar.make(findViewById(R.id.myCoordinatorLayout), "No internet connection!", Snackbar.LENGTH_LONG)
                        .setAction("RETRY", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ifNetworkAvailable();
                            }
                        })
                        .setDuration(Snackbar.LENGTH_INDEFINITE)
                        .show();
            }

        } else {
            Toast.makeText(getBaseContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
        }

        hImageHDL = (ImageView) findViewById(R.id.hImageHDL);
        imageUIDHDL = (TextView) findViewById(R.id.imageUIDHDL);
        hDescriptionHDL = (TextView) findViewById(R.id.homelessDescriptionHDL);

        btn_accept = (Button) findViewById(R.id.btn_accept);
        btn_reject = (Button) findViewById(R.id.btn_reject);
        progressBar = (ProgressBar) findViewById(R.id.progressBar_loading_image);

    }

    public void operateDeepLinkRequest() {
        if (deepLink.contains(imageUID)) {
            if ((imageUID.startsWith("https://firebasestorage.googleapis.com/") || imageUID.startsWith("content://"))) {

                retrieveRespectiveRequest();

            }
        }
    }

    public void retrieveRespectiveRequest() {
        databaseReference1.child("help-requests").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Map<String, String> newRequest = (Map<String, String>) dataSnapshot.getValue();
                imageUIDFD = newRequest.get("imageUIDh");
                hDescription = newRequest.get("hDescription");

                progressBar.setVisibility(View.VISIBLE);

                    imageUIDHDL.setText(imageUID);
                    doSomethingWithPicaso(imageUID, hImageHDL);
                    hDescriptionHDL.setText(hDescription);

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        databaseReference1.child("help-requests").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    private void doSomethingWithPicaso(String imageUIDh, ImageView target){
//        Picasso.with(itemView.getContext()).cancelRequest(homelessImage);
        Picasso.with(getBaseContext())
                .load(imageUIDh)
                .error(R.drawable.ic_warning_black_24dp)
                .into(target, new Callback() {
                    @Override
                    public void onSuccess() {
                        progressBar.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void onError() {
                        Toast.makeText(getBaseContext(), "Error occurred while loading images. Please retry.", Toast.LENGTH_SHORT).show();
                        progressBar.setVisibility(View.INVISIBLE);
                    }
                });
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) HelpRequestThroughDeepLink.this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    public void ifNetworkAvailable(){
        if (isNetworkAvailable()) {
            operateDeepLinkRequest();
        } else {
            Snackbar.make(findViewById(R.id.myCoordinatorLayout), "No internet connection!", Snackbar.LENGTH_LONG)
                    .setAction("RETRY", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ifNetworkAvailable();
                        }
                    })
                    .setDuration(Snackbar.LENGTH_INDEFINITE)
                    .show();
        }
    }

}

请让我知道如何实现这一目标?

4

0 回答 0