我的服务代码 - 它适用于少数设备和崩溃(代码7 NW错误在其他设备上) 测试Kitkat 4.4.2 HTC - 跑 5.1 - 魅族 - 奔跑 5.1 - Moto g -ran 4.4 - xiaomi - 失败 4.1 - 索尼 - 失败 我已经浪费了很多夜,有人能解释这里发生了什么吗?
package gaurav.birthdays;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.plus.People;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.model.people.PersonBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by gaurav on 24/7/16.
*/
public class GooglePlusService extends IntentService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<People.LoadPeopleResult> {
GoogleApiClient mGoogleApiClient;
ArrayList<String> listID;
int tries = 0;
public GooglePlusService() {
super("GooglePlusService");
}
@Override
protected void onHandleIntent(Intent intent) {
String ID = (String) intent.getStringExtra("Id");
String[] userId = ID.split(",");
listID = new ArrayList<>(Arrays.asList(userId));
System.out.println(listID);
}
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if( mGoogleApiClient.isConnected())
{
System.out.println("connecting");
}
if (mGoogleApiClient.isConnected())
{
System.out.println("connected");
}
try {
Plus.PeopleApi.load(mGoogleApiClient,listID).setResultCallback(this);
}
catch (Exception ex)
{
Toast.makeText(GooglePlusService.this, "No Contacts in Google plus", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
mGoogleApiClient.connect();
}
@Override
public void onResult(@NonNull People.LoadPeopleResult peopleData) {
System.out.println(peopleData.getStatus().getStatusCode());
DBHandler dbHandler = new DBHandler(getApplicationContext());
if (peopleData.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
PersonBuffer personBuffer = peopleData.getPersonBuffer();
try {
int count = personBuffer.getCount();
for (int i = 0; i < count; i++) {
String bday = personBuffer.get(i).getBirthday();
if (bday != null) {
bday = procBday(bday);
String name = personBuffer.get(i).getDisplayName();
String msg = "Happy Birthday!";
if (!dbHandler.exists(name, bday)) {
dbHandler.insertData(msg, name, bday, "");
}
}
}
} finally {
Toast.makeText(GooglePlusService.this, "Import Completed", Toast.LENGTH_SHORT).show();
dbHandler.close();
personBuffer.release();
}
} else {
if(tries<2) {
mGoogleApiClient.connect();
Plus.PeopleApi.load(mGoogleApiClient, listID).setResultCallback(this);
tries++;
}
}
}
String procBday(String inp) {
System.out.println(inp);
String[] parts = inp.split("-");
String bday = parts[2] + "/" + parts[1];
return bday;
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("BYE");
}
}
Gradle -APP
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "gaurav.birthdays"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
signingConfigs {
debug {
storeFile file(project.property("MyApp.signing"))
storePassword project.property("MyApp.signing.password")
keyAlias project.property("MyApp.signing.alias")
keyPassword project.property("MyApp.signing.password")
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.google.android.gms:play-services-plus:9.2.1'
}
apply plugin: 'com.google.gms.google-services'