gpt4 book ai didi

android - 为什么我不能创建日历?

转载 作者:行者123 更新时间:2023-11-30 01:39:14 24 4
gpt4 key购买 nike

我用了Google SignIn在我的 SignActivity 中,如下所示:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.server_client_id))
.requestEmail()
.requestProfile()
.build();
// [END configure_signin]

// [START build_client]
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
// [END build_client]

SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_WIDE);
signInButton.setScopes(gso.getScopeArray());

现在我可以获得 token 、电子邮件名称和其他信息。

然后我关注了this example 在我的其他 Activity 中为给定的电子邮件创建日历,如下所示:

//for calendar start
static final int REQUEST_ACCOUNT_PICKER = 1000;
static final int REQUEST_AUTHORIZATION = 1001;
static final int REQUEST_GOOGLE_PLAY_SERVICES = 1002;
private static final String PREF_ACCOUNT_NAME = "accountName";
private static final String[] SCOPES = { CalendarScopes.CALENDAR };
GoogleAccountCredential mCredential;
com.google.api.services.calendar.model.Calendar calendar;
//for calendar end

@Override
protected void onCreate(Bundle savedInstanceState) {
...
createBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// I take the email from my sign activity.
String emailName = SignActivity.getPref(PREF_ACCOUNT_NAME, getApplicationContext());
mCredential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff())
.setSelectedAccountName(emailName);
if (isDeviceOnline()) {
new MakeRequestTask(mCredential).execute();
} else {
descriptionEditText.setText("No network connection available.");
}
}
});
...
}

和我的 MakeRequestTask 构造函数:

HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new Calendar.Builder(
transport, jsonFactory, credential)
.setApplicationName("Test")
.build();

而且我的问题出在 try block 第一行的 doInBackground 函数中,它无法执行并执行其他代码,但奇怪的是我没有收到任何错误:

try{
com.google.api.services.calendar.model.Calendar createdCalendar = mService.calendars().insert(calendar).execute();

CalendarListEntry calendarListEntry = new CalendarListEntry();
calendarListEntry.setId(createdCalendar.getId());
calendarListEntry.setBackgroundColor(backGroundColor);
calendarListEntry.setForegroundColor(foreGroundColor);
// Insert the new calendar list entry
CalendarListEntry createdCalendarListEntry = mService.calendarList().insert(calendarListEntry).execute();
Log.i("summary", createdCalendarListEntry.getSummary());
}catch (IOException e){
Log.i("test1", e.getMessage());
}

最佳答案

在我的 createBtn onClick 函数中

我在其他代码的顶部添加了以下代码:

I'm not sure this is the real solution. But in my old codes there were no ask for permission for calendar but now it is asking me to give a permission.

if (mCredential.getSelectedAccountName() == null) {
chooseAccount();
}

现在它正在工作。

关于android - 为什么我不能创建日历?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34723334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com