Blackberry 10上的Sim Card Info问题

时间:2013-03-01 06:47:49

标签: qml blackberry-10 blackberry-cascades

我想显示/获取有关IMSI的读取(在Dev Alpha B上测试)。问题是我可以读取SimCardInfo的其他属性,例如移动网络代码,移动国家代码,序列号但IMSI(subscriberIdentifier)。 这是代码

的main.cpp

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/device/HardwareInfo>
#include <bb/device/SimCardInfo>

#include <QLocale>
#include <QTranslator>
#include <Qt/qdeclarativedebug.h>

using namespace bb::cascades;
using namespace bb::device;

Q_DECL_EXPORT int main(int argc, char **argv)
{
    qmlRegisterUncreatableType<bb::device::HardwareInfo>("bb.device", 1, 0, "HardwareInfo", "");
    qmlRegisterUncreatableType<bb::device::SimCardInfo>("bb.device", 1, 0, "SimCardInfo", "");

    // this is where the server is started etc
    Application app(argc, argv);

    // localization support
    QTranslator translator;
    QString locale_string = QLocale().name();
    QString filename = QString( "hwinfo_%1" ).arg( locale_string );
    if (translator.load(filename, "app/native/qm")) {
        app.installTranslator( &translator );
    }

    //create object
    HardwareInfo hwInfo;
    SimCardInfo simcardInfo;

    QmlDocument *qml = QmlDocument::create("asset:///main.qml");

    qml->setContextProperty("_hardware", &hwInfo);
    qml->setContextProperty("_simcardinfo", &simcardInfo);


    // create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    // set created root object as a scene
    Application::instance()->setScene(root);


    // we complete the transaction started in the app constructor and start the client event loop here
    return Application::exec();
    // when loop is exited the Application deletes the scene which deletes all its children (per qt rules for children)
}

main.qml文件

import bb.cascades 1.0
import bb.device 1.0

Page 
{
    Container 
    {   
        leftPadding: 20
        topPadding: 20
        Container 
        { 
            topMargin: 10
            layout: StackLayout 
                    {
                        orientation: LayoutOrientation.LeftToRight
                    }  
            Button
            {
                text: "retrieve"
                onClicked: 
                {
                    lbl0.text = "Model name: " + _hardware.modelName
                    lbl1.text = "IMEI: " + _hardware.imei
                    lbl2.text = "IMSI: " + _simcardinfo.subscriberIdentifier
                    lbl3.text = "SN: " + _simcardinfo.serialNumber
                    lbl4.text = "Mobile Network Code: " + _simcardinfo.mobileNetworkCode
                    lbl5.text = "Mobile Country Code: " + _simcardinfo.mobileCountryCode
                } 
            }
        }

        Container 
        {   
            layout: StackLayout {                        
                    }
            Label
            {
                id:lbl0
            }                    
            Label 
            {   
                id:lbl1      
            }
            Label
            {
                id:lbl2
            }        
            Label
            {
                id:lbl3
            }        
            Label
            {
                id:lbl4
            }        
            Label
            {
                id:lbl5
            }       
        }

    }
}

非常感谢任何帮助。

参考 http://developer.blackberry.com/cascades/reference/bb_device_simcardinfo.html

1 个答案:

答案 0 :(得分:1)

您将hwInfosimCardInfo声明为堆栈变量,这意味着在构造函数结束后,两个变量都不再存在,因此当QML运行时尝试访问您分配给的属性时他们只是不能。

要使其工作,您需要将这些变量声明为堆变量,以便它们可以比它们的范围更长。

HardwareInfo* hwInfo = new HardwareInfo();
SimCardInfo* simcardInfo = new SimCardInfo();
qml->setContextProperty("_hardware", hwInfo);
qml->setContextProperty("_simcardinfo", simcardInfo);
相关问题