grofs Posted May 3, 2010 Share Posted May 3, 2010 Bonjour je développe actuellement une appli pour la réception des sms, Le truc c'est que j'arrive pas à faire marcher mon service au démarrage de mon appli Voici le code du service : package com.grofs.sms; import com.grofs.sms.Main.Receiver; import com.grofs.sms.R.string; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Binder; import android.os.IBinder; import android.os.Vibrator; import android.telephony.SmsManager; import android.widget.Toast; public class reveive_texto extends Service { public static final String EVENT_ACTION = "Texto"; Vibrator vibrator; public class MyBinder extends Binder{ reveive_texto getService(){ return reveive_texto.this; } } @Override public IBinder onBind(Intent arg0) { return new MyBinder(); } @Override public void onCreate() { //SMS vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); SmsManager smsManager = SmsManager.getDefault(); Receiver receiver = new Receiver(); registerReceiver(receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED")); //Avertissement pour prévenir que le service est bien lancé Toast.makeText(this, "Texto marche maintenant en tâche de fond", 3000).show(); Intent intent = new Intent (EVENT_ACTION); super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override public void onStart(Intent intent, int startId) { Toast.makeText(this, "Texto marche maintenant en tâche de fond", 3000).show(); super.onStart(intent, startId); } public class Receiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Intent intent1 = new Intent (context, TextoReceive.class); startActivity(intent1); Toast.makeText(context, R.string.texto , Toast.LENGTH_SHORT).show(); vibrator.vibrate(500); } } } Si vous savez comment faire merci d'avance ;) Quote Link to comment Share on other sites More sharing options...
Igosuki Posted May 3, 2010 Share Posted May 3, 2010 Peux tu donner l'erreur sortie par logCat ou ton autre outil de debug ? Quote Link to comment Share on other sites More sharing options...
naholyr Posted May 3, 2010 Share Posted May 3, 2010 (edited) Il faut recevoir l'évènement "Boot", et démarrer ton service lors de la réception de cet évènement : À ajouter dans ton AndroidManifest.xml <?xml version="1.0" encoding="UTF-8"?> package="com.naholyr.wakeuplight" xmlns:android="http://schemas.android.com/apk/res/android"> ... ... ... ... Et le code de ton BootReceiver.java public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent bootintent) { // Start battery monitoring service context.startService(new Intent("mon.package.MonService)); } } A priori je verrais deux raisons qui pourraient faire que ça ne marche pas chez toi : Tu fais un startActivity au lieu d'un startService. Même pas sûr que ce soit totalement impossible de faire un startActivity au boot, ce n'est de toute façon pas ce que tu souhaites faire. Tu n'as peut-être pas ajouté la permission nécessaire dans ton manifest, ou mal déclaré ton receiver dans le manifest. Edited May 3, 2010 by naholyr Quote Link to comment Share on other sites More sharing options...
Igosuki Posted May 3, 2010 Share Posted May 3, 2010 A noter que tu peux changer l'intent filter selon tes besoins ;) Quote Link to comment Share on other sites More sharing options...
grofs Posted May 3, 2010 Author Share Posted May 3, 2010 J'ai essayé mais ça ne marche toujours pas Voici mon manifest: <?xml version="1.0" encoding="utf-8"?> package="com.grofs.sms" android:versionCode="6" android:versionName="0.4.1 BETA"> android:theme="@android:style/Theme.NoTitleBar" android:label="@string/app_name"> android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:label="@string/app_name"> android:theme="@android:style/Theme.NoTitleBar" android:label="@string/app_name"> Quote Link to comment Share on other sites More sharing options...
naholyr Posted May 3, 2010 Share Posted May 3, 2010 Et la sortie de ton logcat ? Quote Link to comment Share on other sites More sharing options...
Igosuki Posted May 3, 2010 Share Posted May 3, 2010 Logcat c'est la log de ton AVM Quote Link to comment Share on other sites More sharing options...
grofs Posted May 4, 2010 Author Share Posted May 4, 2010 Je me souviens pas d'avoir trouvé l'erreur si vous voulez je peux vous donner tout mon logcat cet aprem quand je serais chez moi ;) Quote Link to comment Share on other sites More sharing options...
Igosuki Posted May 4, 2010 Share Posted May 4, 2010 StartupBroadcastRecevier c'est le bon nom ? il y a une faute d'ortho on dirait Quote Link to comment Share on other sites More sharing options...
grofs Posted May 4, 2010 Author Share Posted May 4, 2010 Non je comfirme je vois aucune erreur dans le logcat peut être parce que j'ai mal lancé le service :s Voici le code main: package com.grofs.sms; import android.app.Activity; import android.app.AlertDialog; import android.app.PendingIntent.OnFinished; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Vibrator; import android.telephony.SmsManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class Main extends Activity implements OnClickListener { Vibrator vibrator; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent bindIntent = new Intent(Main.this, reveive_texto.class); vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); SmsManager smsManager = SmsManager.getDefault(); } [..........................] } Quote Link to comment Share on other sites More sharing options...
Igosuki Posted May 4, 2010 Share Posted May 4, 2010 StartupBroadcastRecevier est le bon nom de classe ? Quote Link to comment Share on other sites More sharing options...
grofs Posted May 4, 2010 Author Share Posted May 4, 2010 Oui oui parce que justement je l'ai testé en faisant démarrer mon appli au lancement du téléphone et ça marche ;) Quote Link to comment Share on other sites More sharing options...
Igosuki Posted May 4, 2010 Share Posted May 4, 2010 Alors c'est peut être un problème de Bind, car à vu de nez je ne vois pas ce qui cloche. Quote Link to comment Share on other sites More sharing options...
grofs Posted May 4, 2010 Author Share Posted May 4, 2010 Un problème de Bind ? c'est à dire ? Quote Link to comment Share on other sites More sharing options...
Igosuki Posted May 4, 2010 Share Posted May 4, 2010 Pour utiliser un service il faut le lier, ton MyBinder sert à ça. Quote Link to comment Share on other sites More sharing options...
grofs Posted May 5, 2010 Author Share Posted May 5, 2010 Humm possible, j'ai regardé des exemples de service mais mon MyBinder et correct en principe. Mais je pense que c'est un probleme lors du lancement du service. Parce que le log n'affiche pas le service Quote Link to comment Share on other sites More sharing options...
Igosuki Posted May 5, 2010 Share Posted May 5, 2010 Tu retournes un MyBinder lorsqu'une Activity fait un bindservice. Si le code te parrait 100% bon alors ça doit venir de la config, peut être des permissions ? Déjà dans ton receiver, tu devrais vérifier que le l'intent que tu recois correspond à android.intent.action.BOOT_COMPLETED. Ensuite je te conseille de récupérer la valeur de context.startservice dans un ComponentName ou autre chose et de loguer une erreur si il est null. Cela te permettra de vérifier déjà que lors que ton instance de BootReceiver reçoit un broadcast, eh bien le service est déjà actif. Pour la log utilise Log.e (méthode statique des logs android). Si ça ne log rien, alors essaie de rajouter android:enabled="true" en attribut de ton receiver dans le manifest Quote Link to comment Share on other sites More sharing options...
grofs Posted May 5, 2010 Author Share Posted May 5, 2010 Bon je suis débutant sur Android, donc j'ai à peu près compris ce que tu as dit sauf : Ensuite je te conseille de récupérer la valeur de context.startservice dans un ComponentName ou autre chose et de loguer une erreur si il est null. Je comprend pas trop comment faire :s Quote Link to comment Share on other sites More sharing options...
naholyr Posted May 5, 2010 Share Posted May 5, 2010 Et bien à un moment tu dois démarrer ton service avec context.startService(...) il faut que tu récupères le résultat pour le traiter var = context.startService(...) if (var == null) { erreur() } au fil des messages j'ai un peu peur que tu ne comprennes pas vraiment ce que tu es en train de faire ;) Si tu pouvais nous donner le code complet (le vrai, pas celui que tu nous as donné au début où tu fais un startActivity au lieu d'un startService par exemple) je pense qu'on trouverait rapidement les anomalies. Quote Link to comment Share on other sites More sharing options...
grofs Posted May 5, 2010 Author Share Posted May 5, 2010 ^^ c'est pour ça que je demande parce que je comprend pas trop mais je comprend bien quand tu expliques Voici le code de BootReceive : package com.grofs.sms; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class BootReceiver extends BroadcastReceiver { public void onReceive(final Context context, final Intent bootintent) { // Start battery monitoring service context.startService(new Intent(context, reveive_texto.class)); Toast.makeText(context, "Test de démarage ;)", 3000).show(); } } La le code du service lui même : package com.grofs.sms; import com.grofs.sms.R.string; import com.grofs.sms.R.xml; import android.app.Service; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.os.Vibrator; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; import android.widget.TextView; import android.widget.Toast; public class reveive_texto extends Service implements ServiceConnection { TextView text; public static final String EVENT_ACTION = "Texto"; private final IBinder binder = new MyBinder(); private reveive_texto serviceBinder; Vibrator vibrator; public class MyBinder extends Binder{ reveive_texto getService(){ return reveive_texto.this; } } @Override public IBinder onBind(Intent intent) { return binder; } @Override public void onCreate() { //SMS vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); SmsManager smsManager = SmsManager.getDefault(); Receiver receiver = new Receiver(); Log.i("Salut", "[color=red][b]sa[/b][/color] va ?"); text.findViewById(R.id.TextView01); registerReceiver(receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED")); //Avertissement pour prévenir que le service est bien lancé Toast.makeText(this, "Texto marche maintenant en tâche de fond", 3000).show(); Intent intent = new Intent (EVENT_ACTION); super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override public void onStart(Intent intent, int startId) { Toast.makeText(this, "Texto marche maintenant en tâche de fond", 3000).show(); super.onStart(intent, startId); } public class Receiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Intent intent1 = new Intent (context, TextoReceive.class); startActivity(intent1); Toast.makeText(context, R.string.texto , Toast.LENGTH_SHORT).show(); vibrator.vibrate(500); text.setText("Message"); if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){ Bundle bundle = intent.getExtras(); if(bundle != null){ Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage[] messages = new SmsMessage[pdus.length]; for(int i = 0; i messages[i]=SmsMessage.createFromPdu((byte[])pdus[i]); for(SmsMessage message : messages){ Log.i("",message.getOriginatingAddress()+"::"+message.getMessageBody()); } } } } public void onServiceConnected(ComponentName arg0, IBinder arg1) { Log.i("Receive_texto", "Service lancé"); } public void onServiceDisconnected(ComponentName name) { Log.i("ServiceCall", "Service terminé"); } } public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub } public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } } Quote Link to comment Share on other sites More sharing options...
grofs Posted May 5, 2010 Author Share Posted May 5, 2010 Ah est pour le log il y'a un petit problème : Il n'affiche plus rien Quote Link to comment Share on other sites More sharing options...
grofs Posted May 6, 2010 Author Share Posted May 6, 2010 Bon mon logcat à arrêter de me causer des problèmes, j'ai enfin trouvé une erreur voila la capture d'écran que j'ai prise : Si ça peux vous aider un petit peux ;) et vous inquietez pas pour le " android:exported="true"" je l'ai enlevé après :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.