3

请帮助我如何在 Libgdx 线程中运行以下代码 - 在 render()、create() 等...

public class MyGame implements ApplicationListener, InputProcessor {
...
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
.....
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"http://market.android.com/details?id=" + getPackageName()));
startActivity(marketIntent);

代码有编译错误。我搜索了一些类似的线程,但没有“startActivity”的确切代码示例。谢谢。

4

2 回答 2

16

LibGDX is a platform independent library, so all the code that uses the LibGDX platform netural APIs must itself be platform independent (so no Android, or Windows calls, etc). To access platform-specific features, the standard way is to define an interface, and use the interface from your platform-neutral code. Then create an implementation of the interface in the Android (or Desktop) specific projects for your application, and pass that implementation when you initialize your libGDX component.

This tutorial has more details: http://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements3TierProjectSetup

Here's another description of the same approach (its better written, but the example isn't as clearly relevant to you): https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code

The tutorial is interested in accessing Android native UI elements, but the basic idea is the same as what you want.

于 2012-10-02T23:08:10.233 回答
3

您收到错误是因为startActivity()Activity类中的一个方法。

为了能够使用它,您的班级必须

  1. 扩展Activity或扩展Activity的类
  2. 在某处有一个 Activity 实例,可能通过构造函数传入

在第二种情况下,你会得到类似的东西:

public class MyNonActivity {
    Context mContext;
    public MyNonActivity(Context context) {
        mContext = context;
    }

    public void myMethod() {
        Intent intent = new Intent(mContext, Next.class);
        mContext.startActivity(intent);
    }
}

并从ActivityService或其他子类Context或其子类之一调用您的类:

MyNonActivity foo = new MyNonActivity(getBaseContext());

onCreate()确保在被调用时或之后执行上述操作。

于 2012-10-02T16:13:32.520 回答