2010-12-14 82 views
0

嗨再次 已經讓我的頭在讓這個工作3天了。 當我在共享菜單中選擇圖片時,serviceBinder始終爲NULL。 我用LogCat進行調試,步進代碼..並且看不到明顯的錯誤。此外,bindService返回false。我有一個服務課,但認爲這個hickup就在這裏。 請指點我正確的方向!Newbe需要指向正確的方向

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延伸服務{

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

對不起格式錯誤,但它很好,當我把它。 而我真的很喜歡android,但Java對我來說並不陌生 – Erik 2010-12-14 21:04:13

回答

0

的問題就在這裏:

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

綁定到一個服務是異步的。在調用bindService()返回後,不保證被綁定。

+0

服務內部發生了什麼,我認爲服務是同步的。 – Erik 2010-12-14 21:36:19

+0

服務內部運行的代碼在主線程上運行,但服務的創建是異步的。這就是爲什麼有onBind()回調。 – Falmarri 2010-12-14 21:40:17

+0

我的計劃是當我開始手機拍照時。圖片是 默默發送到我的電腦回家。像備份思考..! 此外,圖片保存在手機上正常。我認爲這是一個不錯的小項目。如果服務或其他更好使用 – Erik 2010-12-14 21:42:59