I am getting runtime error, trying to start a boot-time service. Its failing but there is no hint I can see. The intent is finding the Service but it doesn't get to onCreate or onStart.
我收到运行时错误,尝试启动启动时服务。它失败了,但我没有看到任何暗示。目的是找到服务,但它没有进入onCreate或onStart。
public class ControlReceiver extends BroadcastReceiver
{
/**
* @see android.content.BroadcastReceiver#onReceive(Context,Intent)
*/
@Override
public void onReceive(Context context, Intent intent)
{
Intent service = new Intent(context, MyApp.class);
Log.d("rcvr", "Received intent:" + intent.getAction());
context.startService(service);
}
}
The manifest:
<manifest
android:versionCode="1"
android:versionName="1.0"
package="com.company.package"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="10"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS"/>
<uses-permission android:name="android.permission.ACCESS_CELL_ID"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_LOGS"/>
<uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER"/>
<uses-permission android:name="android.permission.STATUS_BAR"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>
<uses-permission android:name="android.permission.SET_DEBUG_APP"/>
<uses-permission android:name="android.permission.BATTERY_STATS"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN"/>
<uses-permission android:name="android.permission.DIAGNOSTIC"/>
<uses-permission android:name="android.permission.DEVICE_POWER"/>
<uses-permission android:name="android.permission.DUMP"/>
<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/>
<uses-permission android:name="android.permission.SIGNAL_PERSISTENT_PROCESSES"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.PERSISTENT_ACTIVITY"/>
<uses-permission android:name="android.permission.HARDWARE_TEST"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/mod_name"
android:name=".MyApp">
<!-- We declare our service here -->
<service
android:enabled="true"
android:exported="true"
android:icon="@drawable/ic_launcher"
android:label="@string/serv_name"
android:name=".MyApp"
android:process=":my_process">
<intent-filter>
<action android:name="com.company.package.MY_INTENT" />
</intent-filter>
</service>
<receiver
android:name=".ControlReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
<receiver
android:name=".BatteryHelper" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.BATTERY_CHANGED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.BATTERY_LOW"/>
<category android:name="android.intent.category.INFO"/>
</intent-filter>
</receiver>
</application>
</manifest>
The exception thrown
抛出异常
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate application com.company.package.MyApp: java.lang.ClassCastException: com.company.package.MyApp cannot be cast to android.app.Application
at android.app.LoadedApk.makeApplication(LoadedApk.java:529)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4442)
at android.app.ActivityThread.access$1300(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
Update
When I removed android:name=".MyApp" for the application from the manifest, the service started. Anyone know why?
更新当我从清单中删除了应用程序的android:name =“。MyApp”时,服务启动了。谁知道为什么?
1 个解决方案
#1
0
when I removed android:name=".MyApp" for the application from the manifest, the service started. Anyone know why?
当我从清单中删除了应用程序的android:name =“。MyApp”时,服务就开始了。谁知道为什么?
The reason for this is most likely the fact that your MyApp
class does not extend android.app.Application
.
这样做的原因很可能是你的MyApp类没有扩展android.app.Application。
As the Android Developer Site says:
正如Android开发者网站所说:
android:name
The fully qualified name of an Application subclass implemented for the application. When the application process is started, this class is instantiated before any of the application's components.
为应用程序实现的Application子类的完全限定名称。启动应用程序进程时,将在任何应用程序的组件之前实例化此类。
The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.
子类是可选的;大多数应用程序不需要一个。在没有子类的情况下,Android使用基本Application类的实例。
Since subclassing Application
is optional, removing android:name=".MyApp"
allows your app to properly start because you're no longer trying to cast MyApp
to Application
which is exactly what your exception claims your app is trying to do.
由于子类化应用程序是可选的,删除android:name =“。MyApp”允许您的应用程序正确启动,因为您不再尝试将MyApp强制转换为应用程序,这正是您的应用程序尝试执行的异常声明。
#1
0
when I removed android:name=".MyApp" for the application from the manifest, the service started. Anyone know why?
当我从清单中删除了应用程序的android:name =“。MyApp”时,服务就开始了。谁知道为什么?
The reason for this is most likely the fact that your MyApp
class does not extend android.app.Application
.
这样做的原因很可能是你的MyApp类没有扩展android.app.Application。
As the Android Developer Site says:
正如Android开发者网站所说:
android:name
The fully qualified name of an Application subclass implemented for the application. When the application process is started, this class is instantiated before any of the application's components.
为应用程序实现的Application子类的完全限定名称。启动应用程序进程时,将在任何应用程序的组件之前实例化此类。
The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.
子类是可选的;大多数应用程序不需要一个。在没有子类的情况下,Android使用基本Application类的实例。
Since subclassing Application
is optional, removing android:name=".MyApp"
allows your app to properly start because you're no longer trying to cast MyApp
to Application
which is exactly what your exception claims your app is trying to do.
由于子类化应用程序是可选的,删除android:name =“。MyApp”允许您的应用程序正确启动,因为您不再尝试将MyApp强制转换为应用程序,这正是您的应用程序尝试执行的异常声明。