簡體   English   中英

引導完成后服務未啟動

[英]Service not starting up after boot completed

我對啟動接收器進行了編程以啟動服務,但是該服務未啟動。 我什至不認為廣泛的演員接受任何意圖。

我設置權限

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

我在清單中聲明了接收者

<receiver android:name=".ServiceStarter">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

這是我廣泛的演員接收器課程。 啟動后,我不會在此類中記錄任何輸出。

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

public class ServiceStarter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent("com.prac.test.MyPersistingService");
        i.setClass(context, MyPersistingService.class);
        context.startService(i);

        Log.v("TAG","Broadcast received"); //Doesn't print anything
    }
}

這是我的服務班。 啟動后,沒有顯示任何吐司。

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

public class MyPersistingService extends Service {

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

    @Override
    public void onCreate() {
        Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();         //Doesn't show up
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();          //Doesn't show up
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show(); //Doesn't show up
        Log.v("TAG", "Service started");
    }
}

嘗試這個:

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

如果仍然無法正常工作,請嘗試此操作

 <receiver
    android:name=".ServiceStarter"
    android:enabled="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
     <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />    
       <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
 </receiver>

祝好運

您的服務很可能正在啟動,但是在可以做任何有意義的事情之前就被殺死了。 嘗試獲取部分喚醒鎖 -馬克·墨菲(Mark Murphy)為此目的編寫了一組方便的類

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM