Bonsoir tous le monde,
Voilà j'ai un étrange problème :p j'ai un BroadcastReceiver qui lance un service au démarrage du téléphone (Et ça fonctionne) mais que pendant 5 à 10 secondes maximum au dela plus rien ne se passe comme si le service était terminé
Sauf que si je lance le service depuis mon application (La Mainactivity) et que je ferme mon programme le service continue et redémarre si besoins...
Je ne comprends pas pourquoi le service lancé par le BroadcastReceiver se stop au bout de quelques secondes alors que lancé depuis l'application même aucun soucis
Voici le BroadcastReceiver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.caranille.myownpet.Kernel;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.caranille.myownpet.MainActivity;
public class KernelReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Votre Pet est heureux de vous revoir !" , Toast.LENGTH_LONG).show();
Intent serviceIntent = new Intent(context, KernelService.class);
context.startService(serviceIntent);
}
} |
Voici le service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| package com.caranille.myownpet.Kernel;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import com.caranille.myownpet.DB.DBPetManage;
import com.caranille.myownpet.MainActivity;
import com.caranille.myownpet.Models.Pets;
import com.caranille.myownpet.Models.Trainers;
import com.caranille.myownpet.R;
import java.util.Timer;
import java.util.TimerTask;
public class KernelService extends Service
{
private Timer timer = new Timer();
private int i = 0;
static String name = "KernelService";
static DBPetManage dbPetManage;
static Pets pets;
static Trainers trainers;
@Override
public void onCreate()
{
super.onCreate();
Notification("Démarrage du service");
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Refresh();
}
}, 0, 1000);
}
public int onStartCommand(Intent intent, int flags, int startId)
{
Notification("Redémarrage du service");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onDestroy() {
Notification("Fermeture du service");
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void Refresh()
{
i++;
Notification("Refresh " + i);
//Mes calculs dont je cache le code
}
public void Notification(String message)
{
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this).setSmallIcon(R.drawable.pet).setContentTitle("MyOwnPet").setContentText(message);
Intent resultIntent = new Intent(this, KernelService.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
} |
Et le manifest:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.caranille.myownpet">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RegisterActivity"
android:screenOrientation="portrait" />
<activity
android:name=".MainPetStatsActivity"
android:screenOrientation="portrait" />
<activity
android:name=".TrainingPetActivity"
android:screenOrientation="portrait" />
<activity
android:name=".MainPetActivity"
android:screenOrientation="portrait" />
<activity android:name=".ArenaActivity"></activity>
<service
android:enabled="true"
android:name="com.caranille.myownpet.Kernel.KernelService">
</service>
<receiver
android:name="com.caranille.myownpet.Kernel.KernelReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest> |
Il s'agit d'un tamagoshi et toutes les x secondes cela met à jour la stats de faim et de soif de l'animal
Voilà je viens ici en espérant comprendre pourquoi mon service se stop si il est lancé avec le BroadcastReceiver alors qu'avec l'application en elle même avec le code suivant cela fonctionne:
startService(new Intent(MainActivity.this, KernelService.class));
P.S: Après avoir analysé le code je vois que mon application est bien lancé et que au bout de X secondes celle-ci s'arrête (Car dans les info de mon appli j'ai le bouton arrêter l'application qui est non grisé et au bout de 8 secondes il est grisé cela me prouve que mon application et mon service se stop net... alors que sur l'émulateur Android cela fonctionne à merveille...) et le pire dans tout ça ? C'est que sur ma tablette Android cela fonctionne aussi le problème vient de mon smartphone... Pourtant même version Android
Cordialement,
Partager