简体   繁体   English

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

[英]Showing an alert dialog in a translucent activity

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. 我在服务中使用了融合的位置提供程序API,我想确保用户在服务运行之前打开其GPS。 I have this in my manifest already: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 我的清单中已经有这个: <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? 并非完全相关的问题,但它会得到答案 。Google PlayStore对全天运行位置服务的应用有何政策? 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 由于我正在使用FusedLocationProvider API,因此Playstore会拒绝我的应用程序,因为这要求GPS整天运行,无论是在前台还是在后台,甚至是应用程序被杀死

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

For the first exception try this: 对于第一个exception尝试以下操作:

create this theme in your styles.xml 在您的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.. 对于第二个查询,我认为这不会给您在Google Play中的应用造成问题。

but it is best practice to stop all background services when app is destroyed 但是最好的方法是在应用程序被销毁时停止所有后台服务

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

From the log 从日志

Unable to start activity java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 无法启动活动java.lang.IllegalStateException:您需要与此活动一起使用Theme.AppCompat主题(或后代)。

try Theme.AppCompat theme (or descendant) 尝试Theme.AppCompat主题(或后代)

also in this scenario avoid using fused location instead use Location Manager 在这种情况下,也请避免使用融合位置,而应使用位置管理器

LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationManager管理器=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

manager.isProviderEnabled(LocationManager.GPS_PROVIDER); manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM