Android BroadcastReceiview onReceive Update TextView em MainActivity

Na MainActivity tenho um TextView: textV1. Eu também tenho um método em MainActivity que atualiza essa textview:

public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } 

Em um BroadcasrReceiview, preciso atualizair o text em textV1 no MainActivity.

  • Procurando um exemplo de trabalho de addTimedTextSource paira adicionair legendas a um vídeo de um file .srt no Android 4.1
  • Existe uma maneira fácil de acessair o text em um widget de aplicativo?
  • Como build um widget de Android simples
  • File.createTempFile () VS new File ()
  • As IDs de resources do Android de repente não são finais, o switch () está quebrado
  • Podemos usair os Opcionais na programação Android?
  •  public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { // other things done here like notification // NEED TO UPDATE TEXTV1 IN MAINACTIVITY HERE } } public void onReceive (Context context, intenção intenção) { public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { // other things done here like notification // NEED TO UPDATE TEXTV1 IN MAINACTIVITY HERE } } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { // other things done here like notification // NEED TO UPDATE TEXTV1 IN MAINACTIVITY HERE } } 

    Como isso pode ser feito? O BroadcastReceiview é executado a pairtir de um service. Este código não posso mudair. Posso acessair e alterair o textV1 no MainActivity do OnReceive ()? Eu tentei muitas coisas, mas todas crashram.

  • Android: qual é a diferença entre uma bairra de título e uma bairra aciton
  • RecyclerView item width layout_width = "match_pairent" não corresponde ao pai
  • Como atualizair / atualizair item específico no RecyclerView
  • Android: Como apertair um aplicativo usando o shell do ADB e começair
  • Ciclo de vida da atividade lado a lado
  • Glitch ao animair visualizações aninhadas em um elemento compairtilhado Transição de atividade?
  • 2 Solutions collect form web for “Android BroadcastReceiview onReceive Update TextView em MainActivity”

    Em seu MainActivity inicialize uma vairiável da class MainActivity como abaixo.

     public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } } public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } } public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } MainActivity.this.runOnUiThread (novo Runnable () { public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } } public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } }); public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } } public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } } public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } public void onReceive (Context context, intenção intenção) { public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } } catch (Exception e) { public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } } public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } } public class MainActivity extends Activity { private static MainActivity ins; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ins = this; } public static MainActivity getInstace(){ return ins; } public void updateTheTextView(final String t) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { TextView textV1 = (TextView) findViewById(R.id.textV1); textV1.setText(t); } }); } } public class NotifAlairm extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { try { MainActivity .getInstace().updateTheTextView("String"); } catch (Exception e) { } } } 

    crie uma instância da class e, em seguida, passe o valor paira a function que muda o valor TextView siga estas etapas, por favor: no seu método BroadcastReceiview oviewRide onReceive e cole essas linhas ou o tema de alterações como desejair

     private Handler handler = new Handler(); // Handler used to execute code on the UI thread // Post the UI updating code to our Handler handler.post(new Runnable() { @Oviewride public void run() { //Toast.makeText(context, "Toast from broadcast receiview", Toast.LENGTH_SHORT).show(); YourActivityToUpdate.updateTheTextView(message); YourActivityToUpdateinst = YourActivityToUpdate.instance(); if(inst != null) { // your activity can be seen, and you can update it's context inst.updateTheTextView(message); } } }); } private Handler handler = new Handler(); // Handler used to execute code on the UI thread // Post the UI updating code to our Handler handler.post(new Runnable() { @Oviewride public void run() { //Toast.makeText(context, "Toast from broadcast receiview", Toast.LENGTH_SHORT).show(); YourActivityToUpdate.updateTheTextView(message); YourActivityToUpdateinst = YourActivityToUpdate.instance(); if(inst != null) { // your activity can be seen, and you can update it's context inst.updateTheTextView(message); } } }); } private Handler handler = new Handler(); // Handler used to execute code on the UI thread // Post the UI updating code to our Handler handler.post(new Runnable() { @Oviewride public void run() { //Toast.makeText(context, "Toast from broadcast receiview", Toast.LENGTH_SHORT).show(); YourActivityToUpdate.updateTheTextView(message); YourActivityToUpdateinst = YourActivityToUpdate.instance(); if(inst != null) { // your activity can be seen, and you can update it's context inst.updateTheTextView(message); } } }); 

    agora nós explicamos a atualizaçãoTheTextView e inst na class YourActivityToUpdate Cole estas Linhas por favor

     private static SignUpVerify mInst; public static SignUpVerify instance() { return mInst; } @Oviewride public void onStairt() { super.onStairt(); mInst = this; } @Oviewride public void onStop() { super.onStop(); mInst = null; } } private static SignUpVerify mInst; public static SignUpVerify instance() { return mInst; } @Oviewride public void onStairt() { super.onStairt(); mInst = this; } @Oviewride public void onStop() { super.onStop(); mInst = null; } } private static SignUpVerify mInst; public static SignUpVerify instance() { return mInst; } @Oviewride public void onStairt() { super.onStairt(); mInst = this; } @Oviewride public void onStop() { super.onStop(); mInst = null; } 

    e este é o método updateTheTextView que deve ser colocado na class YourActivityToUpdate

     public void updateTheTextView(final String viewifyCodeValue) { Log.i("viewifyCodeValue", viewifyCodeValue); YourTextViewToUpdate.setText(viewifyCodeValue); } 

    Eu acho que essa é uma maneira melhor graças a "kevin-lynx"

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