DukeScript:Android启动了一个意图

时间:2016-03-11 05:59:32

标签: dukescript

我们想知道是否有可能获得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);

这可能会改变演示者以满足我们的需求吗?

提前致谢。

2 个答案:

答案 0 :(得分:0)

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;
}
}

Manifest与上面的答案完全相同,仅调用我的AndroidMain活动。

调用意图

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

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

答案 1 :(得分:-1)

对不起,迟到了。您无需更改演示者。默认情况下,DukeScript会为您提供一个自动处理场景背后所有内容的活动。但您也可以创建一个可以访问所有Android服务的装饰活动。 E.g:

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中注册此活动,例如像这样:

<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