我正在尝试创建在后台运行,检查信息并在需要时发出通知的服务。我在互联网上尝试了几个基本相同的示例,但是我没有让它们起作用。
我有一个服务和一个BroadcastReceiver。当我启动该应用程序时,将创建该服务,并在每个滴答处记录一个时间。所以,这很好。在onDestroy上,我设置了一个新的意图,该意图与“ sendBroadcast(...)”一起发送。 BroadcastReceiver类在onReceive上有一个重写,它应该记录一个简单的文本。但是我没看过文字。
服务
public class ServiceNoDelay extends Service {
public int counter = 0;
Context context;
public ServiceNoDelay(Context applicationContext) {
super();
context = applicationContext;
Log.i("HERE", "here service created!");
}
public ServiceNoDelay() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("EXIT", "ondestroy!");
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.custom.package.RestartSensor");
sendBroadcast(broadcastIntent);
stoptimertask();
Log.i("EXIT", "Broadcast send!");
}
private Timer timer;
private TimerTask timerTask;
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//schedule the timer, to wake up every 1 second
timer.schedule(timerTask, 1000, 1000); //
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
Log.i("in timer", "in timer ++++ " + (counter++));
}
};
}
public void stoptimertask() {
//stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
广播接收器
public class SensorRestarterBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(SensorRestarterBroadcastReceiver.class.getSimpleName(), "Service Stops! Oops!!!!");
context.startService(new Intent(context, ServiceNoDelay.class));
}
}
清单部分
<service
android:name=".ServiceNoDelay"
android:enabled="true" />
<receiver
android:name=".SensorRestarterBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:label="RestartServiceWhenStopped">
<intent-filter>
<action android:name="com.custom.package.RestartSensor" />
</intent-filter>
</receiver>
我在MainActivity中调用的代码部分
ServiceNoDelay mSensorService = new ServiceNoDelay(getApplicationContext());
Intent mServiceIntent = new Intent(getApplicationContext(), mSensorService.getClass());
startService(mServiceIntent);
我希望logcat将显示“服务停止!糟糕!!!”当应用关闭时,我可以再次激活该服务。
如果我在关闭应用程序后发现显示通知的错误路径;我愿意提出建议。
答案 0 :(得分:1)
您需要做的几件事, 1.如果需要让服务在后台运行,请创建一个前台服务。 2.关闭打ze模式 3.或点击this链接以在后台运行服务
或者在代码中,您可以致电onTaskRemoved
并添加一条警报以重新启动服务
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("FLAGX : ", ServiceInfo.FLAG_STOP_WITH_TASK + "");
Intent restartServiceIntent = new Intent(getApplicationContext(),
this.getClass());
restartServiceIntent.setPackage(getPackageName());
Intent intent = new Intent(getApplicationContext(), this.getClass());
intent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(
getApplicationContext(), 1, restartServiceIntent,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext()
.getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 2000,
restartServicePendingIntent);
super.onTaskRemoved(rootIntent);
}
在清单中添加权限
<uses-permission android:name="android.permission.SET_ALARM"/>