`
hagensas
  • 浏览: 12380 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

如何实现手机中应用程序的开机自启动,附源代码

阅读更多
根据以往Symbian设计的经验,Recognizer将被用来识别与文件绑定的MIME类型。这是用来处理特殊类型数据的步骤之一。相关知识可以参考Nokia那篇著名的《guide for application developer on document handler》文档,在symbian sdk安装目录下面的Series60Doc目录下面可以找到他。
         Recognizer也能够用来实现应用程序的开机自启动,当然这不是Recognizer的本意。但是,很危险的是,如果recognizer出了问题,手机将无法正常启动,而且你必须要重新到当地的Nokia客服部门重新刷机。
        下面的代码是用于EZBoot应用的,这些代码本身有很好的说明,所以我不再详细解释。
        注意,出现在头文件和mmp文件中的0x101Fxxxx这个值需要用你自己项目的UID替换。

头文件源码:

//////////////////////////////////////////////////////////////////////////////
//

//                             EZ-Boot
//                         

//////////////////////////////////////////////////////////////////////////////
//
                       Boot & Recognizer Module
//                    by NewLC (http://www.newlc.com)

//////////////////////////////////////////////////////////////////////////////
//
 File         : ezrecog.h
//
 Compatibility: Symbian OS v6.1
//
 History:
//
   2003.07.26: EBS : Creation
//
   2003.08.12: EBS : Integration in EZBoot
//
   2003.09.01: EBS : Add boot file recognition
//   2003.10.28: EBS : Cleanup and comment

//////////////////////////////////////////////////////////////////////////////

#include 
<apmrec.h> // CApaDataREcognizerType

#define KUidRecog 0x1xxxxxxx // Use your own value here !!!

class CRecog : public CApaDataRecognizerType
{
public

        CRecog();
        TUint PreferredBufSize();
        TDataType SupportedDataTypeL(TInt aIndex) 
const
;
        
static void
 BootUp();
        
static TInt BootUpKick(TAny *
aParam);
        
static void
 BootUpKickL();
        
private
:
        
void DoRecognizeL(const TDesC& aName, const TDesC8&
 aBuffer);
        TBool HeaderRecognized(
const TDesC8&
 aBuf);
        TBool NameRecognized(
const TDesC&
 aName);
};

 

实现的源代码:

//////////////////////////////////////////////////////////////////////////////
//
//                             EZ-Boot
//                         
//////////////////////////////////////////////////////////////////////////////
//                      Boot & Recognizer Module
//                    by NewLC (http://www.newlc.com)
//////////////////////////////////////////////////////////////////////////////
// File         : ezrecog.cpp
// Compatibility: Symbian OS v6.1
// History:
//   2003.07.26: EBS : Creation
//   2003.08.12: EBS : Integration in EZBoot
//   2003.09.01: EBS : Add boot file recognition
//   2003.10.28: EBS : Cleanup and comment
//////////////////////////////////////////////////////////////////////////////

#include 
<e32std.h>
#include 
<e32base.h>
#include 
<e32def.h>
#include 
<f32file.h>
#include 
<apacmdln.h>
#include 
<apgcli.h>
#include 
<apmrec.h> 
#include 
<apmstd.h>
#include 
"ezrecog.h"

//////////////////////////////////////////////////////////////////////////////
//
// Recognition Definitions
//
/////////////////////////////////////////////////////////////////////////////

// The MIME Type that will be recognized
_LIT8(KEzbMimeType,"text/vnd.newlc.ezboot");

// The file extension that shall be used by data we are recognizing
_LIT(KEzbFileExtension,".boot");
 
// The data header that identifies EZBoot data
_LIT8(KEzbDataHeader,"EZBoot:");

// The priority of the recognizer, can be EHigh, ENormal, ELow
#define KEzRecognizerPriority CApaDataRecognizerType::ENormal

// The size of the data buffer that will be passed to the recognizer
// so that it performs the recognition
#define KEzRecognizerBufferSize 7

// The recognizer UID
const TUid KUidEzBoot={KUidRecog};


//////////////////////////////////////////////////////////////////////////////
//
// Boot Definitions
//
/////////////////////////////////////////////////////////////////////////////

// The application we want to boot (here the EZBoot server)
_LIT(KEzBootExe,"\\system\\programs\\ezboot\\ezbootsrv.exe");

// The thread name that will used to launch the above EXE
_LIT(KBootUpThreadName,"EzBootThr");

//////////////////////////////////////////////////////////////////////////////
/// DLL entry point.
/// \param aReason can be ignored.
/// \return Always KErrNone
/////////////////////////////////////////////////////////////////////////////
GLDEF_C TInt E32Dll(TDllReason /*aReason*/)
{
   
return(KErrNone);
}

//////////////////////////////////////////////////////////////////////////////
/// Recognizer instanciation. This function MUST be the first one defined 
/// for the recognizer.
/// \return a pointer on a new allocated recognizer instance
//////////////////////////////////////////////////////////////////////////////
EXPORT_C CApaDataRecognizerType *CreateRecognizer()
{
   
// Create a recognizer instance
   CApaDataRecognizerType *me = new CRecog();
   
   
// Start all the boot code under a trap harness
   
// This is pure boot code and has (normally) nothing to do 
   
// in a recognizer
   CRecog::BootUp();
   
   
return(me);
}

//////////////////////////////////////////////////////////////////////////////
/// Recognizer Constructor. 
/// Initialise the internal data member iCountDataTypes with the number of 
/// MIME types that will be recognized. Set the recognizer priority.
//////////////////////////////////////////////////////////////////////////////        
CRecog::CRecog()
:CApaDataRecognizerType(KUidEzBoot,KEzRecognizerPriority)
{
        iCountDataTypes
=1;
}

//////////////////////////////////////////////////////////////////////////////
/// Returns the size of the data buffer that will be passed to the recognition
/// function (used by the recognition framework)
/// \see DoRecognizeL()
/// \return size of the data buffer
//////////////////////////////////////////////////////////////////////////////        
TUint CRecog::PreferredBufSize()
{
   
return(KEzRecognizerBufferSize);
}

//////////////////////////////////////////////////////////////////////////////
/// Returns the MIME type that our recognizer is able to manage
/// (used by the recognition framework)
/// \param aIndex: the index of the MIME type to return (will be always 1 for
///                a recognizer that handles a single MIME type)
/// \return a MIME type
//////////////////////////////////////////////////////////////////////////////
TDataType CRecog::SupportedDataTypeL(TInt /*aIndex*/const
{
   
return(TDataType(KEzbMimeType));
}

/////////////////////////////////////////////////////////////////////////////
/// The recognition function. The result of the recognition is stored in 
/// the iConfidence data member.
/// \param aName:   the name of the file that contain the data to analyze
/// \param aBuffer: the data buffer
/// \see PreferredBufSize()
/////////////////////////////////////////////////////////////////////////////
void CRecog::DoRecognizeL(const TDesC& aName, const TDesC8& aBuffer)
{
   
// Initialise the result status
   iConfidence = ENotRecognized;
   iDataType   
= TDataType(KEzbMimeType);
   
   
// Check that we got the required amount of data
   if(aBuffer.Length()<KEzRecognizerBufferSize)
           
return;

   
// Check that the file name corresponds to our criteria
   TBool nameOK(EFalse);
   nameOK
=NameRecognized(aName);
   
   
// Check that the data corresponds to our criteria
   TBool headerOK(EFalse);
   headerOK
=HeaderRecognized(aBuffer);            
   
   
// Conclude: 
   
// - if file name and data are OK then the data are certainly recognized
   
// - if only the data are recognized, then this is only a possibility
   
// - else the data have not been recognized
   if( nameOK && headerOK)
   {
           iConfidence
=ECertain;
   }
   
else if(!nameOK && headerOK)
   {
           iConfidence
=EPossible;
   }
   
else
           
return;
}

/////////////////////////////////////////////////////////////////////////////
/// The file name recognition function. This functions checks whether the 
/// provided filename matches our criteria (here we want it to have the .boot
/// extension)
/// \param aName: the name to check
/// \return ETrue if the file is OK
/////////////////////////////////////////////////////////////////////////////
TBool CRecog::NameRecognized(const TDesC& aName)

  TBool res
=EFalse;
  
if(aName.Length()>5)
  {
     TInt dotPos 
= aName.LocateReverse( '.' );
     
if (dotPos != KErrNotFound)
     {
        TInt extLength 
= aName.Length() - dotPos;
        HBufC
* ext = aName.Right( extLength ).AllocL();
        CleanupStack::PushL( ext );
        
if ( ext->CompareF(KEzbFileExtension) == 0 )
        {
          res 
= ETrue;
        }
        CleanupStack::PopAndDestroy(); 
// ext
     }
   }
   
return(res);
}

/////////////////////////////////////////////////////////////////////////////
/// The data recognition function. This functions checks whether the 
/// provided data starts with our data header
/// extension
/// \param aBuf: the data buffer to check
/// \return ETrue if the data are OK
/////////////////////////////////////////////////////////////////////////////
TBool CRecog::HeaderRecognized(const TDesC8& aBuf)

   
if(aBuf.Find(KEzbDataHeader)==0)
        
return ETrue;
   
return EFalse;
}


/////////////////////////////////////////////////////////////////////////////
/// The Boot code (non leaving). Create a new thread and kicks the real
/// boot code. 
/// \see BootUpKick()
/////////////////////////////////////////////////////////////////////////////
void CRecog::BootUp()
{
   
// Create a new thread
   RThread* bootThread = new RThread();
   
if(bootThread)
   {
       TInt res
=KErrNone;
       
       
// and Start it
       res=bootThread->Create(KBootUpThreadName,
                              CRecog::BootUpKick,
                              KDefaultStackSize,
                              KMinHeapSize,
                              KMinHeapSize,
                              NULL,
                              EOwnerThread);
       
       
if(res==KErrNone)
       {
           bootThread
->Resume();
           bootThread
->Close();
       }
       
else
       {
           delete bootThread;
       }
   }
}

/////////////////////////////////////////////////////////////////////////////
/// The threaded boot code (non leaving). Actually just create a cleanup
/// stack and call a non-leaving implementation of the boot code
/// \see BootUp()
/// \see BootUpKickL()
/// \param aParam: not used but required as a thread entry point
/// \return thread result
/////////////////////////////////////////////////////////////////////////////
TInt CRecog::BootUpKick(TAny* /*aParam*/)
{
   TInt err
=KErrNoMemory;
       
// Create a cleanup stack
   CTrapCleanup *cleanup=CTrapCleanup::New();
   
if(cleanup)
   {
       
// and Kick under a trap harness
       TRAP(err,CRecog::BootUpKickL());
       delete cleanup;
   }
   
return err;
}

/////////////////////////////////////////////////////////////////////////////
/// The Boot code. 
/////////////////////////////////////////////////////////////////////////////
void CRecog::BootUpKickL()
{
        
// Get the full path (including drive letter)
        
// to the boot server
        RFs fs;
        User::LeaveIfError(fs.Connect());
        CleanupClosePushL(fs);
        TFindFile findFile(fs);
        User::LeaveIfError(findFile.FindByDir(KEzBootExe,KNullDesC));

        
// Connect to the Apparc server
        
// and start our server
        RApaLsSession ls;
        User::LeaveIfError(ls.Connect());
        CleanupClosePushL(ls);
        CApaCommandLine 
*cmd = CApaCommandLine::NewLC();
        cmd
->SetLibraryNameL(findFile.File());
        cmd
->SetCommandL(EApaCommandOpen);
        User::LeaveIfError(ls.StartApp(
*cmd));

        
// Delete all stuff on the cleanup stack
        CleanupStack::PopAndDestroy(3);
}

MMP文件的源代码:
TARGET      ezrecog.mdl
TARGETTYPE  mdl
TARGETPATH  \system\recogs

UID         
0x10003A19 0x101Fxxxx 

USERINCLUDE   ..\inc
SYSTEMINCLUDE \epoc32\include

SOURCEPATH    ..\src
SOURCE        ezrecog.cpp

LIBRARY       euser.lib 
LIBRARY       apmime.lib apparc.lib apgrfx.lib
LIBRARY       efsrv.lib
分享到:
评论

相关推荐

    Service开机自动启动

    (也可以实现应用程序开机自动启动) public class OlympicsReceiver extends BroadcastReceiver { /*要接收的intent源*/ static final String ACTION = "android.intent.action.BOOT_COMPLETED"; public ...

    小闹钟程序(C#源代码编写)

    小闹钟程序(C#源代码编写) 小闹钟程序显示时间,可以设置开机自启动、最在最前方 提醒功能等。C#源代码编写,右键功能。

    Alarm自动间隔闹铃程序(含源代码

    一个会在固定一段时间之后响闹铃并弹出提醒框的小程序。...开机启动后不能随意移动应用程序位置,否则定位不能。(所以说山寨……) 写这个主要练习一下开线程和一些MFC常用技巧。欢迎拍砖及交流。

    C#使应用程序开机自动运行

    摘要:C#源码,系统相关,开机启动,自动运行 C#使应用程序开机自动运行,浏览到指定的应用程序,可设置为开机即启动该程序。

    termux-boot:Termux附加应用程序,允许程序在启动时运行

    在切换安装源之前,您将必须卸载Termux应用程序和所有当前安装的插件。 如何使用 安装Termux:Boot应用程序。 通过单击启动器图标启动Termux:Boot应用程序。 这允许该应用在引导时运行。 创建~/.termux/boot/...

    vc++ 应用源码包_6

    自绘按钮button源代码 自绘编辑框 自绘窗体界面 自绘对话框 listbox-6 重载CListBox,演示了拖动功能。 Mail_Report 演示了发送邮箱的功能。 MD5算法 MediaPlayer 视频播放的实现。 MFC 对话框 MP3 内部包含:...

    vc++ 应用源码包_5

    自绘按钮button源代码 自绘编辑框 自绘窗体界面 自绘对话框 listbox-6 重载CListBox,演示了拖动功能。 Mail_Report 演示了发送邮箱的功能。 MD5算法 MediaPlayer 视频播放的实现。 MFC 对话框 MP3 内部包含:...

    明日科技《C#示例源代码》(17-20)

    注意:本源代码共有20章节,分五部分上传,名称分别为:明日科技《C#示例源代码》(1-4)、明日科技《C#示例源代码》(1-4)、明日科技《C#示例源代码》(5-8)、明日科技《C#示例源代码》(9-12)、明日科技《C#...

    vc++ 应用源码包_1

    自绘按钮button源代码 自绘编辑框 自绘窗体界面 自绘对话框 listbox-6 重载CListBox,演示了拖动功能。 Mail_Report 演示了发送邮箱的功能。 MD5算法 MediaPlayer 视频播放的实现。 MFC 对话框 MP3 内部包含:...

    C#开机启动项管理程序

    如果发现你的电脑有许多开机自动运行的程序,可以用这个C#写的开机自启动项目管理程序来删除掉,使用方法:在列表中勾选开机时不自动运行的项目,然后点击“优化”按钮即可。 运行环境:Visual Studio2010

    C#闹钟&&时钟小程序(源代码)

    6、开机自启动。这个可以自己设定,很多人不需要。 7、响铃抖屏。闹钟到点后会抖动一小段时间(我设置的3秒)的屏幕,并同步跳到你打开的所有窗口的最顶层窗体。 8、系统托盘。可以隐藏到系统托盘。 三、备忘录 {...

    VB病毒攻防学习实例源代码

     2、一般开机都自启动  3、建立文件关联  4.自我复制到系统目录  5.设定没隔1段时间运行,用timer控件很容易做到  6.隐藏应用程序  7.隐藏进程  8.自删除  我现在在写木马一启动就自动找GHOST并且将...

    明日科技《C#示例源代码》(13-16)

    注意:本源代码共有20章节,分五部分上传,名称分别为:明日科技《C#示例源代码》(1-4)、明日科技《C#示例源代码》(1-4)、明日科技《C#示例源代码》(5-8)、明日科技《C#示例源代码》(9-12)、明日科技《C#...

    明日科技《C#示例源代码》(5-8)

    注意:本源代码共有20章节,分五部分上传,名称分别为:明日科技《C#示例源代码》(1-4)、明日科技《C#示例源代码》(1-4)、明日科技《C#示例源代码》(5-8)、明日科技《C#示例源代码》(9-12)、明日科技《C#...

    明日科技《C#示例源代码》(9-12)

    注意:本源代码共有20章节,分五部分上传,名称分别为:明日科技《C#示例源代码》(1-4)、明日科技《C#示例源代码》(1-4)、明日科技《C#示例源代码》(5-8)、明日科技《C#示例源代码》(9-12)、明日科技《C#...

    vc++ 应用源码包_2

    自绘按钮button源代码 自绘编辑框 自绘窗体界面 自绘对话框 listbox-6 重载CListBox,演示了拖动功能。 Mail_Report 演示了发送邮箱的功能。 MD5算法 MediaPlayer 视频播放的实现。 MFC 对话框 MP3 内部包含:...

    vc++ 应用源码包_3

    自绘按钮button源代码 自绘编辑框 自绘窗体界面 自绘对话框 listbox-6 重载CListBox,演示了拖动功能。 Mail_Report 演示了发送邮箱的功能。 MD5算法 MediaPlayer 视频播放的实现。 MFC 对话框 MP3 内部包含:...

    vc++ 开发实例源码包

    DirectUI移植到MFC中实现。 MFCHtml 调用脚本 如题。 MFC使用COM加载WMI服务,另类获取系统服务详细 大家都知道,现在流行的检测硬件软件视乎很神秘,我们要获得各种信息好像比较难.但大多数这种软件或多或少的使用了...

    Kesoft.Windows.Startup:Windows 启动助手

    Windows系统下应用程序开机自启动帮助开发好的Windows系统下的应用程序经常需要随系统自动启动,该帮助项目提供了代码和脚本两种方式来实现该功能。如何下载代码实现的可以通过nuget下载:安装脚本实现可以直接下载...

    IT开发方面的视频教程以及案例视频

    专题:源代码管理工具的使用与配 置 Asp.net 开发企业网站 http://edu.ibeifeng.com/view-index-id-350.html专题:JavaScript 压缩、调试及性 能调优 专题:asp.net 下的工作流技术 WF4.0 专题:Excel 数据导出导入...

Global site tag (gtag.js) - Google Analytics