gpt4 book ai didi

java - Android:调用 Context.startService() 后未启动嵌套 Intent 服务

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:00 26 4
gpt4 key购买 nike

我的嵌套 Intent 服务定义如下:

package com.my.package;

... // Bunch of imports

public class MyNotifier

... // Bunch of variables

public class MissedCallIntentService extends IntentService {

private static final String TAG = "MissedCallIntentService";

public MissedCallIntentService() {
super("MissedCallIntentService");
Log.i(TAG, "Creating intent service.");
}

@Override
public void onHandleIntent(Intent intent) {
Log.i(TAG, "Handling intent service.");
}
}

// Test my nested intent filter
public MyNotifier(Context app) {
mApp = app;
Log.i(LOG_TAG, "Going to start intent service.");
Intent intent = new Intent(mApp, MissedCallIntentService.class);
mApp.startService(intent);
}

... // Bunch of functions
}

我的 AndroidManifest.xml 文件中包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
// Protected Broadcasts
// Permissions
<application ...>
<service android:name="com.my.package.MyNotifier.MissedCallIntentService" >
</service>

<activity android:name="ActivityOne"
android:label="@string/activity_one"
<intent-filter>
...
</intent-filter>
</activity>

<activity android:name="ActivityTwo"
android:label="@string/activity_two"
<intent-filter>
...
</intent-filter>
</activity>

<activity android:name="ActivityThree"
android:label="@string/activity_three"
<intent-filter>
...
</intent-filter>
</activity>
</application>
</manifest>

在构建我的应用程序然后将其推送到手机并运行它之后,这就是我所看到的。

$ make_magic && adb remount && adb push MyApp.apk /system/app/ && adb reboot && adb logcat | grep 'intent\ service'
make: Leaving directory `BuildDir'
remount succeeded
6149 KB/s (6036528 bytes in 0.958s)
- waiting for device -
I/MyNotifier( 1184): Going to start intent service.

我应该看到:

I/MyNotifier( XXXX): Going to start intent service.
I/MissedCallIntentService( XXXX): Creating intent service.
I/MissedCallIntentService( XXXX): Handling intent service.

这就是我的问题所在。我需要添加什么才能调用我的 Intent 服务?

最佳答案

将嵌套内部类声明为静态的或在它自己的类中定义它(如果这样做,更新 list )

如果你引用一个内部类,引用应该是

<service android:name="com.my.package.MyNotifier$MissedCallIntentService" />

(注意美元符号)

关于java - Android:调用 Context.startService() 后未启动嵌套 Intent 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19126215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com