如何获取IMSI号

CImsiReader example illustrates how to read IMSI (SIM card's identity number, which has nearly nothing to do with the MSISDN that is the phone number used for calling) in 3rd Edition Symbian devices. Note that this code will most likely not work in the emulator, thus you should only use it in real devices.


1.1 Link against:
etel3rdparty.lib


1.2 Capability require:
CAPABILITY  ReadDeviceData


1.3 IMSI_Getter.cpp


#include "Imsi_Getter.h"  //LP: added #include for Imsi_Getter.h header file
 
CImsiReader* CImsiReader::NewL(MImsiObserver* aObserver)
    {
    CImsiReader* self = NewLC(aObserver);
    CleanupStack::Pop(self);
    return self;
    } 
 
CImsiReader* CImsiReader::NewLC(MImsiObserver* aObserver)
    {
    CImsiReader* self = new (ELeave) CImsiReader(aObserver);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    } 
 
CImsiReader::CImsiReader(MImsiObserver* aObserver)
:CActive(0),iObserver(aObserver),iImsiV1Pkg(iImsiV1)
{
}
 
CImsiReader::~CImsiReader()
{
 Cancel();
 delete iTelephony;
 
}
 
void CImsiReader::ConstructL(void)

 CActiveScheduler::Add(this);
 
 iTelephony = CTelephony::NewL();
 iTelephony->GetSubscriberId(iStatus,iImsiV1Pkg);
 SetActive();
}
 
void CImsiReader::DoCancel()
{
 iTelephony->CancelAsync(CTelephony::EGetSubscriberIdCancel);

 
void CImsiReader::RunL()
{
        //LP: added ";" and replaced iTelephony with iObserver
 iObserver->GotIMSIL(iImsiV1.iSubscriberId,iStatus.Int());
}


1.4 IMSI_Getter.h


#include <Etel3rdParty.h>
 class MImsiObserver
 {
 public: // New methods
  virtual void GotIMSIL(const TDesC& aIMSI, TInt aError) = 0;
 };
 
 class CImsiReader : public CActive
     {
 
 public:
  static CImsiReader* NewL(MImsiObserver* aObserver);
  static CImsiReader* NewLC(MImsiObserver* aObserver);
  ~CImsiReader();
 protected: 
  void DoCancel();
  void RunL();
 private:
  CImsiReader(MImsiObserver* aObserver);
     void ConstructL(void);
 private:
  MImsiObserver*      iObserver;
  CTelephony*      iTelephony;
  CTelephony::TSubscriberIdV1  iImsiV1;   
     CTelephony::TSubscriberIdV1Pckg iImsiV1Pkg;
     };


1.5 NB: Note for console applications
In a Console application you have to create a Active scheduler (to run active objects) before calling the code above or the code above will not work e.g.
LOCAL_C void ApplicationL()
 {
  CIMSIApp::GetIMSI(iIMSI);
 }
 
LOCAL_C void Main()
 {
  // Create active scheduler (to run active objects)
  CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
  CleanupStack::PushL(scheduler);
  CActiveScheduler::Install(scheduler);
  ApplicationL();
  CleanupStack::PopAndDestroy(scheduler);
 }

你可能感兴趣的:(如何获取IMSI号)