java - Connect to google api -
i have prograammed couple of years now, never made android app before, therefore build.gradle , manifest new me.
the app trying make app tests kids on multiplication table. app worked perfectly, tried add function log google , use achivements in google play, can't seem connect google api.
i have tried follow tutorial: tutorial, no luck. connectionresult{statuscode=sign_in_failed, resolution=null} whenever try connect. here manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.nilsen.anders.multiplication" android:versioncode="2" android:versionname="1.0.1"> <uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.get_accounts"/> <uses-permission android:name="android.permission.use_credentials"/> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <meta-data android:name="com.google.android.gms.games.app_id" android:value="@string/app_name" /> <activity android:name="com.example.andste569.multiplication.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.example.andste569.multiplication.mainactivitymultipl" android:label="@string/title_activity_main_activity_multipl" android:screenorientation="portrait" android:configchanges="orientation|keyboardhidden"> </activity> </application>
and here gradle.build. not know sure if minyfy should enabled or not, i've tried both. buildscript copied example, not know does.
apply plugin: 'com.android.application' android { compilesdkversion 21 buildtoolsversion "21.1.2" defaultconfig { applicationid "com.nilsen.anders.oblig_4" minsdkversion 9 targetsdkversion 21 versioncode 2 versionname "1.0.1" } buildtypes { release { minifyenabled true proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile 'com.android.support:appcompat-v7:22.1.0' compile "com.android.support:support-v4:22.1.0" compile 'com.google.android.gms:play-services:7.0.0' compile filetree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.0' } buildscript { repositories { mavencentral() } }
i belive problem in manifest or gradle, , not in activity_main, because followed tutorial quite precisely, post code well.
package com.example.andste569.multiplication; import android.app.dialog; import android.content.dialoginterface; import android.content.intent; import android.content.intentsender; import android.support.v4.app.dialogfragment; import android.support.v4.app.fragmentactivity; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.edittext; import android.widget.seekbar; import android.widget.textview; import android.widget.toast; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.errordialogfragment; import com.google.android.gms.common.googleplayservicesutil; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.drive.drive; import com.nilsen.anders.multiplication.r; import com.google.android.gms.common.api.googleapiclient.connectioncallbacks; import com.google.android.gms.common.api.googleapiclient.onconnectionfailedlistener; public class mainactivity extends fragmentactivity implements connectioncallbacks, onconnectionfailedlistener{ // request code use when launching resolution activity private static final int request_resolve_error = 1001; // unique tag error dialog fragment private static final string dialog_error = "dialog_error"; // bool track whether app resolving error private boolean mresolvingerror = false; private static final string state_resolving_error = "resolving_error"; private googleapiclient mgoogleapiclient; public final static string extra_message = "com.example.andste569.oblig_4.message"; public final static string extra_multiplier = "com.example.andste569.oblig_4.multiplier"; public final static int minmultiplier = 4; public final static int maxmultiplier = 12; public final static int sndrmultiplier =10; private seekbar seekbar; private int progress; private final string logtag ="debug_mul"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); log.wtf(logtag,integer.tostring(checkplayservices())); // create googleapiclient instance mgoogleapiclient = new googleapiclient.builder(this) .addapi(drive.api) .addscope(drive.scope_file) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .build(); //setup seekbar. seekbar = (seekbar)findviewbyid(r.id.seekbar); seekbar.setmax(maxmultiplier-minmultiplier); seekbar.setprogress(sndrmultiplier-minmultiplier); final textview textviewseekvalue = (textview)findviewbyid(r.id.textviewseekvalue); final textview textviewseekmin = (textview)findviewbyid(r.id.textviewseekmin); final textview textviewseekmax = (textview)findviewbyid(r.id.textviewseekmax); progress=sndrmultiplier; // initialize textviews. textviewseekvalue.settext(integer.tostring(seekbar.getprogress()+minmultiplier)); textviewseekmin.settext(integer.tostring(minmultiplier)); textviewseekmax.settext(integer.tostring(maxmultiplier)); seekbar.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() { @override public void onprogresschanged(seekbar seekbar, int progressvalue, boolean fromuser) { progress = progressvalue+minmultiplier; textviewseekvalue.settext(integer.tostring(progress)); //toast.maketext(getapplicationcontext(), "changing seekbar's progress", toast.length_short).show(); } @override public void onstarttrackingtouch(seekbar seekbar) { //toast.maketext(getapplicationcontext(), "started tracking seekbar", toast.length_short).show(); } @override public void onstoptrackingtouch(seekbar seekbar) { //toast.maketext(getapplicationcontext(), "stopped tracking seekbar", toast.length_short).show(); } }); mresolvingerror = savedinstancestate != null && savedinstancestate.getboolean(state_resolving_error, false); } //todo: handle connection failures @override public void onconnected(bundle connectionhint) { // connected google play services! // stuff goes here. log.wtf(logtag,"onconnected"); } @override public void onconnectionsuspended(int cause) { // connection has been interrupted. // disable ui components depend on google apis // until onconnected() called. log.wtf(logtag,"onconnectionsuspended"); } @override public void onconnectionfailed(connectionresult result) { // callback important handling errors // may occur while attempting connect google. // // more in next section. log.wtf(logtag,"onconnectionfailed"); if (mresolvingerror) { // attempting resolve error. return; } else if (result.hasresolution()) { try { mresolvingerror = true; result.startresolutionforresult(this, request_resolve_error); } catch (intentsender.sendintentexception e) { // there error resolution intent. try again. mgoogleapiclient.connect(); } } else { // show dialog using googleplayservicesutil.geterrordialog() showerrordialog(result.geterrorcode()); mresolvingerror = true; log.wtf(logtag,result.tostring()); } } /* creates dialog error message */ private void showerrordialog(int errorcode) { // create fragment error dialog errordialogfragment dialogfragment = new errordialogfragment(); // pass error should displayed bundle args = new bundle(); args.putint(dialog_error, errorcode); dialogfragment.setarguments(args); dialogfragment.show(getsupportfragmentmanager(), "errordialog"); } private int checkplayservices() { int resultcode = googleplayservicesutil.isgoogleplayservicesavailable(this); return resultcode; } /* called errordialogfragment when dialog dismissed. */ public void ondialogdismissed() { mresolvingerror = false; } /* fragment display error dialog */ public static class errordialogfragment extends dialogfragment { public errordialogfragment() { } @override public dialog oncreatedialog(bundle savedinstancestate) { // error code , retrieve appropriate dialog int errorcode = this.getarguments().getint(dialog_error); return googleplayservicesutil.geterrordialog(errorcode, this.getactivity(), request_resolve_error); } @override public void ondismiss(dialoginterface dialog) { ((mainactivity)getactivity()).ondialogdismissed(); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == request_resolve_error) { mresolvingerror = false; if (resultcode == result_ok) { // make sure app not connected or attempting connect if (!mgoogleapiclient.isconnecting() && !mgoogleapiclient.isconnected()) { mgoogleapiclient.connect(); } } } } @override protected void onstart() { super.onstart(); if (!mresolvingerror) { // more later mgoogleapiclient.connect(); log.wtf(logtag,"onstart"); } } @override protected void onstop() { mgoogleapiclient.disconnect(); super.onstop(); } @override public boolean oncreateoptionsmenu(menu menu) { log.i("debug","oncreateoptionmenu"); // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putboolean(state_resolving_error, mresolvingerror); } public void multiplicationbutton(view view){ multiplicationwindow("mul"); } public void divisionbutton (view view){ multiplicationwindow("div"); } private void multiplicationwindow(string operation){ log.wtf("debug","opening window"); intent intent = new intent(this,mainactivitymultipl.class); edittext edittextname =(edittext) findviewbyid(r.id.edittext_name); string name = edittextname.gettext().tostring(); intent.putextra(extra_message,name); intent.putextra(extra_multiplier,progress); intent.setaction(operation); startactivity(intent); log.wtf("debug","opening window2"); } public void onstoptrackingtouch (seekbar seekbar){ } public void signin (view view){ log.wtf(logtag,"connectingbuttonstart"); mgoogleapiclient.connect(); log.wtf(logtag,"connectingbuttonend"); } }