Reputation: 332
So I have a service from which I am trying to show an alert dialog. This seems to be impossible so I decided to show it in the translucent activity of my application. However, I am getting this error:
02-01 10:29:18.336 3806-3806/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.application.sweetiean.dummylocationupdates, PID: 3806
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.sweetiean.dummylocationupdates/com.application.sweetiean.dummylocationupdates.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:355)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:324)
at android.support.v7.app.AppCompatDelegateImplV9.onPostCreate(AppCompatDelegateImplV9.java:172)
at android.support.v7.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:101)
at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1186)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Here is my main activity class:
package com.application.sweetiean.dummylocationupdates;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.ContextThemeWrapper;
import android.view.WindowManager;
import android.widget.Toast;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import static com.application.sweetiean.dummylocationupdates.R.style.dialog;
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Toast.makeText(this, "Background Services starting...", Toast.LENGTH_SHORT).show();
// use this to start and trigger a service
Intent i= new Intent(this, LocationUpdateService.class);
/*this.startService(i);
finish();*/
// potentially add data to the intent
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// check if enabled and if not send user to the GSP settings
if (!enabled) {
showSettingsAlert();
} else {
this.startService(i);
finish();
}
//finish();
}
public void showSettingsAlert() {
//AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getApplication(), dialog));
builder.setTitle("GPS Settings");
builder.setMessage("GPS is not enabled. Do you want to enable it?");
//AlertDialog alert = builder.create();
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
//stopSelf();
}
});
AlertDialog alert = builder.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
}
}
I am using the fused location provider API in my service and I want to ensure that the user turns on their GPS before the service runs.
I have this in my manifest already: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
NOT ENTIRELY RELATED QUESTION BUT IT WOULD BE ELATED TO GET AN ANSWER What is the Google PlayStore's policy on apps that run location services all day? Since I am using the FusedLocationProvider API will the playstore reject my app because the requires the GPS to be running all day whether it is in the foreground or the background or even if the app is killed
Upvotes: 1
Views: 864
Reputation: 23881
For the first exception
try this:
create this theme in your styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Transparent" parent="@style/Theme.AppCompat">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
use this as:
<activity android:name=".MainActivity" android:theme="@style/Theme.Transparent">
</activity>
For the second inquiry i don't think it will create a problem for ur app in google play..
but it is best practice to stop all background services when app is destroyed
@Override
protected void onDestroy() {
super.onDestroy();
mGoogleApiClient.disconnect();
}
Upvotes: 1
Reputation: 763
Do it like this :
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("GPS Settings");
builder.setMessage("GPS is not enabled. Do you want to enable it?");
AlertDialog alertDialog = builder.create();
alertDialog.show();
Upvotes: 1
Reputation: 41
From the log
Unable to start activity java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
try Theme.AppCompat theme (or descendant)
also in this scenario avoid using fused location instead use Location Manager
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Upvotes: 0