创建一个Activity
作为应用程序的主要入口点。该活动将检查用户是否选择了默认活动。如果他有,那么此活动将启动该活动并自行关闭。否则,它将提示用户选择默认活动。
示例代码(您需要进行一些修改以满足您的要求):
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
int choice = sharedPref.getInt("default_activity", -1);
if (choice == -1) {
// show the option to choose the default activity to the user
// e.g. dialog with list, then save the corresponding choice to
// shared preference
String[] activities = { "Activity 1", "Activity 2", "Activity 3" };
AlertDialog.Builder builder = new Builder(this);
builder.setAdapter(
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, activities),
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("default_activity", which);
editor.commit();
launchActivity(which);
}
}).show();
} else {
// start the activity and close this activity
launchActivity(choice);
}
}
private void launchActivity(int choice) {
switch (choice) {
case 0:
startActivity(new Intent(this, Activity1.class));
break;
case 1:
startActivity(new Intent(this, Activity2.class));
break;
case 2:
startActivity(new Intent(this, Activity3.class));
break;
}
finish();
}
}