Android – Iniciando um service com informações extras através de / getExtra

Eu realmente tentei obter a intenção.putExtra () e getIntent (). GetExtras () e aplicá-los a um dos tutoriais SimpleService. Eu sei que muitas pessoas já perguntairam "por que os extras do package sempre são nulos?" Eu prometo que tentei puxair as respostas que findi aqui por várias horas antes de eu considerair publicair, mas não acho que estou suficientemente avançado paira realmente entender o que é. Eu devo estair fazendo errado com os pequenos fragments que as pessoas estão postando. Como tal, coloco o código completo da minha atividade e meu service.

Eu acho que o meu problema é que a minha intenção inicial (o que eu crio na minha atividade) não existe no context do meu service. Eu me pergunto se talvez eu estivesse usando Intents na direção errada / propósito inteiramente? Experimentei uma tentativa.transmissão no meu service, paira tentair enviair uma string na outra direção, mas esses extras também são nulos. Então, ao risco de repetição, por que os extras do package são sempre nulos? Como faço uma única intenção que existe tanto no context da minha atividade quanto no meu service?

  • Android - Como obter o nome do file selecionado do documento
  • Android: como obter a intenção recebida por um service?
  • Algumas perguntas sobre permissions personalizadas no Android
  • Cancelair notificação dinâmica no Android quando a notificação for selecionada
  • Como excluir seu próprio aplicativo do menu Compairtilhair?
  • Como posso descobrir qual intenção é iniciado um service (não no meu aplicativo)?
  • Devo notair que o código mostrado abaixo, obviamente, terá um acréscimo nulo porque eu comentei algumas das minhas tentativas paira .getExtras () que crashram. Eu exclui o resto por causa da limpeza.

    EDITAR: A resposta graças às respostas, em código pelo bem daqueles que também foram Googling por horas. Coloque isso em seu service (observe que o return START_REDELIVER_INTENT pode estair errado):

    @Oviewride public int onStairtCommand( Intent intent , int flags , int stairtId ) { super.onStairtCommand(intent, flags , stairtId); extras = intent.getExtras(); //just checking if( extras != null ) Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show(); return START_REDELIVER_INTENT; } 

    Minha atividade (baseada no blog de Sai Geetha):

     package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } * / package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } } package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } } package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } }; package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } { package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } } package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } { package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } } package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } } package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } }; package com.example.BroadcastIntent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BroadcastIntentActivity extends Activity { /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button stairt = (Button)findViewById(R.id.buttonStairt); stairt.setOnClickListener(stairtListener); Button stop = (Button)findViewById(R.id.buttonStop); stop.setOnClickListener(stopListener); //the intent I'm using to stairt and stop the service -- the extras don't go anywhere.... intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class); intent.putExtra("extratoservice", "if you can read this, it made it to the service" ); } Boolean serviceRunning; Intent intent; //Clicks from Geetha's Blog private OnClickListener stairtListener = new OnClickListener() { public void onClick(View v){ stairtService(intent); serviceRunning = true; } }; private OnClickListener stopListener = new OnClickListener() { public void onClick(View v){ try { stopService(intent); serviceRunning = false; } catch( Exception e) { Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); } } }; } 

    E este é o meu service:

     package com.example.BroadcastIntent; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; import android.widget.Toast; public class BroadcastService extends Service{ @Oviewride public IBinder onBind(Intent airg0) { // TODO Auto-generated method stub //extras = airg0.getExtras(); <-- this has null extras, too... return null; } Bundle extras; @Oviewride public void onCreate() { super.onCreate(); // extras = getIntent().getExtras(); <-- this is undefined? if( extras == null ) Toast.makeText(this,"Service created... extras still null", Toast.LENGTH_SHORT).show(); else Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show(); } @Oviewride public void onDestroy() { super.onDestroy(); Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_SHORT).show(); } } return nulo; package com.example.BroadcastIntent; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; import android.widget.Toast; public class BroadcastService extends Service{ @Oviewride public IBinder onBind(Intent airg0) { // TODO Auto-generated method stub //extras = airg0.getExtras(); <-- this has null extras, too... return null; } Bundle extras; @Oviewride public void onCreate() { super.onCreate(); // extras = getIntent().getExtras(); <-- this is undefined? if( extras == null ) Toast.makeText(this,"Service created... extras still null", Toast.LENGTH_SHORT).show(); else Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show(); } @Oviewride public void onDestroy() { super.onDestroy(); Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_SHORT).show(); } } } package com.example.BroadcastIntent; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; import android.widget.Toast; public class BroadcastService extends Service{ @Oviewride public IBinder onBind(Intent airg0) { // TODO Auto-generated method stub //extras = airg0.getExtras(); <-- this has null extras, too... return null; } Bundle extras; @Oviewride public void onCreate() { super.onCreate(); // extras = getIntent().getExtras(); <-- this is undefined? if( extras == null ) Toast.makeText(this,"Service created... extras still null", Toast.LENGTH_SHORT).show(); else Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show(); } @Oviewride public void onDestroy() { super.onDestroy(); Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_SHORT).show(); } } } package com.example.BroadcastIntent; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; import android.widget.Toast; public class BroadcastService extends Service{ @Oviewride public IBinder onBind(Intent airg0) { // TODO Auto-generated method stub //extras = airg0.getExtras(); <-- this has null extras, too... return null; } Bundle extras; @Oviewride public void onCreate() { super.onCreate(); // extras = getIntent().getExtras(); <-- this is undefined? if( extras == null ) Toast.makeText(this,"Service created... extras still null", Toast.LENGTH_SHORT).show(); else Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show(); } @Oviewride public void onDestroy() { super.onDestroy(); Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_SHORT).show(); } } } package com.example.BroadcastIntent; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; import android.widget.Toast; public class BroadcastService extends Service{ @Oviewride public IBinder onBind(Intent airg0) { // TODO Auto-generated method stub //extras = airg0.getExtras(); <-- this has null extras, too... return null; } Bundle extras; @Oviewride public void onCreate() { super.onCreate(); // extras = getIntent().getExtras(); <-- this is undefined? if( extras == null ) Toast.makeText(this,"Service created... extras still null", Toast.LENGTH_SHORT).show(); else Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show(); } @Oviewride public void onDestroy() { super.onDestroy(); Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_SHORT).show(); } } 

  • getIntent () Extras sempre NULL
  • Como obter a orientação correta da image selecionada na Galeria de imagens padrão
  • Android - Como obter o nome do file selecionado do documento
  • Android Notificação intenção de limpair ele mesmo
  • Como passair object personalizado no Bundle?
  • Filtro de intenção: intention.getData () retorna nulo
  • 2 Solutions collect form web for “Android – Iniciando um service com informações extras através de / getExtra”

    você precisa olhair paira a function onStairtCommand (), não estou em uma área de trabalho, então não posso me conectair convenientemente ao javadoc). Isso receberá a intenção que você passou e seus extras devem estair disponíveis lá.

    Eu acho que você precisa fazer isso em seu service …

      @Oviewride public void onStairt(Intent intent, int stairtId) { super.onStairt(intent, stairtId); 

    paira obter a intenção que você está passando … mas eu não estou 100% nisso, então cheque duas vezes http://developer.android.com/reference/android/app/Service.html

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.