DukeScript:Android 开始一个意图

DukeScript: Android start an intent

我们想知道是否可以获取 Duksecript Android 演示者的上下文以便我们调用外部元素?

    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.bluetoothSettings");
    intent.setComponent(cn);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity( intent);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(path);
    intent.setType("application/pdf");
    startActivity(intent);

如果不更改演示者以满足我们的需要,这是否可能?

提前致谢。

抱歉回答晚了。您无需更改演示者。默认情况下,DukeScript 为您提供一个 activity 自动处理幕后的一切。但您也可以创建一个可以访问所有 Android 服务的装饰性 activity。例如:

public class AndroidMain extends Activity {
    public AndroidMain() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // obtain information you need
        //
        SharedPreferences prefs = getApplicationContext().getSharedPreferences("karel.prefs", 0);
        new AndroidStorage(prefs);

        try {
            // delegate to original activity
            startActivity(new Intent(getApplicationContext(), Class.forName("com.dukescript.presenters.Android")));
        } catch (ClassNotFoundException ex) {
            throw new IllegalStateException(ex);
        }
        finish();
     }
   }
}

您还需要在 AndroidManifest.xml 中注册此 Activity,例如像这样:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cz.xelfi.karel"
    android:versionCode="1"
    android:versionName="1.0-SNAPSHOT" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="karel"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <activity android:name="cz.xelfi.karel.AndroidMain"
                  android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.dukescript.presenters.Android" 
                  android:configChanges="orientation|screenSize"
                  android:exported="false"
                >
        </activity>

        <!-- Configuration section. Defines: 
           - the HTML page to load on start
           - the class that contains the main initialization method
           - name of the initialization method in the given class
        -->
        <meta-data android:name="loadPage" android:value="file:///android_asset/pages/index.html" />
        <meta-data android:name="loadClass" android:value="cz.xelfi.karel.AndroidMain" />
        <meta-data android:name="invoke" android:value="main" />
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

https://dukescript.com/best/practices/2015/11/20/AndroidBoot.html

public class AndroidMain extends Activity{
public AndroidMain() {
}
private static Context mContext;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mContext = this.getApplicationContext();
    GlobalVariables.setmContext(getAppContext());//set context to global variable Global Variable
    try {
        // delegate to original activity
        startActivity(new Intent(getApplicationContext(), Class.forName("com.dukescript.presenters.Android")));
    }
catch (ClassNotFoundException ex) {
        throw new IllegalStateException(ex);
    }
    finish();

}

private static Context context;
public static void main(String... args) throws Exception {
    DataModel.onPageLoad();
}

public static Context getAppContext(){
   return mContext;
}
}

清单与上面的答案完全相同,只是调用了我的 AndroidMain Activity。

调用 Intent

Intent intentOpenBluetoothSettings = new Intent();
intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
intentOpenBluetoothSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getmContext().startActivity(intentOpenBluetoothSettings); 

这非常适合调用您可能需要的任何其他意图,再次感谢@monacotoni 向正确方向的推动。