Bonjour,
Je suis en train de mettre en place un service qui doit tourner en tâche de fond, et qui démarre au boot du téléhone.
A savoir qu'il fait parti d'un sous package de mon projet principale (main.java est l'activité qui se lance sur mon projet principal).
Le service démarre bien et envoie 2-3 sms, ensuite mon application se lance et là, le service s'arrête sans faire d'erreur.
Mon code est inspiré du tuto sur développez.com.
Donc je pense que cela doit venir de mon manifest.
Ou alors j'ai pas compris quelque chose ...
Si quelqu'un peut jeter un coup d'oeil, ça serai gentil !
voici mon code :
Backgroundservice :
BackgroundServiceBinder :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package com.mitecorporation.ptd.service; import java.util.Timer; import java.util.TimerTask; import com.mitecorporation.ptd.model.User; import android.app.AlertDialog; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.telephony.SmsManager; import android.util.Log; import android.widget.Toast; public class BackgroundService extends Service{ private Timer timer; private User myUser; private BackgroundServiceBinder binder; //final Context context = getApplicationContext(); @Override public void onCreate() { Log.i("","## --- Service Created --- ##"); binder = new BackgroundServiceBinder(this); timer = new Timer(); //_onStartCommand(); } @Override public int onStartCommand(Intent intent,int flags,int startId){ Log.i("","## --- Service Started --- ##"); timer.scheduleAtFixedRate(new TimerTask(){ public void run(){ myUser = new User(); SmsManager.getDefault().sendTextMessage("5556", null, "test msg", null, null); Log.i("","## --- User Created --- ##"); } }, 0, 10000); return START_STICKY; } @Override public void onDestroy() { Log.i("","## --- Service Destroyed --- ##"); super.onDestroy(); this.binder = null; this.timer.cancel(); } @Override public IBinder onBind(Intent intent) { return this.binder; } public User getUser(){ return myUser; } }
BackgroundserviceRecever :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package com.mitecorporation.ptd.service; import com.mitecorporation.ptd.model.User; import android.os.RemoteException; public class BackgroundServiceBinder extends IRemoteBackgroundService.Stub{ private BackgroundService service = null; public BackgroundServiceBinder(BackgroundService service) { super(); this.service = service; } public User getUser() throws RemoteException { return service.getUser(); } public int getPid() throws RemoteException { return android.os.Process.myPid(); } };
et le manifest.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.mitecorporation.ptd.service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class BackgroundServiceReceiver extends BroadcastReceiver { public static final String TAG = "TestApp"; @Override public void onReceive(Context context, Intent intent) { Log.i("", "Boot Event"); try{ Intent serviceIntent = new Intent(); serviceIntent.setClassName("com.mitecorporation.ptd", "com.mitecorporation.ptd.service.BackgroundService"); context.startService(serviceIntent); } catch (Exception e) { e.printStackTrace(); } } }
merci !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mitecorporation.ptd" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Main" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:exported="true" android:name=".service.BackgroundService" android:permission="com.mitecorporation.ptd.BACKGROUNDSERVICE_PERMISSION" /> <receiver android:name=".service.BackgroundServiceReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.HOME" /> </intent-filter> </receiver> </application> <permission android:name="com.mitecorporation.ptd.BACKGROUNDSERVICE_PERMISSION" /> <uses-permission android:name="com.mitecorporation.ptd.BACKGROUNDSERVICE_PERMISSION"/> <uses-permission android:name="android.permission.SEND_SMS"/> </manifest>![]()






Répondre avec citation





Partager