This issue previously discussed, the blog is also written so each document, but it was also said that even unbindService up, service will still continue to run.
But just to verify, after unbindService, onDestroy method of execution of the service, service stopped.
Here is a count function to achieve through the servcie, and shows the activity in this count
An Interface ICountService.java, only a count value returns function declaration
package com.min.localservicedemo;
public interface ICountService {
public abstract int getCount ();
}
2 service class CountService.java, for the realization of the current count
counting function, executed in a new thread, and update the view display
package com.min.localservicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class CountService extends Service implements ICountService {
private static final String TAG = "CountService";
private boolean threadDisable;
private int count;
private ServiceBinder serviceBinder = new ServiceBinder ();
public class ServiceBinder extends Binder implements ICountService {
public int getCount () {
/ / TODO Auto-generated method stub
return count;
}
}
public int getCount () {
/ / TODO Auto-generated method stub
return count;
}
@ Override
public IBinder onBind (Intent arg0) {
/ / TODO Auto-generated method stub
return serviceBinder;
}
@ Override
public void onCreate () {
/ / TODO Auto-generated method stub
super.onCreate ();
new Thread (new Runnable () {
public void run () {
/ / TODO Auto-generated method stub
while (! threadDisable) {
try {
Thread.sleep (1000);
} Catch (InterruptedException e) {
e.printStackTrace ();
}
count + +;
Log.d (TAG, "count is" + count);
updateCount ();
}
}
}
.) Start ();
}
@ Override
public void onStart (Intent intent, int startId) {
/ / TODO Auto-generated method stub
super.onStart (intent, startId);
}
@ Override
public boolean onUnbind (Intent intent) {
/ / TODO Auto-generated method stub
return super.onUnbind (intent);
}
@ Override
public void onDestroy () {
/ / TODO Auto-generated method stub
super.onDestroy ();
threadDisable = true;
Log.d (TAG, "onDestroy");
}
/ **
* Update view
* /
public void updateCount () {
. LocalServiceDemo.getMyHandler () sendEmptyMessage (1);
}
}
3.Activity class LocalServiceDemo.java, displays the current count value
package com.min.localservicedemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
public class LocalServiceDemo extends Activity {
private static final String TAG = "LocalServiceDemo";
private static TextView tvCount;
private static ICountService countService;
private static MyHandler myHandler = MyHandler.getInstance ();
private ServiceConnection serviceConnection = new ServiceConnection () {
public void onServiceConnected (ComponentName name, IBinder service) {
/ / TODO Auto-generated method stub
countService = (ICountService) service;
Log.d (TAG, "onServiceConnected count is" + countService.getCount ());
tvCount.setText ("Count:" + countService.getCount ());
}
public void onServiceDisconnected (ComponentName name) {
/ / TODO Auto-generated method stub
countService = null;
}
};
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
tvCount = (TextView) findViewById (R.id.tvCount);
this.bindService (new Intent ("com.min.localservicedemo.CountService"),
serviceConnection, BIND_AUTO_CREATE);
}
@ Override
protected void onDestroy () {
/ / TODO Auto-generated method stub
super.onDestroy ();
this.unbindService (serviceConnection);
Log.d (TAG, "onDestroy unbindService");
}
public static class MyHandler extends Handler {
private static MyHandler myHandler = null;
private MyHandler () {
}
/ **
* Single Instance
* @ Return
* /
public static MyHandler getInstance () {
if (myHandler == null) {
myHandler = new MyHandler ();
}
return myHandler;
}
@ Override
public void handleMessage (Message msg) {
/ / TODO Auto-generated method stub
if (msg.what == 1) {
tvCount.setText ("Count:" + countService.getCount ());
}
super.handleMessage (msg);
}
}
public static MyHandler getMyHandler () {
return myHandler;
}
}
4.log output
03-26 01:33:16.710: INFO / ActivityManager (59): Starting activity: Intent {
act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10200000
cmp = com.min.localservicedemo / .LocalServiceDemo}
03-26 01:33:17.480:
DEBUG / LocalServiceDemo (349): onServiceConnected count is 0
03-26 01:33:17.580:
INFO / ActivityManager (59): Displayed activity com.min.localservicedemo / .LocalServiceDemo:
666 ms (total 666 ms)
03-26 01:33:18.329: DEBUG / CountService (349): count is 1
03-26
01:33:19.388: DEBUG / CountService (349): count is 2
03-26 01:33:20.453: DEBUG / CountService
(349): count is 3
03-26 01:33:21.506: DEBUG / CountService (349): count is 4
03-26
01:33:22.573: DEBUG / CountService (349): count is 5
03-26 01:33:22.850: DEBUG / dalvikvm (127):
GC_EXPLICIT freed 227 objects / 11056 bytes in 168ms
03-26 01:33:23.612:
DEBUG / CountService (349): count is 6
03-26 01:33:24.671: DEBUG / CountService (349): count is
7
03-26 01:33:25.738: DEBUG / CountService (349): count is 8
03-26 01:33:26.804:
DEBUG / CountService (349): count is 9
03-26 01:33:27.871: DEBUG / CountService (349): count is
10
03-26 01:33:28.531: WARN / KeyCharacterMap (349): No keyboard for id 0
03-26 01:33:28.531:
WARN / KeyCharacterMap (349): Using default keymap: / system / usr / keychars / qwerty.kcm.bin
03-26
01:33:28.871: DEBUG / CountService (349): count is 11
03-26 01:33:29.231:
DEBUG / LocalServiceDemo (349): onDestroy unbindService
03-26 01:33:29.340:
DEBUG / CountService (349): onDestroy
03-26 01:33:29.941: DEBUG / CountService (349): count is
12
03-26 01:34:54.540: DEBUG / SntpClient (59): request time failed: java.net.SocketException:
Address family not supported by protocol
Log Screenshot
You can see in the log, when exit activity, performed unbindService
onDestroy functionAnd then execute the CountService class onDestroy, this time on the end of service.
This is what I understand, if after unbindService, service can continue to exist, then it is my understanding there are deviations.
Welcome to express their views.
Reply:
When my last interview, the interviewer said to me later heard unbindService, service can continue to perform.
Could it be that the kind of remote service AIDL way?
Reply:
This is the configuration AndroidManifest.xml file, there is a need to run a friend can directly copy
package = "com.min.localservicedemo"
android: versionCode = "1"
android: versionName = "1.0">android: label = "@ string / app_name">
Reply:
If it is not created by the service bindService (but still got through bindService service object), it could still running after unbindService, otherwise it should be the end off.
Reply:
Ah.
By bindService started service after unbindService should be able to continue to run service, go to the interview today on this issue has been despised.
In the android player source in this usage seems to have, I look up, and this is not the source of the consequences of attention!
Reply:
Reply:
android comes with the music source
http://android.git.kernel. ? org / p = platform / packages / apps / Music.git; a = tree
AIDL looked under way is to start remote service class MediaPlaybackService.java
SERVICE http://android.git.kernel.org/?p=platform/packages/apps/Music.git; a = blob; f = src / com / android / music / MediaPlaybackService.java; h = 7c0bbedbedef49bf4c38ef97fdcd0f1017bf5d35; hb = HEAD
Not had time to look carefully, is not by getting to the remote service after the activity, bindService, so that even after unbindService, or you can continue to run the service to play music?
Reply:
BJ's landlord is what I wish you to find a good job. . For such a comprehension test interview questions I am the egg pain. . .
Reply:
Yes, ah, thank you. Saturday to Baidu interview over the written tests, written tests, such as the one in the afternoon to finish at six before the turn of the interview, the result is less than 10 minutes to die on the interview, I'm doing music player is really too much problem.
Reply:
Reply:
Oh, I suggest you to google the interview, Baidu those technologies not ah
Reply:
Is still good to do two android, raise the level, rather than as they are now only know fur.
So it is easy to re-enter the leading companies, and now most of the opportunity is wasted.
Back to the topic, it can also continue to run the service after unbindService initiated by the remote service aidl it
service and activity belong to different processes in it?
Is a service belongs to server program, activity belongs to client-side?
Aidl read some examples, have not completely understood.
Reply:
BindService by a service bound to start counting when the count is 0, service would destroy it.
App of the same service and activity belong to the same process, but also the same thread, but can be changed by setting properties.
You look under docs / guide / topics / fundamentals / services.html this description of it. I just put this reading, I did not use the app in just a few months to start android.
Reply:
Thanks, previously seen bindService have a count to determine if there is no caller, but did not specifically used, or memory without profound ah.
Reply:
aidl just a stub, the equivalent of the agent, and service does not matter. Does not affect the life cycle of services.
service and activity in the same process can also be absent. It depends on the definition of AndroidManifest.xml inside.
service seems to have no relationship with the server, right? The former is a system concept, which means most of the c / s this structure.
Reply:
Thank you, that is controlled by how bindService start service does not end after unbindService service, let the service continue to run it?
Reply:
This is hard for me, but also did not think any way. Because this is the android system decided to end the service, in the solution after all connections are tied.
Reply:
Thank you.
android comes with the music source is also the first startService, then perform bindService of
This is defined in the binding function MusicUtils.java bindToService, first startService, then bindService.
public static ServiceToken bindToService (Activity context, ServiceConnection callback) {
Activity realActivity = context.getParent ();
if (realActivity == null) {
realActivity = context;
}
ContextWrapper cw = new ContextWrapper (realActivity);
cw.startService (new Intent (cw, MediaPlaybackService.class));
ServiceBinder sb = new ServiceBinder (callback);
if (cw.bindService ((new Intent ()). setClass (cw, MediaPlaybackService.class), sb, 0)) {
sConnectionMap.put (cw, sb);
return new ServiceToken (cw);
}
Log.e ("Music", "Failed to bind to service");
return null;
} Reply:
Indeed the first startService before bindService, when the end of the activity, after unbindService service can continue to run.
This example will only need to add a startService can
tvCount = (TextView) findViewById (R.id.tvCount);
startService (new Intent ("com.min.localservicedemo.CountService"));
this.bindService (new Intent ("com.min.localservicedemo.CountService"),
serviceConnection, BIND_AUTO_CREATE);
Reply:
This lack of
Reply:
There are three cases: if the direct use of the service, there is no need for binding, but if you want to use the service inside the method will have to be binding. Under specific circumstances start:
1 When activated, a separate call bindService method after unbindService, will run the service's onUnbind, in the implementation of onDestroy methods.
2 When activated, the first call startService, after calling bindService method after unbindService, will run the service in onUnbind, not execute onDestroy methods. Unless you perform stopService.
3 first call startService, calling stopService, will run the service's onDestroy method.
Reply:
unbindservice only once. . Very strange. . .
Reply:
I was so understandable
Reply:
Thank you very much, being as bindService worry about it.
Reply:
Good, look.
Reply:
Large cattle were too hung. Worth learning. .
Reply:
Thank you very much
Reply:
Although the service does not stop after unbind, but the service will re-create, this should be how to solve ah
No comments:
Post a Comment