这是我的想法,我想在 android 中创建一个类似活动的组件,所以以这种方式,我希望我的主要活动源自我的启动活动,该启动活动源自 Activity,并且在我的启动活动中,我想检查许可证,如果它是可用然后我想重定向到从启动活动派生的主要活动。
我的整个想法是拥有一个组件,因此通过从主要活动扩展它,一切都应该自动化。
我已经使用以下代码完成了此操作,但它运行不顺利,我确信我的方法有问题:
//my main activity which will be shown after splash screen
public class MyActivity extends SplashLoader
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.setNextActivity(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText("hello");
}
}
//my splash activity
public class SplashLoader extends Activity
{
private static final String ERROR_MESSAGE = "Error message";
private Activity nextActivity=null;
public Activity getNextActivity() {
return nextActivity;
}
public void setNextActivity(Activity nextActivity) {
this.nextActivity = nextActivity;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout lLayout = new LinearLayout(this);
lLayout.setOrientation(LinearLayout.VERTICAL);
lLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
iv.setImageResource(R.drawable.logo);
lLayout.addView(iv);
setContentView(lLayout);
boolean isRegistered = false;
if(!isRegistered)
{
createDialog().show();
}
else
{
// finish();
Intent intent = new Intent(SplashLoader.this,getNextActivity().getClass());
startActivity(intent);
}
}
private AlertDialog.Builder createDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which)
{
switch (which)
{
case DialogInterface.BUTTON_NEUTRAL:
System.exit(0);
break;
default:
break;
}
}
} ;
builder.setMessage(ERROR_MESSAGE).setNeutralButton("Ok" ,clickListener );
return builder;
}
}
//and my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application android:label="@string/app_name">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
编辑:
这种方法没有任何错误,但是在splash想要加载mainActivity之后它有一个滞后或者在改变方向之后我可以看到mainActivity。这种方法的行为有点奇怪。