1

在花了将近整整一周的时间在网上复制和粘贴每个示例之后,我开始意识到我只是不了解 serviceIntent。

我理解这个理论(我认为),只是当我尝试时它对我不起作用。我已经剥离了我现有的代码,只留下了提出这个问题所必需的内容,使用“println”来演示一个工作示例与否。你们能告诉我哪里出错了。谢谢。

如果它很重要,我只使用 AIDE。我检查了 AIDE 在意图服务方面是否有限制,但没有发现没有什么可说的。

MAINACTIVITY.JAVA

package com.mycompany.rns;

imports are listed here...

public class MainActivity extends Activity {

    public class MyService extends IntentService {

        public MyService(){
            super("MyService");
        }

        @Override
        protected void onHandleIntent(Intent intent) {
            system.out.println("At fucking last!");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent k = new Intent(this,MyService.class);
        startService(k);
    }
}

清单.XML

</activity>
<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="false" />
</application>
4

1 回答 1

1

正如 Ahmed Ewiss 给出了一个正确的答案,但没有创建一个我可以接受的答案,使用他的建议,这对于其他所有人来说都是一个他们可以使用的简单模板......

MAINACTIVITY.JAVA

package com.mycompany.rns;

imports are listed here...

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent k = new Intent(this,MyService.class);
        startService(k);
    }
}

我的服务.JAVA

package com.mycompany.rns;

imports are listed here...

public class MyService extends IntentService {

    public MyService(){
        super("MyService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        system.out.println("At fucking last!");
    }
}

主文件

</activity>
<service
     android:name=".MyService"
     android:enabled="true"
     android:exported="false" />
</application>

可行的解决方案是将 MainActivity.java 文件与 Service.java 文件分开。类文件。

于 2018-09-17T12:52:44.040 回答