|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
最近一直都在学习有关ios(苹果系统)的知识,而学习ios的最好方法就是研究iPhone和ipad。因为iPhone和iPad的应用程序都是用Objective-C语言来开发的,所以在接触ios之前,我必须有一定的Objective-C基础。AndroidService组件在GoogleAndroidSDK官网上的界说是如许的:
AServiceisanapplicationcomponentrepresentingeitheranapplicationsdesiretoperformalonger-runningoperationwhilenotinteractingwiththeuserortosupplyfunctionalityforotherapplicationstouse.EachserviceclassmusthaveacorrespondingdeclarationinitspackagesAndroidManifest.xml.ServicescanbestartedwithContext.startService()andContext.bindService().
Notethatservices,likeotherapplicationobjects,runinthemainthreadoftheirhostingprocess.Thismeansthat,ifyourserviceisgoingtodoanyCPUintensive(suchasMP3playback)orblocking(suchasnetworking)operations,itshouldspawnitsownthreadinwhichtodothatwork.MoreinformationonthiscanbefoundinProcessesandThreads.TheIntentServiceclassisavailableasastandardimplementationofServicethathasitsownthreadwhereitschedulesitsworktobedone.
在中文中,他的表述是服务。Android付与了Service比处于不举动形态的举动更高的优先级,如许,在体系哀求资本的时分,他们被停止的大概行更小。现实上,假如运转时过早的停止一个已启动的服务,那末只需有充足的资本可用,则运转时就会从头启动它。在极度情形中,服务的停止将会明显的影响用户体验,从而招致软件计划上的UI与UE的互动缺掉,在这些情形中,能够把服务的优先级提拔到与前台的举动不异的地位。经由过程利用服务,能够包管使用程序延续的运转,并对事务作出呼应,即便他们被自动地利用也是此。
Service运转时没有专门的GUI,可是,与举动和播送吸收器一样,他们仍旧应当在使用程序历程的主历程实行。上面将为人人扼要先容一下Android中利用Java进线程挪用的利用办法。
1.创立一个线程
[java]viewplaincopy
/**
*@CopyrightbyAlfredZ.Zheng,Cindigo.2011-09.Wuhan.
*AninnovativeclubinHuazhongUniv.ofSci.&Tech.
*Prj.name:org.cindigo.javatestthread1
*/
classMythreadimplementsRunnable{
//Mythread完成了Runnable
intcount;
StringthrdName;
Mythread(Stringname){
count=0;
thrdName=name;
}
publicvoidrun(){
//线程运转出发点
System.out.println(thrdName+"startathread.");
try{
do{
Thread.sleep(1000);
System.out.println("@At"+thrdName+",resultis"+count);
count++;
}while(count<10);
}
catch(InterruptExceptionexc){
System.out.println(thrdName+"interrupted.");
}
System.out.println(thrdName+"timingup!");
}
}
classUseThreads{
publicstaticvoidmain(Stringargs[]){
System.out.println("Startingamainthread...");
//创立一个可运转的对象
MyThreadmyth=newMyThread("ActivityNo.1");
//在该对象上机关一个线程
Threadnewth=newThread(myth);
newth.start();
do{
System.out.println("ActivityNo.2");
try{
Thread.sleep(500);
}catch(InterruptExceptionexc){
System.out.println("ActivityNo.3");
}
}while(myth.count!=10);
System.out.println("ActivityNo.4");
}
}
2.创立多个线程
[java]viewplaincopy
/**
*@CopyrightbyAlfredZ.Zheng,Cindigo.2011-09.Wuhan.
*AninnovativeclubinHuazhongUniv.ofSci.&Tech.
*Prj.name:org.cindigo.javatestthread2
*/
//创立多重线程
classMyThreadimplementsRunnable{
intcount;
Threadthrd;
//创建新线程
MyThread(Stringname){
thrd=newThread(this,name);
count=0;
thrd.start();//开启线程
}
//开启新的主线程
publicvoidrun(){
System.out.println(thrd.getName()+"starting.");
try{
do{
Thread.sleep(500);
System.out.println("In"+thrd.getName()+",countis"+count);
count++;
}while(count<10);
}
catch(InterruptExceptionexc){
System.out.println(thrd.getName()+"interrupted.");
}
System.out.println(thrd.getName()+"timingup!");
}
}
classUseThreadsImpoved{
publicstaticvoidmain(Stringarg[]{
System.out.println("Mainthreadstarting.");
//如今线程在创立时启动
Mythreadmyth=newMyThread("MyActivityNo.1");
do{
System.out.println(".");
try{
Thread.sleep(1000);
}
catch(InterruptExceptionexc){
System.out.println("MyActivityNo.2");
}
}while(myth.count!=10);
System.out.println("MyActivityNo.2");
}
}
上面为人人讲一下AndroidService组件的创立并开端的利用的办法。起首为人人扼要先容一下Service的性命周期。
onBind(Intentintent):是Service完成功效的最主要的办法,挪用时会前往一个绑定的接口给Service。
onCreate():当Service初度创立时被体系主动挪用。
onStart(Intentintent,intstartid):当startService()办法启动Service时,这个办法会被主动挪用。
onDestroy():停止Service时,体系会挪用这个办法。
1.创立一个Service
[java]viewplaincopy
/**
*@Copyright(R)Cindigo,HUST,Wuhan.
*Prj.:org.cindigo.jetaime
*/
importandroid.app.Service;
importandroid.content.Intent;
importandroid.os.Binder;
importandroid.os.IBinder;
importandroid.util.Log;
importandroid.widget.Toast;
publicclassMyServiceextendsService{
privatestaticfinalStringTAG="CONNECTION";
privatestaticfinalbooleanJudge=true;
publicIBinderonBind(Intentintent){
Log.i(TAG,"BindSuccess!");
}
@Override
publicvoidonCreate(){
Toast.makeText(this,"MyServiceCreated",Toast.LENGTH_SHORT).show();
if(Judge)
Log.d(TAG,"onCreate");
}
@Override
publicvoidonStart(Intentintent,intstartid){
Toast.makeText(this,"MyServiceStarted",Toast.LENGTH_SHORT).show();
if(Judge)
Log.d(TAG,"onStart");
}
@Override
publicvoidonDestroy(){
Toast.makeText(this,"MyServiceStopped",Toast.LENGTH_SHORT).show();
if(Judge)
Log.d(TAG,"onDestroy");
}
}
2.启动和中断线程
[java]viewplaincopy
/**
*@Copyright(R)Cindigo,HUST,Wuhan.
*Prj.:org.cindigo.jetaime
*/
importandroid.app.Service;
importandroid.content.Intent;
//创立一个Intent
Intentintent=newIntent();
//设置举措属性
intent.setAction("logging1...");
//启动这个Service
startService(intent);
//停止一个Service
stopService(intent);
3.绑定息争绑一个已存在的Service
[java]viewplaincopy
/**
*@Copyright(R)Cindigo,HUST,Wuhan.
*Prj.:org.cindigo.jetaime
*/
importandroid.content.Intent;
importandroid.content.ServiceConnection;
importandroid.content.SharedPreferences;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.IBinder;
importandroid.util.Log;
publicvoidBindService(){//绑定服务
if(!mIsBound){
bindService(newIntent(this,
ConnectService.class),mConnection,Context.BIND_AUTO_CREATE);
mIsBound=true;
}
}
publicvoidUnbindService(){//解绑服务
if(mIsBound){
unbindService(mConnection);
mIsBound=false;
}
}
privateServiceConnectionmConnection=newServiceConnection(){//服务毗连对象
publicvoidonServiceConnected(ComponentNameclassName,IBinderservice){
mConnectService=((ConnectService.LocalBinder)service).getService();//取得已绑定的服务
mConnectService.GetServer(LOGON_HOST,LOGON_PORT);
mConnectService.connect();
}
publicvoidonServiceDisconnected(ComponentNameclassName){
mConnectService=null;
Log.i("Q","EEE");
}
}
4.使用实例(参考《Android使用开辟详解》)
4.1MainActivity.java文件
[java]viewplaincopy
/**
*@Copyright(R)Cindigo,HUST,Wuhan.
*Prj.:org.cindigo.jetaime
*/
importandroid.app.Activity;
importandroid.app.Service;
importandroid.content.ComponentName;
importandroid.content.Intent;
importandroid.content.ServiceConnection;
importandroid.os.Bundle;
importandroid.os.IBinder;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.Toast;
publicclassMainActivityextendsActivity{
//声明Button
privateButtonstartBtn,stopBtn,bindBtn,unbindBtn;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//设置以后结构视图
setContentView(R.layout.main);
//实例化Button
startBtn=(Button)findViewById(R.id.startButton01);
stopBtn=(Button)findViewById(R.id.stopButton02);
bindBtn=(Button)findViewById(R.id.bindButton03);
unbindBtn=(Button)findViewById(R.id.unbindButton04);
//增加监听器
startBtn.setOnClickListener(startListener);
stopBtn.setOnClickListener(stopListener);
bindBtn.setOnClickListener(bindListener);
unbindBtn.setOnClickListener(unBindListener);
}
//启动Service监听器
privateOnClickListenerstartListener=newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//创立Intent
Intentintent=newIntent();
//设置Action属性
intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
//启动该Service
startService(intent);
}
};
//中断Service监听器
privateOnClickListenerstopListener=newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//创立Intent
Intentintent=newIntent();
//设置Action属性
intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
//启动该Service
stopService(intent);
}
};
//毗连对象
privateServiceConnectionconn=newServiceConnection(){
@Override
publicvoidonServiceConnected(ComponentNamename,IBinderservice){
Log.i("SERVICE","毗连乐成!");
Toast.makeText(MainActivity.this,"毗连乐成!",Toast.LENGTH_LONG).show();
}
@Override
publicvoidonServiceDisconnected(ComponentNamename){
Log.i("SERVICE","断开毗连!");
Toast.makeText(MainActivity.this,"断开毗连!",Toast.LENGTH_LONG).show();
}
};
//定Service监听器
privateOnClickListenerbindListener=newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//创立Intent
Intentintent=newIntent();
//设置Action属性
intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
//绑定Service
bindService(intent,conn,Service.BIND_AUTO_CREATE);
}
};
//排除绑定Service监听器
privateOnClickListenerunBindListener=newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//创立Intent
Intentintent=newIntent();
//设置Action属性
intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
//排除绑定Service
unbindService(conn);
}
};
}
4.2MyService.java文件
[java]viewplaincopy
/**
*@Copyright(R)Cindigo,HUST,Wuhan.
*Prj.:org.cindigo.jetaime
*/
importandroid.app.Service;
importandroid.content.Intent;
importandroid.os.IBinder;
importandroid.util.Log;
importandroid.widget.Toast;
/**
*@author郭宏志
*测试Service
*/
publicclassMyServiceextendsService{
//能够前往null,一般前往一个有aidl界说的接口
publicIBinderonBind(Intentintent){
Log.i("SERVICE","onBind.");
Toast.makeText(MyService.this,"onBind.",Toast.LENGTH_LONG).show();
returnnull;
}
//Service创立时挪用
publicvoidonCreate(){
Log.i("SERVICE","onCreate.");
Toast.makeText(MyService.this,"onCreate.",Toast.LENGTH_LONG).show();
}
//当客户端挪用startService()办法启动Service时,该办法被挪用
publicvoidonStart(Intentintent,intstartId){
Log.i("SERVICE","onStart.");
Toast.makeText(MyService.this,"onStart.",Toast.LENGTH_LONG).show();
}
//当Service不再利用时挪用
publicvoidonDestroy(){
Log.i("SERVICE","onDestroy.");
Toast.makeText(MyService.this,"onDestroy.",Toast.LENGTH_LONG).show();
}
}
iPhoneSDK安装,然后最基本的是你要熟悉C语言,再来你得学习开发iPhone所使用的Objective-C语言,接著是Cocoa。如果你是Mac平台开发的入门用户 |
|