Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

android - How can I start MAIN activity with the help of <intent-filter>?

When I declare my main activity in this maner:

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:windowSoftInputMode="stateHidden"
          android:screenOrientation="portrait">
    <intent-filter>
          <action android:name="android.intent.action.MAIN"/>
          <action android:name="com.package.name.MyActivity"/>
          <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

then I get an error No Activity found to handle Intent { act=com.package.name.MyActivity flg=0x24000000 } when I am using this code:

Intent intent = new Intent("com.package.name.MyActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);

If do not use Intent i = new Intent(this, MyActivity.class); how can I do this with the help of action for <intent-filter>

Didn't help:

 <intent-filter>
      <action android:name="android.intent.action.MAIN"/>
      <action android:name="com.package.name.VIEW"/>
      <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>

code:

Intent intent = new Intent("com.package.name.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to specify two intent filters:

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:windowSoftInputMode="stateHidden"
          android:screenOrientation="portrait">
    <intent-filter>
          <action android:name="android.intent.action.MAIN"/>
          <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
          <action android:name="com.package.name.MyAction"/>
          <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

Then you can start the activity using the action name:

Intent intent = new Intent("com.package.name.MyAction");
context.startActivity(intent);

or the class name:

Intent intent = new Intent(context, MyActivity.class);
context.startActivity(intent);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...