Since I have exception running
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
in libgdx's render(),
I have an idea to run bluetooth settings first in Android activity
and then to close the activity and initialize libgdx.
The following code crashes:
public class MyActivity extends AndroidApplication {
public void onCreate (android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Intent myIntent = new Intent(MyActivity.this, StartActivity.class);
startActivity(myIntent);
initialize(new MyGame(), false); //run libgdx
}
}
StartActivity is as follows (later it'll be game title and bluetooth settings):
public class StartActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
text.setText("Hello World, Android");
setContentView(text);
try {
Thread.sleep(7000);
}
catch (InterruptedException e) {};
finish();
return;
}
}
No crash message, but app stays with black screen (even no "hello" message) if I add the following :
Intent i = new Intent(this, MyActivity.class);
startActivity(i);
There is "E/AndroidRuntime(612): ERROR: thread attach failed" in logcat. But as I said no crash message.
Something is wrong here... I don't know how to write such code correctly. Thanks.
That's not going to work anyway, but I'd say it is likely crashing due to the StartActivity not being declared in the Manifest.
Yes, you can start an activity before running libgdx. Don't start the libgdx stuff first. Start the Bluetooth settings, then call the Intent
to start the actual game when ready.
Like this:
public class BluetoothActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetoothsettings);
Button startGame = (Button)findViewById(R.id.btnStartGame);
// handle set start click
startGame.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(BluetoothActivity.this, GameActivity.class);
BluetoothActivity.this.startActivity(intent);
}
});
}
}
Then your game activity can just be the normal one that extends AndroidApplication
like you have above.
And of course make sure your AndroidManifest.xml
starts BluetoothActivity
first, not libgdx.
<activity android:name=".BluetoothActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
来源:https://stackoverflow.com/questions/11474766/can-i-run-an-activity-before-running-libgdx