我一直在寻找一段时间并尝试了一些答案,但到目前为止没有任何效果...... 我想通过点击按钮以编程方式创建一个google-calendar-event。 为我的MainActivity获得了以下代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CalManager c = new CalManager(MainActivity.this);
try{c.addEvent();}
catch(IOException e){
Log.d("mychecks", "failed");
}
}
});
}
}
CalManager-class如下所示:
public class CalManager {
Context ctx;
static String dir;
public CalManager(Context c){
this.ctx = c;
dir = ctx.getFilesDir().toString();
}
/** Application name. */
private static final String APPLICATION_NAME =
"test";
/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(
dir , ".credentials/test");
/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY =
JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
/** Global instance of the scopes required by this quickstart.
*
* If modifying these scopes, delete your previously saved credentials
* at ~/.credentials/calendar-java-quickstart
*/
private static final List<String> SCOPES =
Arrays.asList(CalendarScopes.CALENDAR_READONLY);
static {
try {
HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* Creates an authorized Credential object.
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
CalManager.class.getResourceAsStream("clientsecret.json");
if(in==null){
Log.d("mychecks", "fail");
}
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
/**
* Build and return an authorized Calendar client service.
* @return an authorized Calendar client service
* @throws IOException
*/
public static com.google.api.services.calendar.Calendar
getCalendarService() throws IOException {
Credential credential = authorize();
return new com.google.api.services.calendar.Calendar.Builder(
HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
public void addEvent() throws IOException{
com.google.api.services.calendar.Calendar service =
getCalendarService();
Event event = new Event()
.setSummary("TestCalendarEvent")
.setLocation("800 Howard St., San Francisco, CA 94103")
.setDescription("First Test");
DateTime startDateTime = new DateTime("2017-11-07T22:00:00Z");
EventDateTime start = new EventDateTime()
.setDateTime(startDateTime);
event.setStart(start);
DateTime endDateTime = new DateTime("2017-11-07T23:00:00Z");
EventDateTime end = new EventDateTime()
.setDateTime(endDateTime);
event.setEnd(end);
EventReminder[] reminderOverrides = new EventReminder[] {
new EventReminder().setMethod("popup").setMinutes(10),
};
Event.Reminders reminders = new Event.Reminders()
.setUseDefault(false)
.setOverrides(Arrays.asList(reminderOverrides));
event.setReminders(reminders);
String calendarId = "bagcf5aip1o2v7ek6inb5693m8@group.calendar.google.com";
event = service.events().insert(calendarId, event).execute();
}
}
我的Manifest.xml看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.test">
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最后但并非最不重要的是,我有以下gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
defaultConfig {
applicationId "com.example.user.test"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-
core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
compile 'com.google.apis:google-api-services-calendar:v3-rev264-1.23.0'
}
此时,应用程序在到达
时崩溃GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
由于,CalManager的authorize() - 方法中的-statement
CalManager.class.getResourceAsStream("clientsecret.json");
-statement返回null。总是
有关如何解决此问题的任何想法?
这段代码还有其他明显的问题吗?
感谢任何帮助!
修改 getResourceAsStream() - 问题解决了。
下一期是,
private static final java.io.File DATA_STORE_DIR = new java.io.File(
dir , ".credentials/test");
抛出错误:java.io.IOException:无法创建目录:/.credentials / test
那个想法吗?