bonjour

je débute dans la programmation android.

j'aimerais faire tourner un service en background.
ce service récupère toutes les 5 minutes le md5 d'une image en ligne.
tout fonctionne mais cela crash quand l'appareil / l'émulateur n'est pas connecté.

j'ai mis le code complet du service :

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.mypack.otw;

import java.io.InputStream;
import java.net.URL;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class TestService extends Service {

    public TestService()  {
    	
    }
    
    private static Timer timer = new Timer(); 
    @SuppressWarnings("unused")
	private Context ctx;
    public static String hash = "xxx";
    
    public static String checkHashURL(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            
            InputStream is = new URL(input).openStream();

            
            try {
                is = new DigestInputStream(is, md);

                while (is.read() != -1) {
                    ;
                }
            } catch (Exception vv){
            	is.close();
            	throw new RuntimeException(vv);
            }
             
            finally {
                is.close();
            }
            byte[] digest = md.digest();
            StringBuffer sb = new StringBuffer();

            for (int i = 0; i < digest.length; i++) {
                sb.append(
                        Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(
                                1));
            }
            
            return sb.toString();
            

          
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    
    

    @Override
    public IBinder onBind(Intent intent) {
           // TODO: Return the communication channel to the service.
           throw new UnsupportedOperationException("Not yet implemented");
    }
    @SuppressLint("ShowToast") @Override
    public void onCreate()  {  
    	
           // TODO Auto-generated method stub
    	   Toast.makeText(getApplicationContext(), "Service Created",1).show();
    	   Log.i("Le service a été créé", "Service_created");
           super.onCreate();
    }
    
    
    private class mainTask extends TimerTask
    { 
        public void run() 
        {
        	//hash = checkHashURL("http://www.url_enquestion/example.jpg");
        	toastHandler.sendEmptyMessage(0);
        }
    }    
  
   
    @SuppressLint("ShowToast") @Override
    public void onDestroy() {
           // TODO Auto-generated method stub
           Toast.makeText(getApplicationContext(), "Service Destroy",1).show();
           Log.i("Le service a été démoli", "Service_destroyed");
           super.onDestroy();
    }
   
    @SuppressLint("ShowToast") @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
           // TODO Auto-generated method stub
           Toast.makeText(getApplicationContext(), "Service Working",1).show();
           timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
           Log.i("Le service tourne", "Service_OK");
           return super.onStartCommand(intent, flags, startId);
    }
    

    
    @SuppressLint("HandlerLeak") private final Handler toastHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
        	hash = checkHashURL("http://www.url_enquestion/example.jpg");
           // Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
            try {
            	if (hash != null){
            	Toast.makeText(getApplicationContext(), "Hash is " + hash ,1).show();
            	}
            } catch (Exception em){
            	em.printStackTrace();
            }
        }
    };    
  
    
}
j'ai mis en rouge la ligne qui semblait provoquer l'erreur.
l'url apparait deux fois parce que google suggérait un problème de synchro / threads (j'ai bêtement essayé les deux endroits).
en résumé, je veux bien qu'il ne récupère pas le hash offline, mais j'aimerais qu'il continue à tourner.

merci pour vos réponses