在半透明活动中显示警报对话框

时间:2017-02-01 10:36:56

标签: android service alertdialog

所以我有一个服务,我试图显示一个警告对话框。这似乎是不可能的,所以我决定在我的应用程序的半透明活动中显示它。但是,我收到了这个错误:

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) 

这是我的主要活动类:

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();

}

}

我在我的服务中使用融合位置提供程序API,我想确保用户在服务运行之前打开他们的GPS。 我已经在我的清单中有这个:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

不是完全相关的问题,但可以获得答案 什么是Google PlayStore对全天运行定位服务的应用的政策?由于我正在使用FusedLocationProvider API,因此Playstore会拒绝我的应用程序,因为它需要GPS全天运行,无论它是在前台还是在后台,或者即使应用程序被杀死

3 个答案:

答案 0 :(得分:1)

这样做:

  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();

答案 1 :(得分:1)

首先exception试试这个:

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>

将其用作:

<activity android:name=".MainActivity" android:theme="@style/Theme.Transparent">
</activity>

对于第二次查询,我认为这不会在谷歌播放中为你的应用程序造成问题..

但最佳做法是在销毁应用时停止所有后台服务

@Override
protected void onDestroy() {
    super.onDestroy();
    mGoogleApiClient.disconnect();
}

答案 2 :(得分:0)

从日志中

无法启动活动java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)。

尝试Theme.AppCompat主题(或后代)

同样在这种情况下避免使用融合位置而是使用位置管理器

LocationManager manager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);

manager.isProviderEnabled(LocationManager.GPS_PROVIDER);