Domanda

Ho usato con successo Intentvice un paio di volte, ma nell'app correnti, non posso ottenere l'onreceive di BroadcaStreceiver () per iniziare.Ho la stringa esatta nell'azione dell'intento degli Intentvice e nell'intentfilter registrato in onresume insieme al BroadcaStreceiver.

    filter = new IntentFilter("com.currencyconverter2.intent.action.MESSAGE");
    Log.d("CC2", "intent filter created");
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    Log.d("CC2", "filter category added");
    receiver = new MessageReceiver();
    Log.d("CC2", "receiver instantiated");
}

public void onResume(){
    super.onResume();
    registerReceiver(receiver, filter);
    Log.d("CC2", "receiver registered");
}

public void onPause(){
    super.onPause();
    unregisterReceiver(receiver);
    Log.d("CC2", "receiver unregistered");
}

public class MessageReceiver extends BroadcastReceiver{
    public void onReceive(Context context, Intent intent){
        Log.d(Tag, "entered onReceive...");
        String html = intent.getStringExtra("OUTPUT");
        webView.setBackgroundColor(Color.parseColor("#f4f36f"));
        webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
    }
}
.

Ho anche specificato il servizio in manifesto.xml.

<service android:name=".IntentServiceTest"></service>
.

Il mio log mi dice che l'intentoadcast è stato trasmesso dall'intentivice, e l'app si è fermata lì.BroadcaStreceiver non sembrava averlo ricevuto.Cosa può essere sbagliato?Grazie!

Modifica:

My Intentvice

        Intent broadcastIntent = new Intent();
        Log.d(Tag, "setting intent action");
        broadcastIntent.setAction("com.currencyconverter2.intent.action.MESSAGE");
        Log.d(Tag, "adding intent category");
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        Log.d(Tag, "putting intent extra");
        broadcastIntent.putExtra("OUTPUT", data.text());
        Log.d(Tag, "sending broadcast");
        sendBroadcast(intent);

    }catch(Exception e){
        e.printStackTrace();
        //System.out.println("f");
    }
}

}
.

Modifica 2:

ha trovato il bug.

                          Intent broadcastIntent = new Intent();
        Log.d(Tag, "setting intent action");
        broadcastIntent.setAction("com.currencyconverter2.intent.action.MESSAGE");
        Log.d(Tag, "adding intent category");
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        Log.d(Tag, "putting intent extra");
        broadcastIntent.putExtra("OUTPUT", data.text());
        Log.d(Tag, "sending broadcast");
        sendBroadcast(intent);
.

dovrei avere sendbroadcast (BroadcastIntent);invece.

È stato utile?

Soluzione

Controlla il tuo AndroidManifest.xml:

<receiver android:name=".MessageReceiver">
<intent-filter>
<action android:name="com.currencyconverter2.intent.action.MESSAGE" />
<category android:name="android.intent.category.Default" />
</intent-filter>
</receiver>
.

^ - ^

Altri suggerimenti

INTENTSTERVICE Utilizzare per fare compito e spegnere, ma comePAR il tuo codice Sembra che tu stia usando un lungo tempo di funzionamento, usa Servizio invecedi Intentvice

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top