Newbe需要指向正确的方向

时间:2010-12-14 21:01:20

标签: java android android-intent

再次,你好 现在已经弯下腰让这个工作了3天。 当我在“共享”菜单中选择图片时,serviceBinder始终为NULL。 我用LogCat调试它,踩代码..并且没有看到明显的错误。 bindService也返回false。我有一个服务类,但认为hickup在这里的某个地方。 请指出我正确的方向!

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扩展了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 {

}

}

1 个答案:

答案 0 :(得分:0)

问题就在这里:

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

绑定到服务是异步的。在调用bindService()返回后,不能保证绑定。