java - 服务无法转换为 android.app.Activity

标签 java android android-intent service android-context

我正在尝试访问服务中的 editText 和按钮。 我见过this answerthis answer ,并尝试合并和计算,但没有收获。我在这里缺少什么?我也看到过其他类似的答案,并且从过去两天起一直试图完成它,但一切都是徒劳的。任何其他实现它的方式也将受到欢迎。

相关代码如下: MainActivity.java

btn_start.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View view) {
                startService(new Intent(getApplicationContext(), MyService.class));
            }
        });

MyService.java

        Context context = MyService.this;
        Activity activity = (Activity) context;
        Button stopButton = activity.findViewById(R.id.btn_stop);
        EditText editTextUsername = activity.findViewById(R.id.edit_name);

AndroidManifest.xml

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

但我收到此错误: LOGCAT

11-14 02:55:07.835 5906-5906/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.nwagh.myapplication, PID: 5906 java.lang.RuntimeException: Unable to start service com.example.nwagh.myapplication.MyService@32189af with Intent { cmp=com.example.nwagh.myapplication/.MyService }: java.lang.ClassCastException: com.example.nwagh.myapplication.MyService cannot be cast to android.app.Activity at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3060) at android.app.ActivityThread.access$2300(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:5527) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) Caused by: java.lang.ClassCastException: com.example.nwagh.myapplication.MyService cannot be cast to android.app.Activity at com.example.nwagh.myapplication.MyService.onStartCommand(MyService.java:68) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3043) at android.app.ActivityThread.access$2300(ActivityThread.java:153)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:5527)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)  11-14 02:55:07.844 5906-5906/? E/MQSEventManagerDelegate: failed to get MQSService.

Here如果您需要更多引用,这是完整的代码。

最佳答案

你找错了树;-) 你的 Service 不应该知道关于你的 UI 的任何信息,也不应该对其进行修改。如果您想从 Service 更改 UI,则需要将该任务委托(delegate)给 ActivityActivity 负责 UI。您可以使用多种有据可查的方法:

  • Activity 绑定(bind)到 Service,并且 ServiceActivity 上执行回调
  • Service 发送广播 IntentActivity 注册一个监听器,该监听器在广播时对 Intent 使用react
  • 使用事件总线实现

关于java - 服务无法转换为 android.app.Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47274086/

相关文章:

java - 使用带有空格的参数调用 java.lang.Runtime.exec(String) 时出现问题

java - HashMap 作为字符串的 ArrayList

java - 如何使用 Apache POI 和 TestNG dataProvider 编写 Excel

javascript - 如何从数据库android在recyclerview中显示html.fromhtml文本

android - 这是解析json的正确方法吗?

android - 在同一 Activity 中在 android 中播放 youtube 视频有哪些替代方案?

android - Flutter:如果页面已经被压入堆栈,如何重新打开它们?

java - 如何从 java android 中的相机预览中获取字节数组?

android - 如何避免回到之前的 Activity

java - 使用传递的字符串调用资源