5

Hi. I am an Android beginner trying to make an IntentFilter that can filter multiple actions. Unfortunately, when I begin using the addAction method, Eclipse throws an error:

"Syntax error on token "addAction", Identifier expected after this token"

even though I have imported the required file. This is probably a settings issue even though I am unsure why rest of the code doesnt show any problems.

Here is my code stub:

import android.content.Intent;
import android.content.Context;
import android.content.IntentFilter;

...

private Context mContext;
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.SCREEN_OFF");

I observed one more thing--as I type ' filter.' nothing shows up, just the message "No Default Proposals"

Can anybody help me?

3
  • You probably need to show more sorrounding code, not just a small excerpt. May be your filter variable are out of the scope or something else...
    – Olegas
    Commented May 23, 2011 at 15:06
  • out of scope ?? did not understand ?? i havent declared the IntentFilter in inside any methods , following that mContext.registerReceiver(status_Handler,TMS); here too while trying to registerReceiver , I get errors saying "Multiple markers at this line - Syntax error on token ")", delete this token - Syntax error on token "(", delete this token" "status_Handler" is my broadcast reciever method... has anyone ever faced the..the error statment i got
    – amIT
    Commented May 23, 2011 at 16:39
  • If you post your code, i can try to help you.
    – Olegas
    Commented May 23, 2011 at 17:24

2 Answers 2

8

You're trying to run code outside of a method scope, which is illegal in Java (unless it's an assignment). If you change the code to

private Context mContext;
IntentFilter filter = new IntentFilter();
{
    filter.addAction("android.intent.action.SCREEN_OFF");
}

it will run as part of the object constructor.

1
  • I think this best answers the question. The marked answer is sort of tackling a different concept.
    – Rachael
    Commented Oct 6, 2015 at 22:47
5

Reading your comment above in the question ("Multiple markers at this line - Syntax error on token ")", delete this token, etc.), I still think it is a bracket (or parenthesis) mismatch problem that does not let Eclipse understand the code. It could be in another method. Check carefully all your file. You may try to remove most of your code (just copy it to notepad) until you narrow the issue.


From the dev guide (emphasis mine):

An intent filter is an instance of the IntentFilter class. However, since the Android system must know about the capabilities of a component before it can launch that component, intent filters are generally not set up in Java code, but in the application's manifest file (AndroidManifest.xml) as elements. (The one exception would be filters for broadcast receivers that are registered dynamically by calling Context.registerReceiver(); they are directly created as IntentFilter objects.)

I would add this to your manifest instead:

<intent-filter android:label="@string/screen_off">
  <action android:name="android.intent.action.SCREEN_OFF" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
3
  • well yes i but precisely im tryig to create a intent filter just to register a broadcast listner.
    – amIT
    Commented May 23, 2011 at 16:19
  • What it does not make any sense is the "syntax error" message. Make sure you do not have any missing (or extra) bracket somewhere.
    – Aleadam
    Commented May 23, 2011 at 16:35
  • no brackets issue infact i have followed one of the test stubs given here and android developer site... this is what is really bugging such stupid error which i cant make any sense... any idea why i cant see any "No Default Proposals" for the "filter" Intent filter i made ..i have hunch that is the problem..
    – amIT
    Commented May 23, 2011 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.