GoogleApiClient.ConnectionCallbacks methods not called after connecting to the GoogleApiClient - google-play-services

I have this issue:
GoogleApiClient.ConnectionCallbacks methods not being called after connecting to the GoogleApiClient
But the callbacks have been added but still no onConnection callback and no errors why would that be? Can anyone help?
My code:
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initGoogleApiClient();
}
private void initGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder( this )
.addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
#Override
public void onConnected(Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
protected void onDestroy() {
super.onDestroy();
mGoogleApiClient.disconnect();
}
#Override
public void onConnectionFailed(com.google.android.gms.common.ConnectionResult connectionResult) {
}

I'll answer my own question - again. The answer is to do with setting up the developer console with a debug key:
https://developers.google.com/games/services/console/enabling#a_create_a_linked_application
I can’t find the Android keytool
How to obtain Signing certificate fingerprint (SHA1) for OAuth 2.0 on Android?

It's ok I've done it! Apparently you can't just debug you app from Eclipse you have to install it with an APK otherwise it wont work. As described here:
https://developers.google.com/games/services/android/quickstart
Good luck to anyone else wading through all the fantastic Google documentation.
:-S

Related

Is there any way to not stop service even if app is stopped through the multitasking screen?

My service gets stopped when app is closed.
Code already provided.
My Service code is:
public class MusicService extends Service {
MediaPlayer myPlayer;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
myPlayer = MediaPlayer.create(this, R.raw.nokiatune);
myPlayer.setLooping(false); // Set looping
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
myPlayer.start();
}
#Override
public void onDestroy() {
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
myPlayer.stop();
}
}
I have developed a Service in android. It is working fine. The only thing is that service stops when app is closed through the multitasking screen. Is there any way to not stop the service even if the app is closed through the multitasking screen ?
You could try the approach discussed here. Basically, you register a BroadcastReceiver that restarts your Service if it is destroyed. On your AndroidManifest.xml:
<receiver
android:name="yourpackagename.RestartServiceBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:label="RestartServiceWhenStopped">
</receiver
Your BroadcastReceiver:
public class RestartServiceBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MusicService.class));
}
}
Then on your Service's onDestroy, you send a Broadcast so that the BroadcastReceiver can restart your service.
#Override
public void onDestroy() {
Intent broadcastIntent = new Intent(this,RestartServiceBroadcastReceiver .class);
sendBroadcast(broadcastIntent);
myPlayer.stop();
}
Also, you have to move your logic inside the onStart to your Service's onStartCommand and have it return START_STICKY, like this:
#Override
public int onStartCommand() {
myPlayer.start();
return START_STICKY;
}
Returning this tells Android not to kill your service--however, there's no guarantee that Android will honor this and can still kill your service.
Finally in the Activity where you start the Service, you need to make sure that you check first if the service is already existing before starting the service, and stop the service during onDestroy, so that the BroadcastReceiver can restart the service.
A warning though: this approach will not work above Android O, please see this for more details.

Toast from JobIntentService

I am a beginner.
I want Toast from jobintentservice (onHandleWork) but crash my app.
logcat error is : " can't toast on a thread that has not called looper.prepare()"
i want learn work with handler in jobintentservice
please help me.
public class NotificationService extends JobIntentService {
public static final String TAG = NotificationService.class.getSimpleName();
Handler handler;
public static void enqueuWork(Context context,Intent intent){
enqueueWork(context,NotificationService.class,20,intent);
}
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Start background Service", Toast.LENGTH_SHORT).show();
}
#Override
public int onStartCommand(#Nullable Intent intent, int flags, int startId) {
handler = new Handler();
return super.onStartCommand(intent, flags, startId);
}
#Override
protected void onHandleWork(#NonNull Intent intent) {
handler.post(new Runnable() {
#Override
public void run()
Toast.makeText(NotificationService.this, "onhandlework", Toast.LENGTH_SHORT).show();
}
});
you probebly figured it out by now, but for future readers, the error "can't toast on a thread that has not called looper.prepare()" simply means you must call ui related from ui thread.
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
showToast(activity);
}});
you can find similiar questions here and here

ReactNavice Onesignal Integration Issiue

public class MainApplication extends Application implements ReactApplication
{
private final ReactNativeHost mReactNativeHost=new ReactNativeHost(this){
#Override public boolean getUseDeveloperSupport(){return BuildConfig.DEBUG;}
#Override protected List<ReactPackage>getPackages(){
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativeOneSignalPackage(),
new MapsPackage(),
new VectorIconsPackage());
}
#Override protected String getJSMainModuleName(){return"index";}
};
#Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
#Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
//This part is giving error:
#Override
public List<ReactPackage> createAdditionalReactPackages() {
return getPackages();
}
}
Hi there i have a problem with integration onesignal push notification into myapp.
As you see i added necessary codes as documanted. But onesignal is not working and if i add createAdditionalReactPackages it gives error and not building. If i remove that part project building succesfully but onIds not working.
What is wrong i dont understand please help me
Thanks in advance.

How to write Background Service in Android 6.0 and above?

I have created a background service and that service is working fine in below Android 6.0 when I kill app.
Also working in Android 6.0 and above but only when I minimize app.
When I kill app in Android 6.0 and above service also kill and not get restart and also not get start on BOOT_COMPLETE.
What to DO?
Can I get a simple example.
I tried this:
MyService.java
public class MyService extends Service {
private MediaPlayer player;
#Nullable
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet started");
}
public A_ExampleMyService() {
super();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
player.setLooping(true);
player.start();
return START_STICKY;
}
#Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
startService(restartServiceIntent);
super.onTaskRemoved(rootIntent);
}
}
MyActivity.java
public class MyActivity extends Activity {
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_example_activity);
this.context = this;
Intent background = new Intent(MyActivity.this,MyService.class);
context.startService(background);
}
}
manifest.xml
<manifest>
<application>
<service>
<android:name=".MyService">
<android:enabled="true">
<android:exported="true"/>
</application>
</manifest>

Google+ Sign-In for users without a Google+ profile

According to the official announcement, Google+ Sign-In now supports users without a Google+ profile.
However, when I visit one the official examples from an account without a Google+ profile, it only gives me the option of signing up for Google+.
What's the way to implement this feature? I've been checking the official docs as well but I can't find any references to this topic.
Let's make this snippet work shall we?
package com.example.googleplusauth;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.plus.Plus;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
public class MainActivity extends Activity implements ConnectionCallbacks,
OnConnectionFailedListener {
/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 0;
/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;
/*
* A flag indicating that a PendingIntent is in progress and prevents us
* from starting further intents.
*/
private boolean mIntentInProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API, null)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.build();
}
#Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
#Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
#Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
#Override
public void onConnectionFailed(ConnectionResult result) {
if (!mIntentInProgress && result.hasResolution()) {
try {
mIntentInProgress = true;
startIntentSenderForResult(
//result.getIntentSender(),
result.getResolution().getIntentSender(),
RC_SIGN_IN, null, 0, 0, 0);
} catch (SendIntentException e) {
// The intent was canceled before it was sent. Return to the
// default
// state and attempt to connect to get an updated
// ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
#Override
public void onConnected(Bundle connectionHint) {
// We've resolved any connection errors. mGoogleApiClient can be used to
// access Google APIs on behalf of the user.
}
#Override
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
}