Je veux démarrer un service au démarrage. J'ai fait un récepteur breoadcast, lancé un service de broadcastreceiver, tout inséré dans xml. Le tutoriel de http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/
m'a beaucoup aidé. Le résultat: Sur l'émulateur, comme sur un périphérique réel se produit comme ceci: - le service est démarré, puis s'arrête immédiatement! Je ne sais pas pourquoi le service s'arrête! Démarrage du service android forcé arrêté au démarrage- Que faire?
Le service est démarré à coup sûr, je peux lire les journaux et toast msg. qui certifie que le service est démarré.
également dans les journaux que je peux voir msg comme:
<u>
05-17 23:18:44.379: D/EAS SyncManager(247): !!! EAS SyncManager, onCreate
05-17 23:18:44.789: D/EAS SyncManager(247): !!! EAS SyncManager, onStartCommand
05-17 23:18:44.860: D/EAS SyncManager(247): !!! EAS SyncManager, stopping self
05-17 23:18:44.999: D/My Service(258): starting broadcast receiver
05-17 23:18:45.119: D/My Service(258): starting service in onCreate
05-17 23:18:45.985: D/Eas Debug(247): Logging:
05-17 23:18:45.999: D/EAS SyncManager(247): !!! EAS SyncManager, onDestroy
05-17 23:18:46.299: I//system/bin/fsck_msdos(29): Attempting to allocate 998 KB for FAT
</u>
L'EAS SyncManager est détruit. Je crois que c'est la raison pour laquelle mon service est forcé d'arrêter. De ce que j'ai lu de http://hi-android.info/src/com/android/exchange/SyncManager.java.html il semble que lorsque EAS SyncManager est détruit, le service démarré au démarrage, reçoit un StopSelf() par défaut.
Je ne sais pas si c'est vrai ou non. Et si c'est vrai ... comment puis-je le réparer? Est-ce que quelqu'un s'est confrounted avec ceci jusqu'à maintenant? S'il vous plaît aider.
Voici le code:
classe de
// classe de service
public class Contacts_Service extends Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d("My Service", "onCreate");
mPeriodicEventHandler = new Handler();
mPeriodicEventHandler.postDelayed(doPeriodicTask,13000);
}
private Runnable doPeriodicTask = new Runnable()
{
public void run()
{
//your action here
Log.d("My Service", "eeeevery 13000 ms");
mPeriodicEventHandler.postDelayed(doPeriodicTask, 13000);
}
};
}
// classe BroadcastReceiver où est le service déjeunait au démarrage
public class MyStartupIntentReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
Log.d("My Service", "starting broadcast receiver");
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.Srv.mail.Contacts_Service");
context.startService(serviceIntent);
//serviceIntent.setAction("com.Srv.mail.SrvMailSettingsActivity");
//context.startActivity(serviceIntent);
}
}
}
et aussi AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Srv.mail"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".MyStartupIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
<service android:enabled="true" android:name="Contacts_Service">
<intent-filter>
<action android:name="com.Srv.mail.Contacts_Service" />
</intent-filter>
</service>
<activity
android:name="com.Srv.mail.SrvMailSettingsActivity"
android:label="@string/app_name" >
<!-->intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter-->
</activity>
</application>
</manifest>
Pouvez-vous montrer le code du service? – Dharmendra
Si votre service est un 'IntentService', il est probable qu'après avoir traité Intent, il s'arrête avec' stopSelf(). 'Dur à dire sans regarder votre code. – curioustechizen
ok voici le code: – marisxanis