55768

Unable to add window — token null is not for an application from Service

Question:

I have read many questions like this but none of them seemed to address my problem.

The problem lies with:

AlertScreen ad = new AlertScreen(SensorListenerService.this);

in my Service class:

public class SensorListener extends Service implements SensorEventListener { public int onStartCommand(Intent intent, int flags, int startId) { startForeground(Process.myPid(), new Notification()); AlertScreen ad = new AlertScreen(SensorListener.this); //problem ad.show(); return START_STICKY; } ...

It calls my AlertScreen class:

public class AlertScreen extends AlertDialog { public AlertScreen(Context context) { super(context); } ...

What LogCat has to say:

<img alt="LocCat" class="b-lazy" data-src="https://i.stack.imgur.com/zqSFh.png" data-original="https://i.stack.imgur.com/zqSFh.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Could anyone shed some light on the problem?

Answer1:

You cannot show Dialogs from a Service context.

I suggest you to open an Activity that actually shows the dialog, or take a look at this answer to learn how to show system alerts.

<blockquote>

<a href="https://stackoverflow.com/a/19269931/1725088" rel="nofollow">https://stackoverflow.com/a/19269931/1725088</a>

</blockquote>

Recommend

  • How can I detect the up-down (Ping-pong) movement of device? [closed]
  • How to make sure that a class runs in background as long as needed on all devices?
  • How to read accelerometer data in Service with Android
  • Android - service keep alive
  • Not able to fetch correct degree from getOrientation method
  • Using Variables on UI Thread from Worker Thread
  • How to send a custom event to AccessibilityService?
  • Android: What is the best way to make repetitive Background Tasks Android Oreo ready?
  • How to Create Forground Service Never Stop when Application is Inactive
  • What is the definition for gamma(double x) and why is it different on two gcc versions?
  • Android AWS S3 SDK TransferUtility Not Working in Service
  • Android - Best practice for a periodic service
  • How to run background service after every 5 sec not working in android 5.1?
  • what is browser's native support according to selenium webdriver
  • Laravel 5.1 Multitenancy setup
  • Receive custom intent without activity restart
  • record audio in HTML / js without Flash?
  • how to set id for each item in ListView
  • Google Maps getMap returns null
  • Android NFC read Tags issue. Activity starts each time on data received
  • Cannot invoke my method on the array type int[]
  • Send data from edittext to listview
  • How can go in last activity with resume button?
  • Regarding starting the threads on a condition
  • How to disable all widgets inside Panel or inside Composite?
  • Android application: how to use the camera and grab the image bytes?
  • how to avoid repetitive constructor in children
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • Highlight and Bold text in JTextPane
  • How to access EntityManager inside Entity class in EJB3
  • how to do an event when i swipe from fragment to the other
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • MySQL WHERE-condition in procedure ignored
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Akka Routing: Reply's send to router ends up as dead letters
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • unknown Exception android
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal