Äú¿ÉÒÔ¾èÖú£¬Ö§³ÖÎÒÃǵĹ«ÒæÊÂÒµ¡£

1Ôª 10Ôª 50Ôª





ÈÏÖ¤Â룺  ÑéÖ¤Âë,¿´²»Çå³þ?Çëµã»÷Ë¢ÐÂÑéÖ¤Âë ±ØÌî



  ÇóÖª ÎÄÕ ÎÄ¿â Lib ÊÓÆµ iPerson ¿Î³Ì ÈÏÖ¤ ×Éѯ ¹¤¾ß ½²×ù Modeler   Code  
»áÔ±   
 
   
 
 
     
   
 ¶©ÔÄ
  ¾èÖú
°²×¿µÚÊ®ÈýÌì±Ê¼Ç-·þÎñ(Service)
 
×÷ÕߣºÉ­ÁÖÉ­ À´×ÔÓÚ£º²©¿ÍÔ° ·¢²¼ÓÚ 2016-3-29
  2545  次浏览      27
 

1.·þÎñ¸ÅÄî

·þÎñ

windows

·þÎñûÓнçÃæ£¬Ò»Ö±ÔËÐÐÔÚºǫ́£¬ ÔËÐÐÔÚ¶ÀÁ¢µÄÒ»¸ö½ø³ÌÀïÃæ

android

·þÎñûÓнçÃæ£¬Ò»Ö±ÔËÐÐÔÚºǫ́£¬Ä¬ÈÏÊÇÔËÐе±Ç°µÄÓ¦ÓóÌÐò½ø³ÌÀïÃæ¡£

2.½¨Á¢·þÎñ

½¨Á¢Ò»¸öÀà¼Ì³ÐServiceÀà

public class ServiceDemo extends Service {

ÔÚÇåµ¥ÎļþÖÐ×¢²áservice

<service android:name="com.ithiema.servicequick.servcie.ServiceDemo"></service>

3.ÉúÃüÖÜÆÚ

Ö»»áÔÚ¿ªÆô·þÎñʱ³õʼ»¯Ò»´Î

@Override
public void onCreate() {
//Ö»»áÔÚ¿ªÆô·þÎñʱ³õʼ»¯Ò»´Î
super.onCreate();
}

ÿ´Î¿ªÆô·þÎñ¶¼»áÖ´Ðе÷ÓÃ

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//ÿ´Î¿ªÆô·þÎñ¶¼»áÖ´Ðе÷ÓÃ
return super.onStartCommand(intent, flags, startId);
}

Í£Ö¹·þÎñʱ,Ö»Ö´ÐÐÒ»´Î

public void onDestroy() {
//Í£Ö¹·þÎñʱ,Ö»Ö´ÐÐÒ»´Î
super.onDestroy();
}

ÍêÕûÉúÃüÖÜÆÚ

onCreate -- onStartCommand--onDestroy

Æô¶¯¶à´Î·þÎñ

onCreate·½·¨Ö»»áÖ´ÐÐÒ»´Î£¬ µ«ÊÇonStartCommandÖ´Ðжà´Î

¶à´ÎÍ£Ö¹·þÎñ

Ö»»áÖ´ÐÐÒ»´ÎonDestroy·½·¨¡£

4.½ø³Ì

Foreground process

ǰ̨½ø³Ì£º µ±Ç°ÓëÓû§½øÐн»»¥µÄÓ¦ÓÃËù´¦µÄ½ø³Ì

Visible process

¿É¼û½ø³Ì£º Ó¦ÓóÌÐò²»Äܽ»»¥£¬µ«ÊǽçÃæ¿É¼û¡£ ÓеãÀàËÆactivityÉúÃüÖÜÆÚµÄonPause

Service process

·þÎñ½ø³Ì£º Ó¦ÓóÌÐòÀïÃæÔËÐÐ×ÅÒ»¸ö·þÎñ

Background process

ºǫ́½ø³Ì: Ó¦ÓóÌÐò±»×îС»¯ÁË(home)

Empty process

¿Õ½ø³Ì£ºÓ¦ÓóÌÐòÀïÃæÃ»ÓÐÈκλµÄ×é¼þ£¨activity \ service£©

ǰ̨½ø³Ì > ¿É¼û½ø³Ì > ·þÎñ½ø³Ì > ºǫ́½ø³Ì > ¿Õ½ø³Ì

5.¿ªÆôÓëÍ£Ö¹·þÎñ

public void start(View v){
startService(new Intent(this , ServiceDemo.class));
} public void stop(View v){ stopService(new Intent(this , ServiceDemo.class)); }

6.ΪʲôҪʹÓ÷þÎñ

1.¿ÉÒÔÈýø³ÌËù´¦µÄÓÅÏȼ¶ÓÐËù±£ÕÏ¡£

2.¿ÉÒÔ³¤¾ÃµÄÔÚºǫִ́ÐÐÂß¼­²Ù×÷£¬¼´Ê¹ËùÓеÄactivity¶¼Ïú»ÙÁË£¬Ò²²»Ó°Ïì¡£

³¡¾°£º 1. ÔÚºǫ́¼ì²âÉ豸½ÓÈë×´¿ö 2. ÔÚºǫִ́ÐÐÁªÍøÊý¾ÝÇëÇó(ÀàËÆ¹ÉƱÈí¼þ) 3. ÒôÀÖ²¥·ÅÆ÷

7.°ó¶¨·þÎñµÚÒ»ÖÖ

Extending the Binder class µÚÒ»ÖÖ

1 ÏÈдһ¸ö¼Ì³ÐServiceÀà

2.ÔÚServiceÖÐдһ¸öpublicÀà¼Ì³ÐBinder,ÔÚÕâ¸öÀàÖÐд¸ö·½·¨·µ»Ø¾ß·þÎñÀàµÄ¶ÔÏóʵÀý

3.ÔÚonBind Öзµ»Ø Õâ¸öÄÚ²¿ÀàµÄʵÀý

4.ÔÚserviceÖÐд¼¸¸öpublicµÄ·½·¨Ìṩ¸ø×ÓÀàµ÷ÓñØÐëΪpublicµÄ

 /**
public class LocalService extends Service {
private static final String TAG = "LocalService";
//ÉùÃ÷°ó¶¨µÄÀà
private LocalBinder mBinder = new LocalBinder();
//¸ø¿Í»§¶ËʹÓÃ
private final Random mGenerator = new Random();

/*
µÚÒ»Öּ̳ÐBinderÀà
*/
public class LocalBinder extends Binder {

public LocalService getServcie() {
Log.d(TAG, "==getServcie==");
return LocalService.this;
}

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "==IBinder==");
return mBinder;
}

@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "==onCreate==");
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "==onStartCommand==");
return super.onStartCommand(intent, flags, startId);
}

/**
* ·µ»ØÒ»¸öËæ»úÊý
*
* @return
*/
public int getRandom() {
Log.d(TAG, "==getRandom==");
return mGenerator.nextInt(500);
}


@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "==onUnbind==");
return super.onUnbind(intent);
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "==onDestroy==");
}
}

5.ÔÚManifest.xmlÖÐ×¢²áservice

<service android:name=".serivice.LocalService"/>

6.ÔÚ¿Í»§¶ËµÄActivityÖаó¶¨service bindService(intnet,ServiceConnection,Context.BINDAUTOCREATE)

7.дһ¸öÀàʵÏÖServiceConnection½Ó¿Ú,ÔÚ·½·¨ÖÐÍê³É»ñÈ¡ServiceÀàµÄʵÀý

8.µ÷Ó÷þÎñÖеķ½·¨

9.½â³ý°ó¶¨

²¼¾Ö

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:onClick="bind1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="°ó¶¨·þÎñ1"/>


<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:onClick="show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="µ÷Ó÷½·¨"/>

<Button
android:onClick="unBind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="½â°ó°ó¶¨·þÎñ1"/>
</LinearLayout>

¿Í»§¶ËÀà

Activity

/**
* °ó¶¨·þÎñ
*/
public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity" ;
//·þÎñ
private LocalService mService;

private boolean mbound = false;

private MyServiceConnection mConection;


private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "=====MainActivity==onCreate=====");

tv = (TextView) findViewById(R.id.tv);
}

/**
* °ó¶¨·þÎñ
* @param view °´¼ü
*/
public void bind1(View view){
Log.d(TAG, "=====MainActivity==bind1=====");
Intent intent = new Intent(this,LocalService.class);

if(mConection==null){
mConection = new MyServiceConnection();
}

mbound = bindService(intent, mConection, Context.BIND_AUTO_CREATE);

Log.d(TAG, "=====MainActivity==flag=====" + mbound);


}

public void show(View v) {

if (mbound) {
int random = mService.getRandom();
tv.setText(random + "");
}

}


/**
* ·þÎñÁ¬½ÓÀà
*/
private class MyServiceConnection implements ServiceConnection{

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
LocalService.LocalBinder mBinder= (LocalService.LocalBinder) service;

mService = mBinder.getServcie();
Log.d(TAG, "=====MainActivity==MyServiceConnection==onServiceConnected===");


}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG,"=====MainActivity==MyServiceConnection==onServiceDisconnected===");
mbound=false;
}
}

/**
* ½â³ý°ó¶¨
* @param v
*/
public void unBind(View v){
Log.d(TAG,"=====MainActivity==unBind======");
if(mService!=null) {
unbindService(mConection);
mConection=null;

}
}

/**
* Ïú»Ù
*/
@Override
protected void onDestroy() {
Log.d(TAG,"=====MainActivity==onDestroy======");
super.onDestroy();
if(mService!=null&&mConection!=null) {
unbindService(mConection);
mConection=null;

}
}
}

ÒÔÉϵķ½Ê½Èç¹ûÖ»Òª¸ø×Ô¼º³ÌÐòÓÃͬʱ²»Òª°ÑһЩ·½·¨Ë½Óл¯µÄ»°¶¼¿ÉÒÔÔÚServiceÖÐдpublicµÄ·½·¨,ʹÓÃÕâÖÖ·½Ê½

8.°ó¶¨·þÎñµÚ¶þÖÖ

ʹÓÃMessengerÓëHandlerµÄ»úÖÆ´¦Àí,ÕâÖÖ·½Ê½ÊÇÒ»ÖÖµ¥Ï̵߳Ä,²»°²È«µÄ,ĿǰºÜÉÙÓÐÈËÓÃÁË,»áÔì³ÉÅŶÓÏÖÏó

Service

/**
* Created by Áõéª on 2016-02-28 16:29.
*/
public class MessengerService extends Service {

public static final int MSG_SAY_HELLO = 1;

Messenger mMessenger = new Messenger(new IncomingHandler());

public class IncomingHandler extends Handler{
@Override
public void handleMessage(Message msg) {
switch (msg.what){

case MSG_SAY_HELLO:
Toast.makeText(getApplicationContext(),"hello Android===handleMessage==",Toast.LENGTH_SHORT).show();


}
}
}

/**
*
* @param intent Òâͼ
* @return
*/
@Nullable //²ÎÊý¿ÉÒÔΪ¿Õ
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(getApplicationContext(), "===onBind====", Toast.LENGTH_SHORT).show();
return mMessenger.getBinder();
}
}

Çåµ¥Îļþ

×¢²á

<service android:name=".service.MessengerService"/>

¿Í»§¶Ë

public class MainActivity extends AppCompatActivity {

private Messenger mService;

private MyServiceConnection mServiceConnection;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

/**
* ½çÃæ¿É¼ûʱ,°ó¶¨Service
*/
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent();
intent.setClass(this, MessengerService.class);

if(mServiceConnection==null){
mServiceConnection = new MyServiceConnection();
}
//°ó¶¨
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}

private class MyServiceConnection implements ServiceConnection{


@Override
public void onServiceConnected(ComponentName name, IBinder service) {


mService = new Messenger(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {

}
}

/**
* µã»÷ʼþ
* @param v
*/
public void sayHello(View v) {

Message msg = Message.obtain();
msg.what=MessengerService.MSG_SAY_HELLO;
//·¢ËÍÏûÏ¢
try {
mService.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if(mService!=null&&mServiceConnection!=null) {
unbindService(mServiceConnection);
mServiceConnection=null;
}
}
}

9.ʹÓÃÆÕͨ½Ó¿ÚÍê³É

ʹÓýӿڵķ½Ê½½¨Á¢Service

½Ó¿Ú

/**
* Created by Áõéª on 2016-02-28 18:25.
* ½Ó¿Ú¶¨Òå·½·¨ÈÃServiceÖÐ×Ô¶¨ÒåµÄBinderÀ´ÊµÏÖÕâ¸ö½Ó¿Ú,ͬʱµ÷ÓÃServiceÖй«ÓÃÓë˽Óз½·¨
*/
public interface IRemoteService {

public void display(String name,int age);

public void gogo();

}

½¨Á¢Service

/**
* Created by Áõéª on 2016-02-28 18:20.
*/
public class LocalService extends Service {
//×Ô¶¨ÒåµÄIBinder¶ÔÏó
private LocalBinder mLocalBinder= new LocalBinder();


/**
* ×Ô¶¨ÒåµÄBinder
* ¶ÔÃæ½ÓÍ·µÄÄÚÏß
*/
public class LocalBinder extends Binder implements IRemoteService {
//½Ó¿ÚµÄ·½·¨
@Override
public void display(String name, int age) {
//µ÷ÓÃServiceÖеķ½·¨
show(name,age);
}
//½Ó¿ÚµÄ·½·¨
@Override
public void gogo() {
//µ÷ÓÃServiceÖеķ½·¨
go();
}
//ÕâÀïËã×Ô¶¨ÒåµÄ·½·¨
public void showData(){
Toast.makeText(getApplicationContext(),new Date().toString(),Toast.LENGTH_SHORT).show();
}
}


private static final String TAG ="LocalService" ;

@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG,"=====onBind=======");
return mLocalBinder;
}

@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"=====onCreate=======");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Log.d(TAG,"=====onStartCommand=======");
return super.onStartCommand(intent, flags, startId);
}

@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG,"=====onUnbind=======");
return super.onUnbind(intent);
}

@Override
public void unbindService(ServiceConnection conn) {
super.unbindService(conn);
Log.d(TAG, "=====unbindService=======");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "=====onDestroy=======");
}

/*
ÉÏÃæ»áµ÷ÓÃÕâ¸ö·½·¨
*/
private void show(String name,int age){
Toast.makeText(getApplicationContext(),"ÐÕÃû:"+name+"ÄêÁä:"+age,Toast.LENGTH_SHORT).show();
Log.d(TAG, "====service=show======");
}

/*
ÉÏÃæµÄBinder»áµ÷ÓÃÕâ¸ö·½·¨
*/

private void go(){
Toast.makeText(getApplicationContext(),"gogogogogogoggog",Toast.LENGTH_SHORT).show();
Log.d(TAG, "====service=go======");
}
}

²¼¾Ö¾Í5¸ö°´¼ü

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bind"
android:text="°ó¶¨bindService·þÎñ"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="callMethod"
android:text="µ÷Ó÷þÎñÖеķ½·¨"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="unBind"
android:text="½â³ýunBindService°ó¶¨"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="start"
android:text="ʹÓÃstartService¿ªÊ¼·þÎñ"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="ʹÓÃstopServiceÍ£Ö¹·þÎñ"/>
</LinearLayout>

Activity

public class MainActivity extends AppCompatActivity {
private static final String TGA ="MainActivity" ;
//Á¬½ÓµÄÀà
private MyServcieConnection mConnection;
//IBinder¶ÔÏó
private LocalService.LocalBinder mLocalBinder;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


/**
* °ó¶¨·þÎñ
*
* @param view °´¼ü
*/
public void bind(View view) {
Log.d(TGA, "========Activity===bind======");
Intent intent = new Intent();

intent.setClass(this, LocalService.class);
if(mConnection==null){
mConnection = new MyServcieConnection();
}
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

/**
* µ÷Ó÷þÎñÖеķ½·¨
*
* @param view °´¼ü
*/
public void callMethod(View view) {
Log.d(TGA,"========Activity===callMethod======");
if(mLocalBinder!=null){
//µ÷Ó÷½·¨
mLocalBinder.display("ÕÅÈý",18);
mLocalBinder.gogo();
mLocalBinder.showData();

}
}

/**
* ½â³ý°ó¶¨·þÎñ
*
* @param view °´¼ü
*/
public void unBind(View view) {
Log.d(TGA,"========Activity===unBind======");
if(mConnection!=null){
unbindService(mConnection);
mConnection=null;
mLocalBinder=null;
}
}

/**
* ¿ªÊ¼·þÎñ
*
* @param view °´¼ü
*/
public void start(View view) {
Log.d(TGA,"========Activity===start======");
Intent intent = new Intent();
intent.setClass(this, LocalService.class);
startService(intent);
}

/**
* Í£Ö¹·þÎñ
*
* @param view °´¼ü
*/
public void stop(View view) {
Log.d(TGA,"========Activity===stop======");
//Í£Ö¹·þÎñ
Intent intent = new Intent();
intent.setClass(this, LocalService.class);
stopService(intent);
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TGA, "========Activity===onDestroy======");
/* if(mConnection!=null){
unbindService(mConnection);
mConnection=null;
mLocalBinder=null;
return;
}
//Í£Ö¹·þÎñ
Intent intent = new Intent();
intent.setClass(this, LocalService.class);
stopService(intent);*/
}

/**
* ·þÎñÁ¬½ÓÀà
*/
private class MyServcieConnection implements ServiceConnection{

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TGA,"========Activity===onServiceConnected======");
mLocalBinder = (LocalService.LocalBinder) service;
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TGA,"========Activity===onServiceDisconnected======");
}
}
}

×¢²á

<!--×¢²á-->

<service android:name=".service.LocalService"/>

½á¹û

02-28 06:39:24.895 4350-4350/? D/MainActivity: ========Activity===bind======

02-28 06:39:24.968 4350-4350/? D/LocalService: =====onCreate=======
02-28 06:39:24.969 4350-4350/? D/LocalService: =====onBind=======
02-28 06:39:24.985 4350-4350/? D/MainActivity: ========Activity===onServiceConnected======
02-28 06:39:28.625 4350-4350/? D/MainActivity: ========Activity===callMethod======

02-28 06:39:28.631 4350-4350/? D/LocalService: ====service=show======
02-28 06:39:28.637 4350-4350/? D/LocalService: ====service=go======
ace 0xeb9b54a0, error=EGL_SUCCESS
02-28 06:39:30.906 4350-4350/? D/MainActivity: ========Activity===unBind======

02-28 06:39:30.937 4350-4350/? D/LocalService: =====onUnbind=======
02-28 06:39:30.937 4350-4350/? D/LocalService: =====onDestroy=======

02-28 06:39:32.721 4350-4368/? V/RenderScript: 0xe1f0e200 Launching thread(s), CPUs 4
02-28 06:39:35.827 4350-4350/? D/MainActivity: ========Activity===start======

02-28 06:39:35.846 4350-4350/? D/LocalService: =====onCreate=======
02-28 06:39:35.846 4350-4350/? D/LocalService: =====onStartCommand=======
02-28 06:39:36.878 4350-4350/? D/MainActivity: ========Activity===stop======

02-28 06:39:36.902 4350-4350/? D/LocalService: =====onDestroy=======

10.AIDL (AIDL-Android Interface Definition Language)

AIDL¿ÉÒÔͬʱ´¦Àí¶à¸öÏ̵߳ÄÇëÇó,²¢ÇÒÊÇḬ̈߳²È«µÄ.

To use AIDL directly, you must create an .aidl file that defines the programming interface. The Android SDK tools use this file to generate an abstract class that implements the interface and handles IPC, which you can then extend within your service.

ҪʹÓÃAIDL±ØÐ뽨Á¢Ò»¸öÀ©Õ¹ÃûΪ.aidlµÄÎļþ,SDK»á×Ô¶¯Éú³É¶ÔÏóµÄ³éÏóÀàͬÊÇʵÏÖ½Ó¿ÚÓëHandles IPCͨÐÅ,ÔÚServiceÖпÉÒÔÑ¡Ôñ¼Ì³Ð

ÀýÈç:Ö§¸¶±¦,¾ÍÖ»ÊDZ©Â¶ÁË,½Ó¿Ú¸øÉ̼ÒʹÓÃ,ÀïÃæ¾ßÌåÔõôʵÏÖ²¢Ã»Óб©Â¶

½«ÉÏÃæµÄ½Ó¿Úºó׺Ãû¸ÄΪ.aidl

¸ü¸ÄSeriveÖеÄ×Ô¶¨ÒåIBinder

public class LocalBinder extends IRemoteService.Stub {

¸ü¸Ä¿Í»§¶Ë ÖÐÉùÃ÷²»ÔÚдԭÀ´µÄbinder

¸ü¸ÄÕâ¸öΪ

private LocalService.LocalBinder mLocalBinder;

¸ü¸Äºó

//IRemoteService
private IRemoteService mIRemoteService;

Activity

public class MainActivity extends AppCompatActivity {
private static final String TGA ="MainActivity" ;
//Á¬½ÓµÄÀà
private MyServcieConnection mConnection;
//IRemoteService
private IRemoteService mIRemoteService;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


/**
* °ó¶¨·þÎñ
*
* @param view °´¼ü
*/
public void bind(View view) {
Log.d(TGA, "========Activity===bind======");
Intent intent = new Intent();

intent.setClass(this, LocalService.class);
if(mConnection==null){
mConnection = new MyServcieConnection();
}
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

/**
* µ÷Ó÷þÎñÖеķ½·¨
*
* @param view °´¼ü
*/
public void callMethod(View view) {
Log.d(TGA,"========Activity===callMethod======");
if(mIRemoteService!=null){
//µ÷Ó÷½·¨
try {
mIRemoteService.display("ÕÅÈý",18);
mIRemoteService.gogo();
} catch (RemoteException e) {
e.printStackTrace();
}

}
}

/**
* ½â³ý°ó¶¨·þÎñ
*
* @param view °´¼ü
*/
public void unBind(View view) {
Log.d(TGA,"========Activity===unBind======");
if(mConnection!=null){
unbindService(mConnection);
mConnection=null;
mIRemoteService=null;
}
}

/**
* ¿ªÊ¼·þÎñ
*
* @param view °´¼ü
*/
public void start(View view) {
Log.d(TGA,"========Activity===start======");
Intent intent = new Intent();
intent.setClass(this, LocalService.class);
startService(intent);
}

/**
* Í£Ö¹·þÎñ
*
* @param view °´¼ü
*/
public void stop(View view) {
Log.d(TGA,"========Activity===stop======");
//Í£Ö¹·þÎñ
Intent intent = new Intent();
intent.setClass(this, LocalService.class);
stopService(intent);
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TGA, "========Activity===onDestroy======");

}

/**
* ·þÎñÁ¬½ÓÀà
*/
private class MyServcieConnection implements ServiceConnection{

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TGA,"========Activity===onServiceConnected======");
mIRemoteService =IRemoteService.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TGA,"========Activity===onServiceDisconnected======");
}
}
}

ÕâÖÖaidlµÄ·½Ê½Ö»Äܵ÷ÓÃSerivce½Ó¿ÚÖеķ½·¨,²»Äܵ÷ÓÃServiceºÍ×Ô¶¨ÒåBinderÖÐÌØÓеķ½·¨ ³£ÓÃÓÚ¸øµÚÈý·½µÄÓ¦ÓÃÌṩ·þÎñ * ͨ¹ý

IRemoteService.Stub.asInterface(service)

·½·¨µÃµ½aidl¶¨ÒåµÄ½Ó¿ÚʵÀý

11.startServiceÓëbindServiceµÄÉúÃüÖÜÆÚ

startServiceÓëstopService

1.startService

onCreate---´´½¨·þÎñʱִֻÐÐÒ»´Î

onStartCommand ----ÿ´Î¿ªÆô·þÎñ¶¼»áµ÷ÓÃ

2.stopService

onDestory--Í£Ö¹·þÎñʱ¶à´Îµ÷ÓÃ,Ö»»áÖ´ÐÐÒ»´Î

bindServiceÓëunBindServcie

3.bindService

onCreate---´´½¨·þÎñʱִֻÐÐÒ»´Î

onBind----¶à´Îµ÷ÓÃ,Ö»Ö´ÐÐÒ»´Î

4.unBindService

onUnbind----½â³ýʱ,Ö»Ö´ÐÐÒ»´Î

onDestory----Ö»Ö´ÐÐÒ»´Î

   
2545 ´Îä¯ÀÀ       27
 
Ïà¹ØÎÄÕÂ

ÊÖ»úÈí¼þ²âÊÔÓÃÀýÉè¼ÆÊµ¼ù
ÊÖ»ú¿Í»§¶ËUI²âÊÔ·ÖÎö
iPhoneÏûÏ¢ÍÆËÍ»úÖÆÊµÏÖÓë̽ÌÖ
AndroidÊÖ»ú¿ª·¢£¨Ò»£©
 
Ïà¹ØÎĵµ

Android_UI¹Ù·½Éè¼Æ½Ì³Ì
ÊÖ»ú¿ª·¢Æ½Ì¨½éÉÜ
androidÅÄÕÕ¼°ÉÏ´«¹¦ÄÜ
Android½²ÒåÖÇÄÜÊÖ»ú¿ª·¢
Ïà¹Ø¿Î³Ì

Android¸ß¼¶Òƶ¯Ó¦ÓóÌÐò
Androidϵͳ¿ª·¢
AndroidÓ¦Óÿª·¢
ÊÖ»úÈí¼þ²âÊÔ
×îл¼Æ»®
DeepSeekÔÚÈí¼þ²âÊÔÓ¦ÓÃʵ¼ù 4-12[ÔÚÏß]
DeepSeek´óÄ£ÐÍÓ¦Óÿª·¢Êµ¼ù 4-19[ÔÚÏß]
UAF¼Ü¹¹ÌåϵÓëʵ¼ù 4-11[±±¾©]
AIÖÇÄÜ»¯Èí¼þ²âÊÔ·½·¨Óëʵ¼ù 5-23[ÉϺ£]
»ùÓÚ UML ºÍEA½øÐзÖÎöÉè¼Æ 4-26[±±¾©]
ÒµÎñ¼Ü¹¹Éè¼ÆÓ뽨ģ 4-18[±±¾©]

androidÈË»ú½çÃæÖ¸ÄÏ
AndroidÊÖ»ú¿ª·¢£¨Ò»£©
AndroidÊÖ»ú¿ª·¢£¨¶þ£©
AndroidÊÖ»ú¿ª·¢£¨Èý£©
AndroidÊÖ»ú¿ª·¢£¨ËÄ£©
iPhoneÏûÏ¢ÍÆËÍ»úÖÆÊµÏÖ̽ÌÖ
ÊÖ»úÈí¼þ²âÊÔÓÃÀýÉè¼ÆÊµ¼ù
ÊÖ»ú¿Í»§¶ËUI²âÊÔ·ÖÎö
ÊÖ»úÈí¼þ×Ô¶¯»¯²âÊÔÑо¿±¨¸æ

Android¸ß¼¶Òƶ¯Ó¦ÓóÌÐò
AndroidÓ¦Óÿª·¢
Androidϵͳ¿ª·¢
ÊÖ»úÈí¼þ²âÊÔ
ǶÈëʽÈí¼þ²âÊÔ
AndroidÈí¡¢Ó²¡¢ÔÆÕûºÏ

ÁìÏÈIT¹«Ë¾ android¿ª·¢Æ½Ì¨×î¼Ñʵ¼ù
±±¾© Android¿ª·¢¼¼Êõ½ø½×
ijÐÂÄÜÔ´ÁìÓòÆóÒµ Android¿ª·¢¼¼Êõ
ijº½Ì칫˾ Android¡¢IOSÓ¦ÓÃÈí¼þ¿ª·¢
°¢¶û¿¨ÌØ LinuxÄÚºËÇý¶¯
°¬Ä¬Éú ǶÈëʽÈí¼þ¼Ü¹¹Éè¼Æ
Î÷ÃÅ×Ó Ç¶Èëʽ¼Ü¹¹Éè¼Æ