1

好的,我已经查看了很多问题,但无法找到解决方案...

不断收到“java.lang.IllegalArgumentException:服务未注册:”

请注意,没有调用 ServiceConnection 的“onServiceConnected”方法....

这就是我所拥有的(忽略所有Log.e ..我真的不知道有什么其他方法可以找出哪些函数在没有调试的情况下被调用):

如何在清单中声明服务:

<service android:name="com.example.servicestry.MyService" android:enabled="true"/> 

我的活动

public class MainActivity extends Activity {
    MyService mBoundService=null;


    public ServiceConnection mConnection;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mConnection=new ServiceConnection(){

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                // TODO Auto-generated method stub 
                Log.e("onserviceconnected","");
                mBoundService = ((MyService.LocalBinder)service).getService();

                Toast.makeText(getApplicationContext(), "local service connected",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub
                Log.e("onservicedisconnected","");
                mBoundService=null; 
                Toast.makeText(getApplicationContext(), "local service disconnected",Toast.LENGTH_SHORT).show();

            }

        };

        this.doBindService();

        Timer timer=new Timer();
        timer.schedule(new RemindTask(), 3000);

    } 

    public void doUnbindService(){
        Log.e("doUnbindService","");
        unbindService(mConnection);
    }
    public void doBindService(){


        boolean b=bindService(new Intent(this,MyService.class),mConnection, Context.BIND_AUTO_CREATE);
        if(b){
            Toast.makeText(this, "binding service", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(this, "binding service FAILED", Toast.LENGTH_SHORT).show();
        }

        this.doUnbindService();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    public class RemindTask extends TimerTask{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(), "this is runner", Toast.LENGTH_SHORT).show();
            Log.e("runner run","r");
            doUnbindService();

        }

    }
}

我的服务等级

包 com.example.servicestry;

public class MyService extends Service{
    LocalBinder mBinder= new LocalBinder();;
    @Override
    public void onCreate(){
        super.onCreate();

        Log.e("binder create","ASDF");


    } 

    @Override
    public int onStartCommand(Intent i,int flag, int startId){
        Log.e("on start command","D");
        return super.onStartCommand(i, flag, startId);
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.e("DEST","");
    }

    @Override
    public IBinder onBind(Intent intent) {

        // TODO Auto-generated method stub
        Log.e("onbind","ASDF");


        return mBinder;

    }

    public class LocalBinder extends Binder {
        MyService getService() {


            return MyService.this;
        }
    }




}
4

0 回答 0