我有一个BOOT_COMPLETED接收器。该接收器在电话启动时启动服务,但是当接收器工作并启动服务时,仅在Service上的DeDetroy模块上工作。我不知道为什么我创建了一个JobScheduler并检查服务的可用性,然后重新运行该服务,每次操作系统都会销毁。
Boot接收器为;
public class BootReciever extends BroadcastReceiver {
private static final String TAG="Deneme-App";
@Override public void onReceive(Context context, Intent intent){
try{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context,DenemeService.class));
}else{
context.startService(new Intent(context,DenemeService.class));
}
GcmNetworkManager mGcmNetworkManager = GcmNetworkManager.getInstance(context);
Task periodicTask = new PeriodicTask.Builder()
.setService(BestTimeService.class)
.setPeriod(1)
.setFlex(1)
.setTag("checkService")
.setPersisted(true)
.build();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
if (resultCode == ConnectionResult.SUCCESS) {
mGcmNetworkManager.schedule(periodicTask);
} else {
Log.e("Deneme-App", "Hello - Tag");
}
Log.e("Deneme-App", "Hello - Tag - OK");
Log.e("Deneme-App","Starting Service ConnectivityListener");
}catch(Exception e){
Log.e("Deneme-App",e.toString());
}
} }
JOB计划程序代码块;
public class BestTimeService extends GcmTaskService {
private static final String TAG = "Deneme-App";
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@Override
public int onRunTask(TaskParams taskParams) {
Context context=getApplicationContext();
if(!isMyServiceRunning(DenemeService.class))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context,DenemeService.class));
}else{
context.startService(new Intent(context,DenemeService.class));
}
}
Log.i(TAG, "onRunTask");
switch (taskParams.getTag()) {
case "checkService":
Log.i(TAG, "checkService");
// This is where you need to perform the background tasks.
return GcmNetworkManager.RESULT_SUCCESS;
default:
Log.i(TAG, "FAILURE checkService");
return GcmNetworkManager.RESULT_FAILURE;
}
}
}
服务代码块;
public class DenemeService extends Service {
Socket socket;
public DenemeService() {
Log.e("Deneme-App","Service Constuructor");
}
@Nullable
@Override
public IBinder onBind(Intent ıntent) {
Log.e("Deneme-App", "Service Started");
return null;
}
@Override
public void onRebind(Intent intent) {
Log.e("Deneme-App", "Service onRebind");
super.onRebind(intent);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("Deneme-App", "Service onTaskRemoved");
super.onTaskRemoved(rootIntent);
}
@Override
public void onDestroy() {
Log.e("Deneme-App", "Service onDestroy");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("Deneme-App", "Service onUnbind");
return super.onUnbind(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
return START_REDELIVER_INTENT;
}
@Override
public void onCreate() {
Log.e("Deneme-App", "Service onCreate");
super.onCreate();
}
}
Manifest.xml
<service
android:name=".BestTimeService"
android:exported="true"
android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
<intent-filter>
<action android:name="com.google.android.gms.gcm.ACTION_TASK_READY" />
</intent-filter>
</service>
<service android:name=".DenemeService" />
<receiver
android:name=".BootReciever"
android:enabled="true"
android:exported="false"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
答案 0 :(得分:0)
从Android API级别23开始,Android引入了两个新的节能功能,即Doze和App Standby,这对Apps施加了更多限制,使其能够在后台连续运行。具体来说,您可能需要将前台服务与用户在锁定屏幕上或通知托盘中看到的通知一起使用,以避免被Android系统杀死。
有关更多详细信息,请参见此链接:https://developer.android.com/training/monitoring-device-state/doze-standby。