[英]Service not starting up after boot completed
I programmed a boot up receiver to start a service, but the service is not starting. 我对启动接收器进行了编程以启动服务,但是该服务未启动。 I don't even think the broad cast receiver is receiving any intents.
我什至不认为广泛的演员接受任何意图。
I set the permission 我设置权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
I declared the receiver in the manifest 我在清单中声明了接收者
<receiver android:name=".ServiceStarter">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Here is my broad cast receiver class. 这是我广泛的演员接收器课程。 I'm not logging any output in this class after boot up.
启动后,我不会在此类中记录任何输出。
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
}
}
Here is my service class. 这是我的服务班。 None of the toasts are displayed after boot up.
启动后,没有显示任何吐司。
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");
}
}
Try this: 尝试这个:
<receiver android:name=".ServiceStarter" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
if still dosn't work please try this 如果仍然无法正常工作,请尝试此操作
<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>
Good luck 祝好运
It's very likely that your service is starting up, but is being killed off before it can do anything meaningful. 您的服务很可能正在启动,但是在可以做任何有意义的事情之前就被杀死了。 Try acquiring a partial wake lock - Mark Murphy has written a handy set of classes just for that purpose.
尝试获取部分唤醒锁 -马克·墨菲(Mark Murphy)为此目的编写了一组方便的类 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.