我尝试通过邮件和短信邀请应用程序并成功地做到了。但我需要获取邀请朋友历史的推荐历史,即被推荐的朋友姓名和邮件ID。我通过谷歌搜索并尝试了只有机会获得邀请ID。是否有任何其他选项可以使用谷歌 API 应用邀请来获取被推荐的朋友历史详细信息。我在这里提交了代码。如下请见。谢谢。
主要活动
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.appinvite.AppInvite;
import com.google.android.gms.appinvite.AppInviteInvitation;
import com.google.android.gms.appinvite.AppInviteInvitationResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
/**
* Main Activity for sending App Invites and launchings the DeepLinkActivity when an
* App Invite is received.
*/
public class MainActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private static final int REQUEST_INVITE = 0;
private GoogleApiClient mGoogleApiClient;
// [START on_create]
@Override
protected void onCreate(Bundle savedInstanceState) {
// [START_EXCLUDE]
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// No savedInstanceState, so it is the first launch of this activity
// Invite button click listener
findViewById(R.id.invite_button).setOnClickListener(this);
findViewById(R.id.custom_invite_button).setOnClickListener(this);
// [END_EXCLUDE]
// Create an auto-managed GoogleApiClient with acccess to App Invites.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(AppInvite.API)
.enableAutoManage(this, this)
.build();
// Check for App Invite invitations and launch deep-link activity if possible.
// Requires that an Activity is registered in AndroidManifest.xml to handle
// deep-link URLs.
boolean autoLaunchDeepLink = true;
AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
.setResultCallback(
new ResultCallback<AppInviteInvitationResult>() {
@Override
public void onResult(AppInviteInvitationResult result) {
Log.d(TAG, "getInvitation:onResult:" + result.getStatus());
Log.e(TAG, "Redult:---->" + result.toString());
// Because autoLaunchDeepLink = true we don't have to do anything
// here, but we could set that to false and manually choose
// an Activity to launch to handle the deep link here.
}
});
}
/* [END on_create] */
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed:" + connectionResult);
showMessage(getString(R.string.google_play_services_error));
}
/**
* User has clicked the 'Invite' button, launch the invitation UI with the proper
* title, message, and deep link
*/
// [START on_invite_clicked]
private void onInviteClicked() {
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
.setMessage(getString(R.string.invitation_message))
.setDeepLink(Uri.parse(getString(R.string.invitation_deep_link)))
.setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
.setCallToActionText(getString(R.string.invitation_cta))
.build();
startActivityForResult(intent, REQUEST_INVITE);
}
// [END on_invite_clicked]
/**
* User has clicked the 'Custom Invite' button, launch the invitation UI but pass in
* a custom HTML body and subject for email invites.
*/
// [START on_custom_invite_clicked]
private void onCustomInviteClicked() {
// When using the setEmailHtmlContent method, you must also set a subject using the
// setEmailSubject message and you may not use either setCustomImage or setCallToActionText
// in conjunction with the setEmailHtmlContent method.
//
// The "%%APPINVITE_LINK_PLACEHOLDER%%" token is replaced by the invitation server
// with the custom invitation deep link based on the other parameters you provide.
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
.setMessage(getString(R.string.invitation_message))
.setDeepLink(Uri.parse(getString(R.string.invitation_deep_link)))
.setEmailHtmlContent("<html><body>" +
"<h1>App Invites</h1>" +
"<a href=\"%%APPINVITE_LINK_PLACEHOLDER%%\">Install Now!</a>" +
"<body></html>")
.setEmailSubject(getString(R.string.invitation_subject))
.build();
startActivityForResult(intent, REQUEST_INVITE);
/* Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + "9786228072"));
intent.putExtra("sms_body", "install");*/
// startActivity(intent);
}
// [END on_custom_invite_clicked]
// [START on_activity_result]
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);
Log.e("onActivityResult", "onActivityResult" + requestCode + " : " + resultCode);
if (requestCode == REQUEST_INVITE) {
if (resultCode == RESULT_OK) {
// Check how many invitations were sent and log a message
// The ids array contains the unique invitation ids for each invitation sent
// (one for each contact select by the user). You can use these for analytics
// as the ID will be consistent on the sending and receiving devices.
String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
Log.d(TAG, getString(R.string.sent_invitations_fmt, ids.length));
Log.e("sent_invitations_fmt","sent_invitations_fmt"+ ids.length);
} else {
// Sending failed or it was canceled, show failure message to the user
showMessage(getString(R.string.send_failed));
}
}
}
// [END on_activity_result]
private void showMessage(String msg) {
ViewGroup container = (ViewGroup) findViewById(R.id.snackbar_layout);
Snackbar.make(container, msg, Snackbar.LENGTH_SHORT).show();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.invite_button:
onInviteClicked();
break;
case R.id.custom_invite_button:
onCustomInviteClicked();
break;
}
}
}
深度链接活动:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.appinvite.AppInviteReferral;
/**
* Activity for displaying information about a receive App Invite invitation. This activity
* displays as a Dialog over the MainActivity and does not cover the full screen.
*/
public class DeepLinkActivity extends AppCompatActivity implements
View.OnClickListener {
private static final String TAG = DeepLinkActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deep_link_activity);
Log.e("deepclass", "deepclass: ");
// Button click listener
findViewById(R.id.button_ok).setOnClickListener(this);
}
// [START deep_link_on_start]
@Override
protected void onStart() {
super.onStart();
// Check if the intent contains an AppInvite and then process the referral information.
Intent intent = getIntent();
if (AppInviteReferral.hasReferral(intent)) {
processReferralIntent(intent);
}
}
// [END deep_link_on_start]
// [START process_referral_intent]
private void processReferralIntent(Intent intent) {
// Extract referral information from the intent
String invitationId = AppInviteReferral.getInvitationId(intent);
String deepLink = AppInviteReferral.getDeepLink(intent);
if (AppInviteReferral.isOpenedFromPlayStore(intent)) {
Log.e("invite","invite");
}else{
Log.e("not invite","not invite");
}
addReferralDataToIntent(invitationId, deepLink, intent);
// Display referral information
// [START_EXCLUDE]
Log.d(TAG, "Found Referral: " + invitationId + ":" + deepLink);
Log.e("deepLink", "deepLink: " + invitationId + ":" + deepLink);
((TextView) findViewById(R.id.deep_link_text))
.setText(getString(R.string.deep_link_fmt, deepLink));
((TextView) findViewById(R.id.invitation_id_text))
.setText(getString(R.string.invitation_id_fmt, invitationId));
// [END_EXCLUDE]
}
public static Intent addReferralDataToIntent (String invitationId, String deepLink, Intent referralIntent){
Log.e("deepLink", "deepLink: " + deepLink);
Log.e("invitationId", "invitationId: " + invitationId);
return referralIntent;``
}
// [END process_referral_intent]
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_ok:
finish();
break;
}
}
}