Unable to start service Intent not found問題及Service無法拉起也無報錯問題

  1. Unable to start service Intent not found問題

    日誌:

07-02 12:29:06.129   781  3702 W ActivityManager: Unable to start service Intent { act=com.example.xiaomin.TestService cmp=com.example.xiaomin.myapplication/.service.TsetService } U=0: not found

代碼如下:

Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xiaomin.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.example.xiaomin.myapplication.service.TestService">
            <intent-filter>
                <action android:name="com.example.xiaomin.TestService" />
            </intent-filter>
        </service>
    </application>

</manifest>
Intent intent = new Intent();
        intent.setClassName("com.example.xiaomin.myapplication","com.example.xiaomin.myapplication.service.TsetService");
        intent.setAction("com.example.xiaomin.TestService");
        startService(intent);

將Manifest.xml文件片段修改如下,同樣報錯:

        <service android:name=".service.TestService">
            <intent-filter>
                <action android:name="com.example.xiaomin.TestService" />
            </intent-filter>
        </service>
  1. Service無法啓動也無報錯的情況
    基於上面的情況,修改intent,不指定intent的action值:
        Intent intent = new Intent();
        intent.setClassName("com.example.xiaomin.myapplication","com.example.xiaomin.myapplication.service.TsetService");

        startService(intent);

解決方法:修改intent如下:

        Intent intent = new Intent();
        intent.setClass(this, TestService.class);
        startService(intent);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章