Hi,
I’ve been trying to get some native code running in Android to return the device id
using the TelephonyManager.
FuseforeignCodeExample.unoproj:
{
"RootNamespace":"",
"Packages": [
"Fuse",
"FuseJS"
],
"Includes": [
"*.uno",
"*.uxl",
"*.ux"
]
}
MainView.ux:
<App>
<DockPanel>
<StatusBarBackground Dock="Top" />
<BottomBarBackground Dock="Bottom" />
<Device ux:Global="Device" />
<JavaScript>
var Observable = require("FuseJS/Observable");
var device = require("Device");
var mcDeviceUUID = Observable("DeviceID");
function button_pressed() {
mcDeviceUUID.value = "DeviceID:" + device.getDeviceUUID();
}
module.exports = {
button_pressed: button_pressed,
mcDeviceUUID: mcDeviceUUID
};
</JavaScript>
<StackPanel>
<Text FontSize="30" Alignment="HorizontalCenter" Value="{mcDeviceUUID}" Margin="0,0,0,10" TextColor="#000" />
<Button Text="Press me!" Clicked="{button_pressed}" />
</StackPanel>
</DockPanel>
</App>
MainView.uxl:
<Package>
<Extensions Backend="CPlusPlus" Condition="Android">
<Require AndroidManifest.Permission="android.permission.READ_PHONE_STATE" />
</Extensions>
</Package>
Device.uno:
using Uno;
using Uno.Collections;
using Uno.Compiler.ExportTargetInterop;
using Fuse;
using Fuse.Scripting;
[ForeignInclude(Language.Java, "android.app.Activity",
"android.provider.Settings",
"android.telephony.TelephonyManager",
"android.content.*",
"java.security.*",
"java.net.*",
"java.nio.*",
"java.io.*")]
public class Device : NativeModule
{
public Device()
{
AddMember(new NativeFunction("getDeviceUUID", (NativeCallback)GetDeviceUUID));
}
object GetDeviceUUID(Context c, object[] args)
{
return GetDeviceUUID();
}
[Foreign(Language.Java)]
[Require("AndroidManifest.RootElement", "<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>")]
static extern(Android) string GetDeviceUUID()
@{
final android.app.Activity loContext = com.fuse.Activity.getRootActivity();
final TelephonyManager loTm = (TelephonyManager)loContext.getSystemService(Context.TELEPHONY_SERVICE);
return "" + loTm.getDeviceId();
@}
static extern(!Android) string GetDeviceUUID()
{
debug_log("Not supported on this platform.");
return "";
}
static extern(!(iOS||Android)) string GetDeviceUUID()
{
return "Default";
}
}
It all compiles and runs ok on Android however when I press the button on the MainView.ux screen I get an
error:
Error: java.lang.SecurityException: getDeviceId: Neither user 10280 nor current process has android.permission.READ_PHONE_STATE.
As you see I’ve included the required permission in the .uxl file and in the .uno file.
Regards,
Matt