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?
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With