Unable to start Service Intent

前端 未结 6 1040
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 15:05

I have a service class. I have exported this class to jar and I have embed the jar in my client app.

When needed, I call the service class. When I try to do this, I

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 15:25

    I've found the same problem. I lost almost a day trying to start a service from OnClickListener method - outside the onCreate and after 1 day, I still failed!!!! Very frustrating! I was looking at the sample example RemoteServiceController. Theirs works, but my implementation does not work!

    The only way that was working for me, was from inside onCreate method. None of the other variants worked and believe me I've tried them all.

    Conclusion:

    • If you put your service class in different package than the mainActivity, I'll get all kind of errors
    • Also the one "/" couldn't find path to the service, tried starting with Intent(package,className) and nothing , also other type of Intent starting

    • I moved the service class in the same package of the activity Final form that works

    • Hopefully this helps someone by defining the listerners onClick inside the onCreate method like this:

      public void onCreate() {
      //some code......
          Button btnStartSrv  = (Button)findViewById(R.id.btnStartService);
          Button btnStopSrv  = (Button)findViewById(R.id.btnStopService);
      
          btnStartSrv.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  startService(new Intent("RM_SRV_AIDL"));
              }
          });
      
          btnStopSrv.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  stopService(new Intent("RM_SRV_AIDL"));
              }
          });
      
      } // end onCreate
      

    Also very important for the Manifest file, be sure that service is child of application:

    
        
         ...
        
        
    
            
                 
            
        
    
    

提交回复
热议问题