还有一个“无法启动服务意图 - 未找到”

时间:2014-05-19 21:19:14

标签: android android-intent android-service

很多关于此的问题。但所有这些都是关于"我忘了为清单添加服务"或者"我在清单"中添加了不在应用程序标签中的服务。对我来说,没有这样的错误,但仍然没有找到"给出了。

我的清单:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        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=".PrefActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.Preferences" />
            <category android:name="android.intent.category.PREFERENCE" />
        </intent-filter>
    </activity>
    <service android:name=".ExampleService"
        android:enabled="false"
        android:exported="false" />
</application>

我开始服务(来自主要活动&#39; s片段):

@Override
public void onClick(View view) {

    switch(view.getId()) {
        case R.id.save_btn: {
            getActivity().startService(new Intent(getActivity(), ExampleService.class));
            break;
        }
    }
}

服务代码,用于&#34;以防万一&#34;:

public class ExampleService extends Service {

    NotificationManager notificationManager;

    @Override
    public void onCreate() {

        super.onCreate();
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Log.i("LOG_TAG", "service started!");
    }

    private void fireNotification() {

        // prepare notification with icon and text
        Notification notification = new Notification(R.drawable.ic_launcher, "Notification text",
                System.currentTimeMillis());

        // prepare action for notification pressed
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // prepare extended notification's info (when notification bar opened)
        notification.setLatestEventInfo(this, "Title", "Notification text, baby!", pendingIntent);

        // clear notification after pressing:
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // fire the notification!
        notificationManager.notify(1, notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        fireNotification();
        Log.i("LOG_TAG", "notification fired!");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }
}

我尝试在intent中完全限定package(当指定ExampleService.class时)并且在manifest中 - 没有帮助。

当我按下该按钮时 - 服务未启动。 LogCat显示:

342-352/system_process W/ActivityManager﹕ Unable to start service Intent { cmp=com.drobiazko.den.iremember/.ExampleService }: not found

1 个答案:

答案 0 :(得分:0)

试一试

Intent intent = new Intent(MainActivity.this, ExampleService.class);

startActivity(intent);

并在您的清单文件中

<activity
    android:name=".ExampleService"
    android:label="@string/app_name" >
</activity>