2010-12-14 79 views
0

salut encore ont plié ma tête sur ce que cela fonctionne depuis 3 jours maintenant. Le serviceBinder est toujours NULL lorsque je sélectionne une image dans le menu Partager. Je débogue cela avec LogCat, en faisant le code .. et ne vois pas d'erreur évidente. Le bindService renvoie également false. J'ai une classe de service mais pense que le hickup est ici quelque part. S'il vous plaît pointez-moi dans la bonne direction!Newbe besoin d'être pointé dans la bonne direction

package com.example.ptpp; 

public class ServicesDemo extends Activity { 

private MyService serviceBinder = null; 
private static final String TAG = "ServicesDemo"; 
boolean mIsBound; 
ServiceConnection mConnection = null; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    startService(new Intent(this, MyService.class)); 

    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    final String mimeType = intent.getType(); 
    String action = intent.getAction(); 

    mConnection = new ServiceConnection() { 

    public void onServiceConnected(ComponentName className, IBinder service) { 
    serviceBinder = ((MyService.MyBinder) service).getService(); 
    } 
    public void onServiceDisconnected(ComponentName className) { 
    serviceBinder = null; 
    } 
    }; 

    Intent bindIntent = new Intent(ServicesDemo.this, MyService.class); 
    bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE); 
    mIsBound = true; 

    if (Intent.ACTION_SEND.equals(action)) { 
    if (extras.containsKey(Intent.EXTRA_STREAM)) { 
    Uri uri = (Uri)extras.getParcelable(Intent.EXTRA_STREAM); 
    try { 
    if(serviceBinder != null) 
    // here i call my service to do stuff with the 
             //picture i got from "share" menu 
    serviceBinder.addAttachment(mimeType, uri, false); 
    } catch (UnknownHostException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    return; 
    } else if (extras.containsKey(Intent.EXTRA_TEXT)) { 

    return; 
    } 
    } 
} 
void doUnbindService() { 
    if (mIsBound) { 
    // Detach our existing connection. 
    unbindService(mConnection); 
    mIsBound = false; 
    } 
} 

protected void onDestroy() { 
    super.onDestroy(); 
    doUnbindService(); 
} 
} 

package com.example.ptpp;

MyService public class étend son service {

private static final String TAG = "MyService"; 

private final IBinder binder = new MyBinder(); 
private ServicesDemo callingActivity = null; 

public void setActivity(ServicesDemo i) { 
    callingActivity = i; 
} 
public IBinder onBind(Intent intent) { 
    return binder; 
} 

public class MyBinder extends Binder { 
    MyService getService() { 
     return MyService.this; 
    } 
} 

public void onCreate() { 

} 

public void onDestroy() { 


} 

public void onStart(Intent intent, int startid) { 

} 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    return START_STICKY; 
} 

public void addAttachment(String mimeType, Uri uri, boolean b) throws UnknownHostException, IOException { 

} 

}

+0

désolé pour le mauvais format, mais il était agréable quand je l'ai mis po Et je suis vraiment nul à Android mais Java n'est pas nouveau pour moi – Erik

Répondre

0

Un problème est ici:

bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE); 
    mIsBound = true; 

liaison à un service est asynchrone. Vous n'êtes pas garanti d'être lié après l'appel à bindService() renvoie.

+0

Qu'est-ce qui se passe dans le service, je pensais que les services étaient synchrones. – Erik

+0

Le code exécuté dans un service est exécuté sur le thread principal, mais la création du service est asynchrone. C'est exactement pourquoi il y a le rappel onBind(). – Falmarri

+0

Mon plan est quand je commence mon téléphone et prends une photo. L'image est envoyer silencieusement à mon PC à la maison. Comme une sauvegarde pense ..! De plus, la photo est enregistrée en mode normal sur le téléphone. Genre d'un joli petit projet je pense. Doit juste emballer ma tête si les services ou quelque chose d'autre est préférable d'utiliser – Erik