[英]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>
祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.