Unable to start Service Intent

前端 未结 6 889
被撕碎了的回忆
被撕碎了的回忆 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:24

    For anyone else coming across this thread I had this issue and was pulling my hair out. I had the service declaration OUTSIDE of the '< application>' end tag DUH!

    RIGHT:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      ...>
    ...
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity ...>
            ...
        </activity>    
    
        <service android:name=".Service"/>
    
        <receiver android:name=".Receiver">
            <intent-filter>
                ...
            </intent-filter>
        </receiver>        
    </application>
    
    <uses-permission android:name="..." />
    

    WRONG but still compiles without errors:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      ...>
    ...
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity ...>
            ...
        </activity>
    
    </application>
    
        <service android:name=".Service"/>
    
        <receiver android:name=".Receiver">
            <intent-filter>
                ...
            </intent-filter>
        </receiver>        
    
    <uses-permission android:name="..." />
    

    0 讨论(0)
  • 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:

    <application ... >
        <activity ... >
         ...
        </activity>
        <service
            android:name="com.mainActivity.MyRemoteGPSService"
            android:label="GPSService"
            android:process=":remote">
    
            <intent-filter>
                 <action android:name="RM_SRV_AIDL" />
            </intent-filter>
        </service>
    </application>
    
    0 讨论(0)
  • 2020-11-27 15:30

    First, you do not need android:process=":remote", so please remove it, since all it will do is take up extra RAM for no benefit.

    Second, since the <service> element contains an action string, use it:

    public void onCreate(Bundle savedInstanceState) {    
          super.onCreate(savedInstanceState);  
          Intent intent=new Intent("com.sample.service.serviceClass");  
          this.startService(intent);
    }
    
    0 讨论(0)
  • 2020-11-27 15:33

    I hope I can help someone with this info as well: I moved my service class into another package and I fixed the references. The project was perfectly fine, BUT the service class could not be found by the activity.

    By watching the log in logcat I noticed the warning about the issue: the activity could not find the service class, but the funny thing was that the package was incorrect, it contained a "/" char. The compiler was looking for

    com.something./service.MyService

    instead of

    com.something.service.MyService

    I moved the service class out from the package and back in and everything worked just fine.

    0 讨论(0)
  • 2020-11-27 15:35

    In my case the 1 MB maximum cap for data transport by Intent. I'll just use Cache or Storage.

    0 讨论(0)
  • 2020-11-27 15:38

    1) check if service declaration in manifest is nested in application tag

    <application>
        <service android:name="" />
    </application>
    

    2) check if your service.java is in the same package or diff package as the activity

    <application>
        <!-- service.java exists in diff package -->
        <service android:name="com.package.helper.service" /> 
    </application>
    
    <application>
        <!-- service.java exists in same package -->
        <service android:name=".service" /> 
    </application>
    
    0 讨论(0)
提交回复
热议问题