Application Crashes every time I run it on my phone. Is there something wrong? It says Unfortunately "appname" has stopped working. I have also tried other approaches to checking for googleplay services but it always crashes. I updated my google play services and have a working google map v2 working perfectly. Any solutions to this code? It crashes on my phone running android 4.1.2 and on my AVD.
package com.example.checkgoogleplayproject; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.view.Menu; import android.widget.TextView; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; public class MainActivity extends Activity { @Override protected void onResume() { super.onResume(); // Getting reference to TextView to show the status TextView tvStatus = (TextView)findViewById(R.id.tv_status); // Getting status int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); // Showing status if(status==ConnectionResult.SUCCESS) tvStatus.setText("Google Play Services are available"); else{ tvStatus.setText("Google Play Services are not available"); int requestCode = 10; Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); dialog.show(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
Restart Your Phone Restart your Android phone either by turning it off and on or using the Restart option. The reason this works is that an app or a service might be messing up in Google Play Services. Simply closing the app wouldn't do much, as it could be a background app or service.
Reset Google Play Services Google Play Services works tirelessly in the background to connect apps, Google services, and Android. So, if the Google Play Services is not working correctly, other apps may not work as intended. You can try resetting Google Play Services to see if that fixes the issue.
To check if GooglePlayServices
available or not, Use GoogleApiAvailability
.isGooglePlayServicesAvailable()
, As GooglePlayServicesUtil
.isGooglePlayServicesAvailable()
deprecated.
public boolean isGooglePlayServicesAvailable(Context context){ GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context); return resultCode == ConnectionResult.SUCCESS; }
Update: Check if google play service available, If google play service is not available and error is recoverable then open a dialog to resolve an error.
public boolean isGooglePlayServicesAvailable(Activity activity) { GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); int status = googleApiAvailability.isGooglePlayServicesAvailable(activity); if(status != ConnectionResult.SUCCESS) { if(googleApiAvailability.isUserResolvableError(status)) { googleApiAvailability.getErrorDialog(activity, status, 2404).show(); } return false; } return true; }
I dont know where to post this but difficulty in correct workflow of google play services checking is mind blowing, I am using some custom classes, but you may get a point...
protected boolean checkPlayServices() { final int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity()); if (resultCode != ConnectionResult.SUCCESS) { if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, activity(), PLAY_SERVICES_RESOLUTION_REQUEST); if (dialog != null) { dialog.show(); dialog.setOnDismissListener(new OnDismissListener() { public void onDismiss(DialogInterface dialog) { if (ConnectionResult.SERVICE_INVALID == resultCode) activity().finish(); } }); return false; } } new CSAlertDialog(this).show("Google Play Services Error", "This device is not supported for required Goole Play Services", "OK", new Call() { public void onCall(Object value) { activity().finish(); } }); return false; } return true; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With