How to use Activity attribute android:showForAllUs

2019-07-19 14:40发布

问题:

The attribute is not implemented in Xamarin. This mean I can't declare it as an attribute of my Activity Class like it should:

[Activity(Label = "@string/app_name", Theme = "@style/MainTheme.StopAlarm", LaunchMode = Android.Content.PM.LaunchMode.SingleTask, ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, NoHistory = true)]
public class StopAlarmActivity : Activity
{
...
}

There's no ShowForAllUsers option.

The other option is to edit Properties => AndroidManifest.xml and add the activity manually like this:

<activity
          android:name=".StopAlarmActivity"
          android:label="@string/app_name"
          android:theme="@style/MainTheme.StopAlarm"
          android:showForAllUsers="true">
</activity>

But once I compile the final AndroidManifest.xml file contains two declarations, the manually added and the compiled one from the class declaration.

<activity android:name=".StopAlarmActivity" android:label="@string/app_name" android:theme="@style/MainTheme.StopAlarm" android:showForAllUsers="true"></activity>
<activity android:label="@string/app_name" android:launchMode="singleTask" android:noHistory="true" android:screenOrientation="portrait" android:theme="@style/MainTheme.StopAlarm" android:name="md5e24228758d0205525e724fe958bff865.StopAlarmActivity" />

That said, it looks like the only option is to edit the compiled AndroidManifest.xml file after each compilation. Which is a major breach for problems.

Any other way to accomplish it where I don't need to rely on remembering it every time I compile the app?

回答1:

On your Activity attribute, applied a Name to avoid the MD5-based Java class auto-naming:

[Activity(Name = "com.sushihangover.MainActivity")]

In your manifest use the same fully-qualified name (no abbreviated dot notation) with the attributes you wish to apply to this activity:

<activity
  android:name="com.sushihangover.MainActivity"
  android:showForAllUsers="true"
   android:icon="@mipmap/icon" 
   android:label="MainActivity" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Note: All the attributes for this Activity need to be added within the manifest, not the Activity class attribute.

The final manifest entries will be merged correctly:

<activity android:name="com.sushihangover.MainActivity" android:showForAllUsers="true" android:icon="@mipmap/icon" android:label="MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>