Launching Activity / custom view from a Service

Put your problem here if it does not fit any of the other categories.

Launching Activity / custom view from a Service

Postby tg » Wed Jan 16, 2008 8:15 pm

Hello,

PlusMinus, thanks for this amazing website & the informative tutorials. I have learned more from here than from Google's documentation / groups.

Anyways, I am new to the Android world so please bear with me if this sounds like a beginner question.

When I click a button on my application's main view, it launches a service. The idea is that this service can run in the background & notify users when an even happens (such as change in location).

Here is how I am launching the service (which works just fine)...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onClick(View v) {          
  2.  
  3.                        
  4.  
  5.                         startService(new Intent(Aclient.this,AService.class),null);            
  6.  
  7.                 }  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


However, I am having trouble with notifying when a location change occurs. The notification works if I use the NotificationManager's NotifyWithText method but errors out with a "BadTokenException: Unable to Add Window", if I try to use AlertDialogs or a custom activity view (say to launch the mapview).

I think the service is having trouble finding reference to the active view on the emulator. Any suggestions?

Here is how I am launching the custom view...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         protected void onCreate()
  2.  
  3.         {
  4.  
  5.                 WindowManager windowManager = (WindowManager)getSystemService(this.WINDOW_SERVICE);            
  6.  
  7.                                
  8.  
  9.             TextView textView = new TextView(this);
  10.  
  11.             textView.setText("Text View ");          
  12.  
  13.                        
  14.  
  15.             WindowManager.LayoutParams p = new WindowManager.LayoutParams(
  16.  
  17.                         WindowManager.LayoutParams.APPLICATION_SUB_PANEL_TYPE,
  18.  
  19.                         WindowManager.LayoutParams.NOTIFICATION_TYPE);
  20.  
  21.            
  22.  
  23.             windowManager.addView(textView, p);
  24.  
  25.         }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


I get the error "WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?."

I would really appreciate if someone can guide me on what would be the appropriate way to launch a custom view (or even an AlertDialog) from within the service.

Thanks In Advance,

Trey
tg
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Jan 16, 2008 5:19 pm

Top

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 25 guests