J'ai donc un service à partir duquel j'essaie d'afficher une boîte de dialogue d'alerte. Cela semble impossible, j'ai donc décidé de le montrer dans l'activité translucide de ma demande. Cependant, je reçois cette erreur:Affichage d'une boîte de dialogue d'alerte dans une activité translucide
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)
Voici ma classe principale d'activité:
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();
}
}
J'utilise l'API de fournisseur de emplacement fusionné à mon service et je veux assurer que l'utilisateur allume son GPS avant que le service ne fonctionne. Je ceci dans ma déjà manifeste: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
ENTIEREMENT PAS QUESTION LIÉES MAIS IL SERAIT Exalté pour obtenir une réponse Quelle est la politique de Google Playstore sur les applications qui exécutent les services de localisation toute la journée? Depuis que je suis en utilisant l'API FusedLocationProvider sera le Playstore rejeter mon application parce que l'exige le GPS en cours d'exécution toute la journée que ce soit au premier plan ou l'arrière-plan ou même si l'application est tué
Pourquoi suggéreriez-vous que j'utilise LocationManager, quel est l'avantage par rapport à FusedLocation. J'utilisais Location Manager avant et je suis passé à l'API Google Play Services. Aussi, puis-je utiliser un thème appcompat avec une activité translucide? J'ai seulement commencé à obtenir cette erreur quand j'ai décidé d'introduire l'alertdialog –