首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DukeScript: Android启动了一个意图

DukeScript: Android启动了一个意图
EN

Stack Overflow用户
提问于 2016-03-11 05:59:31
回答 2查看 104关注 0票数 0

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

代码语言:javascript
复制
    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);

代码语言:javascript
复制
    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);

如果不改变推荐人以适应我们的需要,这是否可能呢?

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-11 12:24:55

很抱歉你的回答太迟了。你不需要换主持人。默认情况下,DukeScript为您提供了一个自动处理场景后面所有内容的活动。但是,您也可以创建一个可以访问所有Android服务的装饰活动。例如:

代码语言:javascript
复制
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中注册此活动,例如:

代码语言:javascript
复制
<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

票数 -1
EN

Stack Overflow用户

发布于 2016-06-24 09:39:14

代码语言:javascript
复制
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活动。

称意图为

代码语言:javascript
复制
Intent intentOpenBluetoothSettings = new Intent();
intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
intentOpenBluetoothSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getmContext().startActivity(intentOpenBluetoothSettings); 

这对于调用您可能需要的任何其他意图来说都是完美的,再次感谢@monacotoni向正确方向的推进。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35933320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档