我正在开发一个应用程序,它有一个带有谷歌+登录按钮的LoginActivity。我已经在Google的API控制台中启用了Google+API,并创建了一个Oauth帐户。
这是我代码的相关部分:
活动类声明:
public class LoginActivity extends Activity implements OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener, PlusClient.OnAccessRevokedListener {
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;
private static final int REQUEST_CODE_SIGN_IN = 2
onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mPlusClient = new PlusClient.Builder(this, this, this)
.setActions("http://schemas.google.com/AddActivity").build();
on点击:
public void onClick(View view) {
Utils.LOGD("onClick - View = " + view);
if (view.getId() == R.id.sign_in_button) {
Utils.LOGD("onClick : mPlusClient status = " + mPlusClient);
if (mPlusClient.isConnected()) {
Utils.LOGD("onClick - mPlusClient is connected");
Toast.makeText(this, "you are already signed in!", Toast.LENGTH_LONG).show();
} else {
Utils.LOGD(" mConnectionResult before trying to connect = " + mConnectionResult);
if (mConnectionResult == null) {
return;
}
try {
mConnectionResult.startResolutionForResult(LoginActivity.this, REQUEST_CODE_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
Utils.LOGD( "Exception caught : ) " + e.getMessage()) ;
mPlusClient.connect();
}
}
onActivityResult:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_SIGN_IN) {
if (resultCode == RESULT_OK && !mPlusClient.isConnected()
&& !mPlusClient.isConnecting()) {
// This time, connect should succeed.
mPlusClient.connect();
Utils.LOGD("onActivityResult : mConnectionResult AFTER trying to connect = " + mConnectionResult);
Utils.LOGD(" onActivityResult result code = " + resultCode);
}
}
}
问题是在我点击登录按钮后,谷歌的内置";正在加载"进度条显示,应用程序突然退出手机主屏幕,设备本身或日志中没有任何错误显示!
此外,当我在应用程序崩溃后返回时,我看到登录已成功完成,onConnected已被调用,并且我使用我的谷歌帐户登录。(登录后我有一个显示我名字的editText)
这种情况几乎每次我点击按钮时都会发生,但并非总是如此。我还注意到,当在模拟器上运行应用程序时,这种崩溃几乎从未发生过。(尽管这种情况发生过几次)
我还添加了日志,以查看onConnected onStop和onDestroy何时被称为
这是从我点击登录到崩溃后的日志:
12-04 17:54:37.464 6516-6516/com.meeba.google D/debug﹕ onClick - View = com.google.android.gms.common.SignInButton@415ea1f8
12-04 17:54:37.464 6516-6516/com.meeba.google D/debug﹕ onClick : mPlusClient status = com.google.android.gms.plus.PlusClient@41570e18
12-04 17:54:37.468 6516-6516/com.meeba.google D/debug﹕ mConnectionResult before trying to connect = ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{4164c2a8: android.os.BinderProxy@41571528}}
12-04 17:54:37.472 195-360/system_process I/ActivityManager﹕ START {cmp=com.google.android.gms/.plus.activity.AccountSignUpActivity (has extras)} from pid -1
12-04 17:54:37.550 6516-6516/com.meeba.google D/debug﹕ onPause
12-04 17:54:37.550 6516-6516/com.meeba.google D/debug﹕ onPause: mPlusClient.isConnected = false
12-04 17:54:37.550 6516-6516/com.meeba.google D/debug﹕ onPause: mPlusClient.isConnecting = false
12-04 17:54:37.589 195-441/system_process I/ActivityManager﹕ START {act=com.google.android.gms.common.account.CHOOSE_ACCOUNT cmp=com.google.android.gms/.common.account.AccountPickerActivity (has extras)} from pid 6544
12-04 17:54:37.789 195-197/system_process D/dalvikvm﹕ GC_CONCURRENT freed 816K, 25% free 13321K/17671K, paused 10ms+11ms
12-04 17:54:37.793 195-227/system_process I/ActivityManager﹕ Displayed com.google.android.gms/.plus.activity.AccountSignUpActivity: +235ms
12-04 17:54:37.937 1199-1210/com.google.android.gsf.login D/dalvikvm﹕ GC_EXPLICIT freed 144K, 7% free 9315K/9927K, paused 1ms+4ms
12-04 17:54:38.187 6544-6546/com.google.android.gms.ui D/dalvikvm﹕ GC_CONCURRENT freed 267K, 4% free 9530K/9927K, paused 2ms+16ms
12-04 17:54:38.211 195-3148/system_process W/InputManagerService﹕ Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@415115c8
12-04 17:54:38.312 631-634/com.google.android.gms D/dalvikvm﹕ GC_CONCURRENT freed 530K, 8% free 10775K/11591K, paused 2ms+15ms
12-04 17:54:38.351 6544-6544/com.google.android.gms.ui D/OpenGLRenderer﹕ Flushing caches (mode 0)
12-04 17:54:38.371 6544-6544/com.google.android.gms.ui D/OpenGLRenderer﹕ Flushing caches (mode 0)
12-04 17:54:38.375 6516-6516/com.meeba.google D/debug﹕ onStop
12-04 17:54:38.375 6516-6516/com.meeba.google D/debug﹕ onDestroy
12-04 17:54:38.418 6516-6516/com.meeba.google D/OpenGLRenderer﹕ Flushing caches (mode 1)
这是返回应用后的日志
12-04 17:59:21.828 195-441/system_process I/ActivityManager﹕ START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.meeba.google/.activities.LoginActivity bnds=[240,405][360,556]} from pid 406
12-04 17:59:21.988 6516-6516/com.meeba.google D/debug﹕ mPlusClient = :com.google.android.gms.plus.PlusClient@416af6d8
12-04 17:59:21.992 6516-6516/com.meeba.google D/debug﹕ onCreate finished
12-04 17:59:21.996 6516-6516/com.meeba.google D/debug﹕ onStart
12-04 17:59:22.003 406-406/com.cyanogenmod.trebuchet D/OpenGLRenderer﹕ Flushing caches (mode 1)
12-04 17:59:22.019 6516-6518/com.meeba.google D/dalvikvm﹕ GC_CONCURRENT freed 406K, 5% free 12419K/12999K, paused 2ms+4ms
12-04 17:59:22.019 6516-6516/com.meeba.google D/debug﹕ onResume
12-04 17:59:22.078 406-406/com.cyanogenmod.trebuchet D/OpenGLRenderer﹕ Flushing caches (mode 0)
12-04 17:59:22.230 195-209/system_process D/dalvikvm﹕ GC_EXPLICIT freed 780K, 25% free 13351K/17671K, paused 3ms+12ms
12-04 17:59:22.355 631-634/com.google.android.gms D/dalvikvm﹕ GC_CONCURRENT freed 524K, 8% free 10777K/11591K, paused 3ms+4ms
12-04 17:59:22.378 195-209/system_process I/Process﹕ Sending signal. PID: 6516 SIG: 3
12-04 17:59:22.378 6516-6520/com.meeba.google I/dalvikvm﹕ threadid=3: reacting to signal 3
12-04 17:59:22.382 6516-6520/com.meeba.google I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
12-04 17:59:22.429 195-227/system_process I/ActivityManager﹕ Displayed com.meeba.google/.activities.LoginActivity: +553ms
12-04 17:59:22.640 6544-6544/com.google.android.gms.ui D/OpenGLRenderer﹕ Flushing caches (mode 1)
12-04 17:59:22.660 631-711/com.google.android.gms I/qtaguid﹕ Failed write_ctrl(u 90) res=-1 errno=22
12-04 17:59:22.683 6544-6546/com.google.android.gms.ui D/dalvikvm﹕ GC_CONCURRENT freed 458K, 7% free 9478K/10119K, paused 1ms+3ms
12-04 17:59:22.683 631-711/com.google.android.gms I/qtaguid﹕ Untagging socket 90 failed errno=-22
12-04 17:59:22.683 631-711/com.google.android.gms W/NetworkManagementSocketTagger﹕ untagSocket(90) failed with errno -22
12-04 17:59:22.707 6516-6516/com.meeba.google D/debug﹕ onConnected started
12-04 17:59:22.707 6516-6516/com.meeba.google D/debug﹕ mPlusClient in onConnected is =com.google.android.gms.plus.PlusClient@416af6d8
调试后,我看到应用程序正好在线路上崩溃mConnectionResult.startResolutionForResult(LoginActivity.this,REQUEST_CODE_SIGN_IN)
怎么了?我该怎么办?
edited:添加manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.meeba.google"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.meeba.google.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.meeba.google.permission.C2D_MESSAGE" />
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:debuggable="true"
android:largeHeap="true"
android:theme="@style/Theme.Sherlock.Light" >
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.meeba.google" />
</intent-filter>
</receiver>
<service
android:name=".GcmIntentService"
android:enabled="true" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".activities.LoginActivity"
android:label="@string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.DashboardActivity" >
<!--intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter-->
</activity>
<activity android:name=".activities.WhereWhenActivity" />
<activity android:name=".activities.ContactsActivity"
android:windowSoftInputMode="stateHidden"/>
<activity android:name=".activities.InvitationActivity" />
<activity android:name=".activities.EventPageActivity" />
</application>
</manifest>
从清单中LoginActivity
的<activity>
定义中删除android:noHistory="true"
。我很确定noHistory
设置会导致Android在启动Google+登录活动时完成LoginActivity
。既然活动已经完成,就没有任何东西可以调用onActivityResult()
了。你的应用程序实际上并没有崩溃,只是完成了。
如果你想确保用户不会返回到你的LoginActivity
,你就必须用另一种方式来解决这个问题(覆盖onBackPressed()
或其他东西)。
android:noHistory="true"的解决方案是覆盖活动B的onBackPressed方法。
private static final int TIME_INTERVAL = 2000;
private long mBackPressed;
@Override
public void onBackPressed() {
if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
// exiting app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
finishAffinity();
}
else {
finish();
System.exit(0);
}
return;
} else {
showSnackBar(R.string.exit_app);
}
mBackPressed = System.currentTimeMillis();
}