【发布时间】: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