0

我在尝试在 android 启动时启动服务时遇到问题,只要手机启动我的应用程序就会出错并且必须停止它。这里是:

我的清单的一部分:

<service
    android:name=".EventsNotificationService"
    android:label="EventsNotificationService">
    <intent-filter>
        <action
            android:name="it.bloomp.service.EventsNotificationService" />
    </intent-filter>
/service>

<receiver
    android:name=".receiver.StartEventsNotificationService"
    android:enabled="true"
    android:exported="true"
    android:label="StartEventsNotificationService">
    <intent-filter>
        <action
            android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

我的服务:

package it.bloomp.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class EventsNotificationService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this,"Service created", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this,"Service destroyed", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onCreate();
        Toast.makeText(this,"Service started", Toast.LENGTH_LONG).show();
    }
}

我的服务启动器:

package it.bloomp.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartEventsNotificationService extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent serviceIntent = new Intent("it.bloomp.service.EventsNotificationService");
            context.startService(serviceIntent);
        }
    }
}

此外,这会使服务在启动时启动,但我怎样才能让它一直运行呢?

编辑:

例外情况:

06-01 16:58:55.716: E/AndroidRuntime(483): 致命异常: main 06-01 16:58:55.716: E/AndroidRuntime(483): java.lang.RuntimeException: 无法实例化接收器 it.bloomp。 activity.receiver.StartEventsNotificationService: java.lang.ClassNotFoundException: it.bloomp.activity.receiver.StartEventsNotificationService 06-01 16:58:55.716: E/AndroidRuntime(483): at android.app.ActivityThread.handleReceiver(ActivityThread.java: 2100) 06-01 16:58:55.716: E/AndroidRuntime(483): 在 android.app.ActivityThread.access$1500(ActivityThread.java:123) 06-01 16:58:55.716: E/AndroidRuntime(483):在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197) 06-01 16:58:55.716: E/AndroidRuntime(483): 在 android.os.Handler.dispatchMessage(Handler.java:99) 06- 01 16:58:55.716:E/AndroidRuntime(483):在 android.os.Looper。循环(Looper.java:137) 06-01 16:58:55.716: E/AndroidRuntime(483): 在 android.app.ActivityThread.main(ActivityThread.java:4424) 06-01 16:58:55.716: E/ AndroidRuntime(483): at java.lang.reflect.Method.invokeNative(Native Method) 06-01 16:58:55.716: E/AndroidRuntime(483): at java.lang.reflect.Method.invoke(Method.java: 511) 06-01 16:58:55.716: E/AndroidRuntime(483): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-01 16:58:55.716: E/ AndroidRuntime(483): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-01 16:58:55.716: E/AndroidRuntime(483): 在 dalvik.system.NativeStart.main(Native方法)06-01 16:58:55.716:E/AndroidRuntime(483):引起:java.lang.ClassNotFoundException:it.bloomp.activity.receiver.StartEventsNotificationService 06-01 16:58:55.716:E/AndroidRuntime(483): 在 dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 06-01 16:58:55.716: E/AndroidRuntime(483): 在 java.lang.ClassLoader.loadClass(ClassLoader.java :501) 06-01 16:58:55.716: E/AndroidRuntime(483): 在 java.lang.ClassLoader.loadClass(ClassLoader.java:461) 06-01 16:58:55.716: E/AndroidRuntime(483):在 android.app.ActivityThread.handleReceiver(ActivityThread.java:2095) 06-01 16:58:55.716: E/AndroidRuntime(483): ... 10 更多ActivityThread.handleReceiver(ActivityThread.java:2095) 06-01 16:58:55.716: E/AndroidRuntime(483): ... 10 更多ActivityThread.handleReceiver(ActivityThread.java:2095) 06-01 16:58:55.716: E/AndroidRuntime(483): ... 10 更多

编辑2:

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.bloomp.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <activity
            android:name=".EventsListActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".EventActivity"/>

        <service android:name=".EventsNotificationService" android:label="EventsNotificationService">
            <intent-filter>
                <action android:name="it.bloomp.service.EventsNotificationService" />
            </intent-filter>
        </service>

        <receiver
            android:name=".StartEventsNotificationService"
            android:enabled="true"
            android:exported="true"
            android:label="StartEventsNotificationService">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

我的服务:

package it.bloomp.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class EventsNotificationService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Service created.");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("Service destroyed.");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onCreate();
        System.out.println("Service started.");
    }
}

我的服务启动器:

package it.bloomp.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartEventsNotificationService extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent serviceIntent = new Intent("it.bloomp.service.EventsNotificationService");
            context.startService(serviceIntent);
        }
    }
}

我的例外:

06-03 15:37:36.142: E/AndroidRuntime(479): 致命异常: main 06-03 15:37:36.142: E/AndroidRuntime(479): java.lang.RuntimeException: 无法实例化接收器 it.bloomp。 activity.StartEventsNotificationService: java.lang.ClassNotFoundException: it.bloomp.activity.StartEventsNotificationService 06-03 15:37:36.142: E/AndroidRuntime(479): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2100) 06- 03 15:37:36.142: E/AndroidRuntime(479): 在 android.app.ActivityThread.access$1500(ActivityThread.java:123) 06-03 15:37:36.142: E/AndroidRuntime(479): 在 android.app .ActivityThread$H.handleMessage(ActivityThread.java:1197) 06-03 15:37:36.142: E/AndroidRuntime(479): 在 android.os.Handler.dispatchMessage(Handler.java:99) 06-03 15:37 :36.142: E/AndroidRuntime(479): 在 android.os.Looper.loop(Looper.java:137) 06-03 15:37:36.142: E/AndroidRuntime(479): 在 android.app.ActivityThread.main(ActivityThread.java:4424) 06-03 15:37:36.142: E/AndroidRuntime(479) : 在 java.lang.reflect.Method.invokeNative(Native Method) 06-03 15:37:36.142: E/AndroidRuntime(479): 在 java.lang.reflect.Method.invoke(Method.java:511) 06- 03 15:37:36.142: E/AndroidRuntime(479): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-03 15:37:36.142: E/AndroidRuntime(479) : 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-03 15:37:36.142: E/AndroidRuntime(479): 在 dalvik.system.NativeStart.main(Native Method) 06- 03 15:37:36.142: E/AndroidRuntime(479): 原因: java.lang.ClassNotFoundException: it.bloomp.activity.StartEventsNotificationService 06-03 15:37:36.142: E/AndroidRuntime(479):在 dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 06-03 15:37:36.142: E/AndroidRuntime(479): 在 java.lang.ClassLoader.loadClass(ClassLoader.java:501) 06-03 15 :37:36.142: E/AndroidRuntime(479): 在 java.lang.ClassLoader.loadClass(ClassLoader.java:461) 06-03 15:37:36.142: E/AndroidRuntime(479): 在 android.app.ActivityThread。 handleReceiver(ActivityThread.java:2095) 06-03 15:37:36.142: E/AndroidRuntime(479): ... 10 更多handleReceiver(ActivityThread.java:2095) 06-03 15:37:36.142: E/AndroidRuntime(479): ... 10 更多handleReceiver(ActivityThread.java:2095) 06-03 15:37:36.142: E/AndroidRuntime(479): ... 10 更多

4

2 回答 2

1

我在清单文件中看不到权限。您应该添加:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

此外,首先尝试检查日志中是否启动了服务。在那之后只做吐司,也许问题就在那里。

于 2012-06-01T19:13:01.950 回答
1

android:name=".receiver.StartEventsNotificationService"

可能应该是

android:name=".StartEventsNotificationService"

编辑

试试这个作为你的意图:

Intent i = new Intent(context, EventNotificationService.class);

此外,您应该更新收到的错误。鉴于您从清单和服务启动代码中显示的内容,它不能与您所拥有的相同。尝试自己阅读错误并遵循它们指向的位置,学习如何有效地做到这一点将为您节省大量的 StackOverflow 时间,并且比仅仅询问其他人会教给您很多。

于 2012-06-01T20:07:12.243 回答