您的位置:首页 > 其它

Symbian:打造开机自启动程序完整过程

2008-11-11 09:18 441 查看
很多时候我们希望手机一开机就运行我们的程序,做一些我们要做的事,这就可以用到以下知识了

首先说一下这种方法的危险:
1.我还不知道怎样删除手机系统里的mdl文件,所以一旦将mdl文件放在手机里后如果想完全删除我没办法,比较好点的方法应该是将一个无效的mdl文件代替它.
2.mdl文件如果程序出错,可能会开不了机,我没遇到,我是在模拟器上确认没问题才放到手机上的.

另外加个法律声明吧,利用本文章只是做出来的所有伤天害理的事与本人无关.

好吧,下面就说说怎么做:

首先我们新建一个MIME的程序,也就是在新建程序的时候要选择S60 MIME Type Recognizer

进去后
对.h文件做如下修改:
1.添加头文件和lib
#include <apgcli.h>

#include <f32file.h>

#include <apacmdln.h>

#include <e32std.h>

#include <apmstd.h>

lib: apparc.lib apgrfx.lib

2.修改所需要启动的程序UID,这里我以0x0473de92为例,我要启动的程序是test.app,在这个工程的.pkg文件可以看到有如下语句
#{"test"},(0x05dbdd8c),1,0,0
其中0x05dbdd8c就是程序的UID.

将以下2行:
const TUid Ktest1RecogDllUid = { 0x03d8c1a3 };//这个UID是系统生成的,与你的不同
const TInt Ktest1RecogImplementationUid = 0x03d8c1a3;
修改为:
const TUid KMorangeMIMERecogDllUid = { 0x05dbdd8c };
const TInt KMorangeMIMERecogImplementationUid = 0x05dbdd8c;

3.添加函数声明:

TDataType SupportedDataTypeL(TInt aIndex) const;

下面添加3个函数声明如下:
static void StartThread();
static TInt StartAppThread(TAny* aParam);
static void StartAppByUID();

对.cpp文件的修改:
1.添加头文件
#include <apmrec.h>

#include <apmstd.h>

#include <apacmdln.h>

2.添加函数:也就是添加刚才声明的那3个函数的实现:

void CMorangeMIMERecog::StartThread()
{
RLog::Log(_L("In StartThread()"));

TInt res = KErrNone;
//create a new thread for starting our application
RThread * startAppThread;
startAppThread = new RThread();
User::LeaveIfError( res = startAppThread->Create(
_L("MyThreadName"),
CMorangeMIMERecog::StartAppThread,
KDefaultStackSize,
KMinHeapSize,
KMinHeapSize,
NULL,
EOwnerThread));
startAppThread->SetPriority(EPriorityNormal);
startAppThread->Resume();
startAppThread->Close();
}

TInt CMorangeMIMERecog::StartAppThread(TAny* /*aParam*/)
{

RLog::Log(_L("In StartAppThread((TAny* /*aParam*/))"));

//wait 5 seconds...
RTimer timer; // The asynchronous timer and ...
TRequestStatus timerStatus; // ... its associated request status
timer.CreateLocal(); // Always created for this thread.
// get current time (microseconds since 0AD nominal Gregorian)
TTime time;
time.HomeTime();
// add ten seconds to the time
TTimeIntervalSeconds timeIntervalSeconds(30);
time += timeIntervalSeconds;
// issue and wait
timer.At(timerStatus,time);
User::WaitForRequest(timerStatus);
CActiveScheduler * scheduler = new CActiveScheduler();
if( scheduler == NULL )
return KErrNoMemory;
CActiveScheduler::Install(scheduler);
// create a TRAP cleanup
CTrapCleanup * cleanup = CTrapCleanup::New();
TInt err;
if( cleanup == NULL )
{
err = KErrNoMemory;
}
else
{
TRAP( err, StartAppByUID() );
}
delete cleanup;
delete CActiveScheduler::Current();
return err;
}

void CMorangeMIMERecog::StartAppByUID()
{

RApaLsSession ras;

User::LeaveIfError( ras.Connect() );

CleanupClosePushL( ras );

TApaAppInfo appInfo;

ras.GetAppInfo( appInfo, KMorangeMIMERecogDllUid ); //KMorangeMIMERecogDllUid是程序的UID

CApaCommandLine *cmd = CApaCommandLine::NewLC();

cmd->SetLibraryNameL( appInfo.iFullName );
cmd->SetCommandL( EApaCommandRun );

ras.StartApp( *cmd );

CleanupStack::PopAndDestroy( cmd );

CleanupStack::Pop();
ras.Close();
}

3.修改系统自动生成的函数CApaDataRecognizerType* CreateRecognizer()

修改为如下内容:

EXPORT_C CApaDataRecognizerType* CreateRecognizer()
{
CApaDataRecognizerType* thing = new CMorangeMIMERecog();

//start thread for our application

CMorangeMIMERecog::StartThread();

return thing;
}

其实也就是加了一句CMorangeMIMERecog::StartThread();

使得系统启动的时候调用我们的函数做我们的事

这编文章和以下网页的文章很大相同

http://www.sf.org.cn/Article/symbiandev/200701/19987.html

其实我也是看那个文章做出来的,不过那个文章有2个不完美的地方就是添加库时加少了apgrfx.lib,使得程序在编译时出现LNK2019错误,这个错误是连接库时找不到对应的函数产生的,另外的不足是程序路径用了相对路径,我就是因为路径设置一直不成功,User::LeaveIfError( findFile.FindByDir(fnAppPath, KNullDesC) ); 一直Leave才使用UID启动的,另外还有5秒好像太快,我的程序设了30秒

另外参照了 http://discussion.forum.nokia.com/forum/showpost.php?p=216160&postcount=2

附:已经解决试用路径启动的办法,有空再写上来

http://blog.csdn.net/meteor0627/archive/2007/07/02/1675720.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: