【问题标题】:Newbe need to be Pointed in the right direction纽贝需要指出正确的方向
【发布时间】:2010-12-14 21:01:19
【问题描述】:

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

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();
 }
}

包 com.example.ptpp;

公共类 MyService 扩展服务 {

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 {

}

}

【问题讨论】:

  • 很抱歉格式不好,但我把它放进去时很好。我真的很讨厌 android,但 Java 对我来说并不陌生

标签: java android android-intent


【解决方案1】:

问题就在这里:

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

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

【讨论】:

  • 服务内部发生了什么,我认为服务是同步的。
  • 服务内部运行的代码在主线程上运行,但服务的创建是异步的。这正是有 onBind() 回调的原因。
  • 我的计划是当我启动手机并拍照时。图片被默默地发送到我家的电脑上。就像一个备份的想法..!图片也正常保存在手机上。我认为这是一个不错的小项目。如果服务或其他东西更好用,必须把我的脑袋转过来
  • Service 中发生了什么,它是否像一个线程,我必须让它保持忙碌,否则它会死掉?如您所见,我确实这样做了,所以也许这就是 serviceBinder=null 的原因
  • 嗯,我不知道什么是最好用的。不过,您的问题的答案是您必须在绑定服务之前等待回调。
猜你喜欢
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
相关资源
最近更新 更多