Tuesday, May 22, 2012

Service.onUnbind() called even if there are active clients?

The point of the exercise is: keep the service alive, passing it from one activity to another.




  1. Activity A calls bindService() on service S;

  2. S.onBound() called;

  3. A.serviceConnection.onServiceConnected() is called;

  4. Activity A starts activity B;

  5. Activity B calls bindService() on service S;

  6. B.serviceConnection.onServiceConnected() is called;
    5a: from onServiceConnected() activity B calls A.finish();

  7. Activity A is stopping, calling unbindService(S) from its onDestroy() method.



Expected behavior: Service S continues to exist happily until activity B calls unbindService()



Actual behavior:




  1. S.onUnbind() is called;

  2. S.onDestroy() is called;

  3. B.serviceConnection.onServiceDisconnected() is called;



thus destroying the link and contradicting the documentation.



Why? What am I missing?



Update: Solved. From http://developer.android.com/reference/android/app/Service.html:




A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag
.




Here's the code:



public class A extends Activity {

private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

private String serviceClassName;
private ServiceConnection feedConnection;
private Messenger feedMessenger;

private void bind(String argument) {

serviceClassName = TheService.class.getName();
Intent intent = new Intent(serviceClassName);

intent.putExtra(Keys.ACCOUNT, argument);

feedConnection = new FeedConnection();

if (!bindService(intent, feedConnection, Context.BIND_AUTO_CREATE)) {
throw new IllegalStateException("Failed to bind to " + argument);
}

logger.debug("bindService(" + serviceClassName + ") successful");
}

private void forward() {

Intent intentB = new Intent();

intentB.setClassName(B.class.getPackage().getName(), B.class.getName());
intentB.putExtra(Keys.SERVICE_CLASS_NAME, serviceClassName);

startActivity(intentB);
}

@Override
protected void onDestroy() {
super.onDestroy();

unbindService(feedConnection);
}


private class FeedConnection implements ServiceConnection {

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

A.this.feedMessenger = new Messenger(service);
}

@Override
public void onServiceDisconnected(ComponentName className) {

A.this.feedMessenger = null;
logger.error("Crashed " + Integer.toHexString(hashCode()));
}
}
}

public class B extends Activity {

private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());
private ServiceConnection feedConnection;
private Messenger feedMessenger;
private A activityA;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

bindFeed();
}

private void bindFeed() {

Intent startingIntent = getIntent();

String serviceClassName = startingIntent.getStringExtra(Keys.SERVICE_CLASS_NAME);

Intent intent = new Intent(serviceClassName);

feedConnection = new FeedConnection();
// FIXME: BIND_AUTO_CREATE flag is missing
if (!bindService(intent, feedConnection, 0)) {
throw new IllegalStateException("Failed to bind to " + serviceClassName);
}

logger.debug("bindService(" + serviceClassName + ") successful");
}

private class FeedConnection implements ServiceConnection {

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

B.this.feedMessenger = new Messenger(service);

logger.debug("bound " + className);

// Finish the previous activity only after the service is bound
activityA.fileList();
}

@Override
public void onServiceDisconnected(ComponentName className) {

B.this.feedMessenger = null;
logger.error("Crashed " + className);
}
}
}




Run a cPAddon automatically on account creation?

I am working on writing my own system to automatically provision cPanel accounts upon payment.
One of my requirements is to automatically install WordPress on certain Accounts.
What would be the easiest way to do this?
My preferred scripting language is PHP...
I have the WordPress cPAddon installed on my server and I figured it may be the easiest way to do it if it is possible to invoke them via php.



Thanks in Advance





Putting contact info into Phone Book from Blackberry

In my application I need to add contact info from my app into phonebook of BlackBerry.
How can I achieve it?



I have referred to the Java development guide "Create a contact and assign it to a contact list"





Save attribute of a UIButton

I need some help with my code.
I have an UIAlert that pops up the first time that you open the app, in that pop up I have two buttons and the user will choose one of those. I want the app to remember what button the user chose to execute some code or other. The thing is I have this code right here :



-(void)changeLabel{


progressView.progress += 0.25;
scan.hidden = YES;


if (progressView.progress == 1 ) {
label.hidden = YES;

progressView.hidden = YES;

[timer invalidate];

imagesText.hidden = NO;


int randomNumber = arc4random() % 4;

switch (randomNumber) {
case 0:


imagesText.image = [UIImage imageNamed:@"image1.png"];


break;

case 1:


imagesText.image = [UIImage imageNamed:@"image2.png"];


break;

case 2:

imagesText.image = [UIImage imageNamed:@"image3.png"];

break;
case 3:

imagesText.image = [UIImage imageNamed:@"image4.png"];

default:
break;

}
}
}


So I want to make it in some way that if the user selected the first button the app will do the switch between cases 0,1 and 2 and if he selected the second button it'll do it between 3 and others. But I want the beginning of the code to be the same for both cases.
I tried some stuff but it doesn't work how I wanted.
Thank you for your help!





How to store universities and departments in database in an efficient way?

Basically I would like to create a web site about universities. My problem is trying to find an efficient way to store departments and universities in tables. For example Koc University has departments Computer Engineering, Business Administration, Economics. But Sabanci University has also Computer Engineering and Economics in its departments. I was thinking that having a table which has university ID's and department ID's but I'm not sure that it is the best idea. Do you have any ideas ?



Thanks





Javascript form won't submit

I have been using the uploader as provided by http://www.hoppinger.com/blog/2010/05/28/file-upload-progress-bar-with-phpapc-and-javascript/ and have since applied it to one of my forms and whilst the progress meter works, the submit function doesn't fire on successful upload.



The full code of what I am using is below:



JS

get_progress.php

uploader.php



As far as I can tell, in my limited experience, this is the function that handles the submit:



postUpload : function(o_data)
{
// Loop through every input and set json response in hidden input
this.a_inputs.each((function(o_input)
{
var s_name = o_input.get('name');
var s_value = '';

if(o_file = o_data.files[s_name])
{
s_value = JSON.encode(o_file);
}


var o_jsonInput = new Element('input',{'type': 'hidden','name':o_input.origName,'value':s_value}).replaces(o_input);


}).bind(this));

// Make form "original" again by purging elements and resetting attributes
this.revertSubmit();
this.o_form.submit();
},


I noticed that the submit was this.o_form.submit(); rather than this.form.submit(); and checked it out and he has declared o_form : {} at the top of the class, so I assume that his syntax is correct but I have no real idea.



Prior to my implementing this progress tracker the form worked perfectly, so this has got me quite frustrated.



Essentially what has gone wrong, I can only assume that it's something as simple as a missing ; or similar mistake.



If you get a 404 on the submit that means it worked. I have temporarily unblocked the page for troubleshooting.



As it may be relevant, my site uses WordPress.





Are there any organizations that provide training in iOS Development?

I was kinda hoping there was already an answer to this question. It seems like the iPad/iPod/iPhone/iYaddaYaddaYadda has enough of a consumer base that you would expect some organization to offer a formal training course on doing Development for them. Anybody know where you can get classes on iOS development and Objective C?