【问题标题】:Standalone IntentService error: Unable to start service Intent {act = com.example.. pkg=com.example.somepkg} U=0: not found独立 IntentService 错误:无法启动服务 Intent {act = com.example.. pkg=com.example.somepkg} U=0:未找到
【发布时间】:2020-04-02 23:50:51
【问题描述】:

我正在尝试从另一个 android 客户端应用程序启动一个独立的 Intent 服务,即使花了几个小时上网,我也无法让它工作。

错误如下:

04-02 16:40:32.052 2131 6557 W ActivityManager:无法启动服务 Intent { act=com.example.zorro.Slash pkg=com.example.cookie } U=0:未找到

应用如下:

意图类:

package com.example.zorro;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;

public class Slash extends IntentService {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public Slash(String name) {
        super(name);
    }

    private static final String TAG = "ZORRO";
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d(TAG, "Hello world from Zorro Service");

    }
}

意图服务AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zorro">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <service android:name="com.example.zorro.Slash" android:enabled = "true" android:exported = "true" />
    </application>
</manifest>

客户端应用代码:

package com.example.cookie;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    Intent serviceIntent = new Intent();

    @Override
    protected void onStart() {
        super.onStart();

        serviceIntent.setAction("com.example.zorro.Slash");
        serviceIntent.setPackage(this.getPackageName());
        startService(serviceIntent);
    }

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

首先,我从 Android Studio 构建 Intent 服务并通过运行按钮将其加载到模拟器中。我将启动选项设置为 Nothing,将 Deploy 设置为 Default APK

我在来自不同 android studio 实例的同一模拟器上运行的客户端应用程序。

任何想法都将不胜感激。

【问题讨论】:

    标签: java android android-intent


    【解决方案1】:
    serviceIntent.setAction("com.example.zorro.Slash");
    

    您的&lt;service&gt; 没有带有此&lt;action&gt;&lt;intent-filter&gt;。因此,Android 无法找到该服务。

    【讨论】:

    • 即使将intent-filter添加到服务的AndroidManifest.xml中,在下面的sn-p中: ``` xml ``` 我得到了同样的结果
    【解决方案2】:

    似乎缺少的部分是在客户端代码的意图上设置类名。

    具体

    serviceIntent.setClassName("com.example.zorro", "com.example.zorro.Slash");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多