我先用以下方式聲明了mian activity:
<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>
然後使用如下代碼時就得到一個錯誤提示:No Activity found to handle Intent { act=com.package.name.MyActivity flg=0x24000000 }
Intent intent = new Intent("com.package.name.MyActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
如果不使用,如何使用來開始這個動作呢?
我又改為以下代碼,還是不正確。
<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>
Intent intent = new Intent("com.package.name.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
先指定兩個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>
然後使用action名稱開啟activity:
Intent intent = new Intent("com.package.name.MyAction");
context.startActivity(intent);
或者使用class名稱:
Intent intent = new Intent(context, MyActivity.class);
context.startActivity(intent);