2016-08-16 4 views
1

메시지를 수신하자마자 수신자 측의 텍스트보기를 업데이트하고 싶습니다. 주요 활동의 인스턴스를 만들고 방송 수신기에서 UI를 업데이트하는 다음 코드를 작성했습니다. 그러나 텍스트보기가 업데이트되지 않습니다 ?? smsreceiver 클래스에서브로드 캐스트 수신기의 TextView 업데이트

public class Mainactivity extends activity{ 
private static MainActivity ins; 

public static MainActivity getInst() 
{ 
    return ins; 
} 
protected void onCreate(Bundle savedInstanceState) { 
ins=this;} 
public void updateUI(final String s) 
{ 
MainActivity.this.runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     TextView textView=(TextView) findViewById(R.id.textView); 
     textView.setText(s); 
    } 
}); 
} 

것은

public class smsreceiver extends BroadcastReceiver 
{ 
try{ 

      if (MainActivity.getInst()!=null) 
       MainActivity.getInst().updateUI(str); 

     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 
} 

저를 도와주세요!

+0

은 어디'된 setContentView()'와'super.onCreate()'입니까? – Opiatefuchs

+0

해결 되었습니까? –

+0

내 앱에 우선 순위 문제가 있습니다. 우선 순위가 높은 SMS 앱이이 앱에서 메시지를받지 못했습니다. 이 코드는 UI 업데이트에 잘 작동합니다. 어쨌든 고마워. – user3170131

답변

0

아래처럼하려고하면 무엇 :

public class Mainactivity extends activity{ 

    protected void onCreate(Bundle savedInstanceState) { 
     setContentView(R.layout.your_layout); 
     //register your receiver 
    } 

    protected void onDestroy() { 

     //unregister your receiver 
     super.onDestroy(); 
    } 
    public void updateUI(final String s) 
    { 
     MainActivity.this.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       TextView textView=(TextView) findViewById(R.id.textView); 
       textView.setText(s); 
      } 
     }); 
    } 

    private class smsreceiver extends BroadcastReceiver 
    { 
     try{ 

      updateUI(str) 

     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 
0

이 내 코드베이스는 당신이

공용 클래스 MainActivity는 활동 {

private static MainActivity ins; 

private Button mButton; 

public static MainActivity getInst() 
{ 
    return ins; 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ins = this; 

    mButton = (Button) findViewById(R.id.but_send); 

    mButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent inten = new Intent(MainActivity.this, SmsReceiver.class); 
      inten.setAction("com.example.demo.action.info"); 
      sendBroadcast(inten); 
     } 
    }); 
} 

public void updateUI(final String s) 
{ 
    MainActivity.this.runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     TextView textView=(TextView) findViewById(R.id.tv_info); 
     textView.setText(s); 
    } 
}); 
} 

}

확장이

을 작업 할 수 코드

공용 클래스 SmsReceiver는 BroadcastReceiver를 확장합니다,{

@Override 
public void onReceive(Context context, Intent intent) { 
    try { 
     if (MainActivity.getInst() != null) 
      MainActivity.getInst().updateUI("Hello World"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

}

<receiver android:name="com.example.demoexample.SmsReceiver" > 
     <intent-filter> 
      <action android:name="com.example.demo.action.info" /> 
     </intent-filter> 
    </receiver> 
관련 문제