本帖最后由 maverickgoose 于 2010-10-14 22:53:46 编辑

解决方案 »

  1.   

    没用过bindService这种方式启service,用这种方式启动的service会随着activity的销毁而销毁
      

  2.   


    整个过程中我的activity始终都处于前端,应该不是这个。继续期待...
      

  3.   


    我可以表达的不是很完整为了清楚的看到Service是如何运作的,我在每个回调函数中都输出字串来跟踪我现在是只调用bindService()方法,而 没有 调用unBindService()
    但我的LOG中依旧显示的是onCreate()-->onBind()-->onUnbind()-->onDestroy()
    按正常来说,后两个方法是不会自动调用的奇怪就奇怪在这里
      

  4.   

    另外我把Activity的代码也帖出来,希望有哪位可以帮忙看看package sgtmav.test.helloworld;import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    public class firstandroidproject extends Activity implements OnClickListener {    /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            System.out.println("onCreate()");
            
            Button button1 = (Button)findViewById(R.id.button1);
            Button button2 = (Button)findViewById(R.id.button2);
            Button button3 = (Button)findViewById(R.id.button3);
            Button button4 = (Button)findViewById(R.id.button4);
            
            button1.setOnClickListener(this);
            button2.setOnClickListener(this);
            button3.setOnClickListener(this);
            button4.setOnClickListener(this);
                    
            
        }
    @Override
    protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    System.out.println("onStart()");

    }
    @Override
    protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    System.out.println("onResume()");

    }
    @Override
    protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    System.out.println("onPause()");

    }
    @Override
    protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    System.out.println("onStop()");

    }
    @Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    System.out.println("onDestroy()");

    } @Override
    protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    System.out.println("onRestart()");

    }
    public void onClick(View v) {
    // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setClass(this, TService.class);
            
         ServiceConnection sconn = new ServiceConnection() {
        
         public void onServiceDisconnected(ComponentName name) {
         // TODO Auto-generated method stub
         //System.out.println("sconn-Disconnected");
         }
        
         public void onServiceConnected(ComponentName name, IBinder service) {
         // TODO Auto-generated method stub
         //System.out.println("sconn-Connected");
         }
         };

    switch (v.getId()) {
    case R.id.button1:
    startService(intent);
    break;
    case R.id.button2:
    stopService(intent);
    break;
    case R.id.button3:
    bindService(intent, sconn, Context.BIND_AUTO_CREATE);
    case R.id.button4:
    unbindService(sconn);
    default:
    break;
    }

    }
    }
      

  5.   

    我自己找到原因啦,丢人哇switch--case中bindService后忘了break
    结贴。
      

  6.   

    还是不能结贴啊 还以为到此就结束了呢 没想到unbindService(sconn)这样又有问题了按照我5楼sconn那样实例化,unbindService(sconn)会异常,说我没有注册Service实际上我在AndroidManifest.xml中是注册了的,又要头大了,求解?!错误代码如下:10-15 14:46:12.114: ERROR/AndroidRuntime(983): ERROR: thread attach failed
    10-15 14:46:15.956: ERROR/AndroidRuntime(994): ERROR: thread attach failed
    10-15 14:46:40.684: ERROR/AndroidRuntime(1001): Uncaught handler: thread main exiting due to uncaught exception
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001): java.lang.IllegalArgumentException: Service not registered: sgtmav.test.helloworld.firstandroidproject$1@43b96408
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.app.ActivityThread$PackageInfo.forgetServiceDispatcher(ActivityThread.java:930)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.app.ApplicationContext.unbindService(ApplicationContext.java:819)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.content.ContextWrapper.unbindService(ContextWrapper.java:342)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at sgtmav.test.helloworld.firstandroidproject.onClick(firstandroidproject.java:129)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.view.View.performClick(View.java:2364)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.view.View.onTouchEvent(View.java:4179)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.view.View.dispatchTouchEvent(View.java:3709)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.os.Handler.dispatchMessage(Handler.java:99)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.os.Looper.loop(Looper.java:123)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at android.app.ActivityThread.main(ActivityThread.java:4363)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at java.lang.reflect.Method.invokeNative(Native Method)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at java.lang.reflect.Method.invoke(Method.java:521)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    10-15 14:46:40.715: ERROR/AndroidRuntime(1001):     at dalvik.system.NativeStart.main(Native Method)
      

  7.   

    ServiceConnection sconn = new ServiceConnection() {
    不能作为方法里的局部变量,应该定义为类变量。否则每次按钮onClick都会产生新对象
      

  8.   

    在MAANIFEST。xml里  如何加权限啊?