我是android的新手。我的目标是在特定时间启动服务。上面的代码没有显示出良好的效果。
闹钟接收器:
public class AlarmReceiverActivity extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent background = new Intent(context, BackgroundService.class);
context.startService(background);
}}
服务类:
@TargetApi(Build.VERSION_CODES.N)
public class BackgroundService extends Service {
private boolean isRunning = false;
private Context context;
private Thread backgroundThread;
@Override
public IBinder onBind(Intent intent) {
return null;
}
private Runnable myTask = new Runnable() {
public void run() {
//task
stopSelf();
}
};
@Override
public void onCreate() {
this.context = this;
this.isRunning = true;
this.backgroundThread= new Thread(myTask);
}
@Override
public void onDestroy() {
this.isRunning = false;
stored3();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//if(!this.isRunning) {
//this.isRunning = true;
this.backgroundThread= new Thread(myTask);
this.backgroundThread.start();
stored4();
//}
return START_STICKY;
}}
Oncreate方法不是由系统执行的,所以我猜问题来自报警接收器。代码有问题吗?我会感激任何帮助。