0

We would like to know if it is possible to get the context of Duksecript Android presenter so we can call external elements?

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

or

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

would this be possible short of changing the presenter to suit our needs?

Thanks in advance.

Ed_Hill
  • 3
  • 4

2 Answers2

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

The Manifest is Exactly the same as the Above answer only calling my AndroidMain Activity.

To Call the Intent

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

This Works Perfect for calling any other intent you might need, Thanks again @monacotoni for the push in the right direction.

Ed_Hill
  • 3
  • 4
-1

Sorry for the late answer. You don't need to change the presenter. By default DukeScript provides you an activity which handles everything behind the scene automatically. But you can also create a decorating activity that has access to all Android services. 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();
     }
   }
}

You’ll also need to register this Activity in your AndroidManifest.xml, e.g. like this:

<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

monacotoni
  • 606
  • 5
  • 18
  • Hi Tony... Thanks for the Help, I did not -1 it I will try this as soon as we work on the project again as we are working on something else at the moment but will return to this asap. – Ed_Hill May 27 '16 at 11:01
  • Great, let me know when you need help. – monacotoni May 30 '16 at 08:37
  • Hi There. Just a Follow Up. I have manged to get this Right and wanted to Thank you for the Help. Just Adding our example as it might help someone else who is still struggling with this. – Ed_Hill Jun 24 '16 at 09:22