Autore Topic: Tentativo service  (Letto 467 volte)

Offline Rino63

  • Utente normale
  • ***
  • Post: 167
  • Respect: +3
    • Mostra profilo
Tentativo service
« il: 22 Maggio 2014, 18:36:06 CEST »
0
ciao, vorrei provare a creare un servizio che viene lanciato all'avvio del cellulare. girovagando per la rete ho trovato un esempio che però non funziona. all'avvio il servizio non viene avviato e se tento lanciare l'app esce il messaggino:"l'applicazione è terminata in maniera imprevista"
riporto l'intero codice dell'esempio:

Codice (Java): [Seleziona]
package com.helloworldreceiver.it;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
 
public class BootReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
                Intent serviceIntent = new Intent();
                serviceIntent.setAction("com.helloworldreceiver.it.Receiver");
                context.startService(serviceIntent);
        }
}

Codice (Java): [Seleziona]
package com.helloworldreceiver.it;
     
    import android.app.Activity;
    import android.os.Bundle;
     
    public class MainActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }

Codice (Java): [Seleziona]
package com.helloworldreceiver.it;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
 
public class Receiver extends Service {
        @Override
        public IBinder onBind(Intent intent) {
                return null;
        }
 
        @Override
        public void onCreate() {
                super.onCreate();
        }
 
        @Override
        public void onDestroy() {
                super.onDestroy();
        }
 
        @Override
        public void onStart(Intent intent, int startId) {
                super.onStart(intent, startId);
                Toast.makeText(getApplicationContext(), (String)getText(R.string.hello_world), Toast.LENGTH_LONG).show();
        }
}

il layout della main activity:

Codice (XML): [Seleziona]
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
    <TextView  
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/hello_world"
      />
    </LinearLayout>

ed ovviamente il manifest

Codice: [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.helloworldreceiver.it"
         android:versionCode="1"
         android:versionName="1.0">
        <uses-sdk android:minSdkVersion="3" />
     
        <application android:icon="@drawable/ic_launcher" 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>
           
                    <receiver android:name="BootReceiver">
                            <intent-filter>
                                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                                    <category android:name="android.intent.category.HOME" />
                            </intent-filter>
                    </receiver>
                   
                    <service android:name="Receiver">
                            <intent-filter>
                                    <action android:name="com.helloworldreceiver.it.Receiver" />
                            </intent-filter>
                    </service>
                   
        </application>
    </manifest>

Qualcuno saprebbe dirmi perchè non funziona? grazie per l'attenzione

Offline bradipao

  • Moderatore globale
  • Utente storico
  • *****
  • Post: 4043
  • keep it simple
  • Respect: +567
    • Github
    • Google+
    • bradipao
    • Mostra profilo
  • Dispositivo Android:
    Nexus 5
  • Play Store ID:
    Bradipao
  • Sistema operativo:
    W7
Re:Tentativo service
« Risposta #1 il: 22 Maggio 2014, 18:47:03 CEST »
0
Quando si ha a che fare con gli eventi BOOT_COMPLETED non è mai semplice, la mia app ogni tanto ne riceveva due, di cui solo il secondo aveva il context buono.

Comunque, armati di LogCat e fai debug di cosa accade, e per fare il debug di BOOT_COMPLETED, usa questo:

Generare eventi BOOT_COMPLETED da ADB - Android Developers Italia
NON rispondo a domande nei messaggi privati
Bradipao @ Play Store

Offline bart

  • Utente junior
  • **
  • Post: 133
  • Respect: 0
    • highlanderteam
    • BBartsv
    • Mostra profilo
  • Dispositivo Android:
    samsung galaxy tab
  • Play Store ID:
    barth
  • Sistema operativo:
    ubuntu 11.04,mac os leopard,windows7
Re:Tentativo service
« Risposta #2 il: 22 Maggio 2014, 19:15:27 CEST »
0
Nel manifest hai messo
Codice (Java): [Seleziona]
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
   
io per esempio nel broadcast receiver attivo il PowerManager e risveglio il cellulare  ;-) poi faccio
Codice (Java): [Seleziona]
Coontext c passato come argomento;
              AlarmManager al = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
              int alType = AlarmManager.RTC_WAKEUP;
              Calendar now = Calendar.getInstance();//attivo alarm manager e li setto quando deve partire dopo che l'utente ha acceso il dispositivo
              long triggerAtTime = now.getTimeInMillis()+10000;
              Intent intentToFire = new Intent(c,Ora.class);//classe del service
              int g=1000*60*30;
              Log.i(""+g, "setto allarme");
              PendingIntent pendingIntent = PendingIntent.getService(c,0,intentToFire,0);/Intent da far partire
              al.setRepeating(alType, triggerAtTime,g, pendingIntent);//quando deve partire e con quale frequenza
             
Utilizzando questo codice nelle mie applicazioni ottengo un service che mi aggiorna in modo automatico i risultati delle gare di motocross e mi invia le notifiche ai dispositivi iOS che non hanno i service

Offline Rino63

  • Utente normale
  • ***
  • Post: 167
  • Respect: +3
    • Mostra profilo
Re:Tentativo service
« Risposta #3 il: 22 Maggio 2014, 21:00:22 CEST »
0
Nell'emulatore parte o meglio viene visualizzata l'activity(ma non dovrebbe visualizzare un toast e basta?). questo il ligcat......... ho aggiunto anche il permesso wak_lock ma nulla da fare si blocca sempre in modo anomale(almeno così dice android......

Codice (XML): [Seleziona]
05-22 18:56:34.163: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:34.253: W/ActivityThread(551): Application com.helloworldreceiver.it is waiting for the debugger on port 8100...
05-22 18:56:34.264: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:34.383: I/System.out(551): Sending WAIT chunk
05-22 18:56:34.403: I/dalvikvm(551): Debugger is active
05-22 18:56:34.583: I/System.out(551): Debugger has connected
05-22 18:56:34.593: I/System.out(551): waiting for debugger to settle...
05-22 18:56:34.763: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:34.783: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:34.793: I/System.out(551): waiting for debugger to settle...
05-22 18:56:34.993: I/System.out(551): waiting for debugger to settle...
05-22 18:56:35.193: I/System.out(551): waiting for debugger to settle...
05-22 18:56:35.314: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:35.403: I/System.out(551): waiting for debugger to settle...
05-22 18:56:35.524: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:35.603: I/System.out(551): waiting for debugger to settle...
05-22 18:56:35.783: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:35.793: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:35.803: I/System.out(551): waiting for debugger to settle...
05-22 18:56:36.003: I/System.out(551): waiting for debugger to settle...
05-22 18:56:36.203: I/System.out(551): waiting for debugger to settle...
05-22 18:56:36.293: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:36.303: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:36.413: I/System.out(551): waiting for debugger to settle...
05-22 18:56:36.613: I/System.out(551): waiting for debugger to settle...
05-22 18:56:36.793: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:36.815: I/System.out(551): waiting for debugger to settle...
05-22 18:56:36.815: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:37.019: I/System.out(551): debugger has settled (1360)
05-22 18:56:37.303: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:37.393: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:37.803: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:37.843: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:38.303: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:38.343: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:38.803: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:38.823: D/dalvikvm(551): threadid=1: still suspended after undo (sc=1 dc=1)
05-22 18:56:38.823: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:39.314: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:39.353: D/dalvikvm(551): threadid=1: still suspended after undo (sc=1 dc=1)
05-22 18:56:39.353: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:39.823: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:39.834: D/dalvikvm(551): threadid=1: still suspended after undo (sc=1 dc=1)
05-22 18:56:39.844: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:40.314: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:40.334: D/dalvikvm(551): threadid=1: still suspended after undo (sc=1 dc=1)
05-22 18:56:40.334: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:40.814: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:40.834: D/dalvikvm(551): threadid=1: still suspended after undo (sc=1 dc=1)
05-22 18:56:40.834: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:41.314: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:41.344: D/dalvikvm(551): threadid=1: still suspended after undo (sc=1 dc=1)
05-22 18:56:41.344: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:41.814: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:41.844: D/dalvikvm(551): threadid=1: still suspended after undo (sc=1 dc=1)
05-22 18:56:41.844: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:42.314: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:42.334: D/dalvikvm(551): threadid=1: still suspended after undo (sc=1 dc=1)
05-22 18:56:42.334: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:42.824: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:42.844: D/dalvikvm(551): threadid=1: still suspended after undo (sc=1 dc=1)
05-22 18:56:42.844: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
05-22 18:56:43.324: I/dalvikvm(551): threadid=3: reacting to signal 3
05-22 18:56:43.364: D/dalvikvm(551): threadid=1: still suspended after undo (sc=1 dc=1)
05-22 18:56:43.364: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'

Post unito: 22 Maggio 2014, 21:23:18 CEST
risolto aggiungendo i due permessi citati da hart funziona, grazie !!!!!!!!!!
« Ultima modifica: 22 Maggio 2014, 21:23:18 CEST da Rino63, Reason: Merged DoublePost »

Offline bart

  • Utente junior
  • **
  • Post: 133
  • Respect: 0
    • highlanderteam
    • BBartsv
    • Mostra profilo
  • Dispositivo Android:
    samsung galaxy tab
  • Play Store ID:
    barth
  • Sistema operativo:
    ubuntu 11.04,mac os leopard,windows7
Re:Tentativo service
« Risposta #4 il: 22 Maggio 2014, 23:31:50 CEST »
0
Funziona o no? ;-)

Offline Rino63

  • Utente normale
  • ***
  • Post: 167
  • Respect: +3
    • Mostra profilo
Re:Tentativo service
« Risposta #5 il: 23 Maggio 2014, 10:25:23 CEST »
0
si certo funziona perfettamente. bisognava aggiungere nel manifest i permessi che avevi indicato tu. 

EDIT: Per completezza aggiungo il manifest corretto.

Codice: [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.helloworldreceiver.it"
         android:versionCode="1"
         android:versionName="1.0">
        <uses-sdk android:minSdkVersion="3" />
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />
        <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:permission="android.permission.WAKE_LOCK">
            <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>
           
                    <receiver android:name="BootReceiver">
                            <intent-filter>
                                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                                   
                                    <category android:name="android.intent.category.HOME" />
                            </intent-filter>
                    </receiver>
                   
                    <service android:name="Receiver">
                            <intent-filter>
                                    <action android:name="com.helloworldreceiver.it.Receiver" />
                            </intent-filter>
                    </service>
                   
        </application>
    </manifest>
« Ultima modifica: 23 Maggio 2014, 10:50:14 CEST da Rino63 »