gpt4 book ai didi

android - 如何在运行时在 android 中将我的应用程序设置为启动器?

转载 作者:行者123 更新时间:2023-11-30 00:53:36 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,并希望在运行时作为启动器启动,而不使用 list 文件中的类别标签。

<activity
android:name="com.sample.test.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

最佳答案

没有办法直接按照你想要的方式去做。要设置启动器 Activity,您必须编辑 list ,但实际上您无法这样做。
但是您可以使用此 Intent 过滤器在 list 中声明一些 LauncherActivity:

<activity
android:name="com.sample.test.LauncherActivity"
android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

在它的 onCreate() 方法内部读取以前保存的 Activity 名称,您真的需要从 SharedPreferences 中单击应用程序图标,然后启动它。像这样:

public class LauncherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String activityToLaunch = getSharedPreferences("LauncherActivity", MODE_PRIVATE).getString("LauncherActivity", "Some default Activity");
Intent intent;
switch (activityToLaunch) {
case "SomeActivity1":
intent = new Intent(this, SomeActivity1.class);
break;
case "SomeActivity2":
intent = new Intent(this, SomeActivity2.class);
break;
case "SomeActivity3":
intent = new Intent(this, SomeActivity3.class);
break;
default:
intent = new Intent(this, SomeDefaultActivity.class);
break;
}
startActivity(intent);
finish();
}
}

关于android - 如何在运行时在 android 中将我的应用程序设置为启动器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40542288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com