2012-12-06 65 views
1

哦,男人,我已經把我的褲子弄掉了,現在幾天沒有錯誤。如何在沒有應用程序活動時更改TextView?

總之,我希望接收具有消息設備短信:命令所以例如SPA:ON

我的應用程序非常簡單,我想要做的就是更改顯示MainActivity中設備狀態的文本(即更改XML中的@ string/somevalue),以便當我收到一個SPA:ON消息,我的TextView從OFF的初始字符串變爲ON的新命令文本。

什麼是最好的方式來做到這一點給我的代碼?

如果這是不可能的 - 我該怎麼辦?

感謝

public class MainActivity extends Activity { 

    private static Button buttonSend; 
    private static EditText textPhoneNo; 
    private static EditText textSMS; 
    private static Context context; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     buttonSend = (Button) findViewById(R.id.buttonSend); 
     textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo); 
     textSMS = (EditText) findViewById(R.id.editTextSMS); 

     buttonSend.setOnClickListener(new OnClickListener() { 

      @Override 
     public void onClick(View v) { 

      String phoneNo = textPhoneNo.getText().toString(); 
      String sms = textSMS.getText().toString(); 

      if (phoneNo.length()>0 && sms.length()>0) 
      { 
       sendSMS(phoneNo, sms); 
      } 
      else 
      { 
       Toast.makeText(getBaseContext(), 
        "Please enter both phone number and message.", 
        Toast.LENGTH_SHORT).show(); 
      } 
     } 
     }); 
    } 

    //---sends a SMS message to another device--- 
    public void sendSMS(String phoneNo, String message) {  

     String SENT = "SMS_SENT"; 
     String DELIVERED = "SMS_DELIVERED"; 

     PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, 
      new Intent(SENT), 0); 

     PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, 
      new Intent(DELIVERED), 0); 

     //---when the SMS has been sent--- 
     registerReceiver(new BroadcastReceiver(){ 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       switch (getResultCode()) 
       { 
       case Activity.RESULT_OK: 
         Toast.makeText(getBaseContext(), "SMS sent", 
           Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
         Toast.makeText(getBaseContext(), "Generic failure", 
           Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_NO_SERVICE: 
         Toast.makeText(getBaseContext(), "No service", 
           Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_NULL_PDU: 
         Toast.makeText(getBaseContext(), "Null PDU", 
           Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_RADIO_OFF: 
         Toast.makeText(getBaseContext(), "Radio off", 
           Toast.LENGTH_SHORT).show(); 
         break; 
       } 
      } 
     }, new IntentFilter(SENT)); 

     //---when the SMS has been delivered--- 
     registerReceiver(new BroadcastReceiver(){ 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       switch (getResultCode()) 
       { 
        case Activity.RESULT_OK: 
         Toast.makeText(getBaseContext(), "SMS delivered", 
           Toast.LENGTH_SHORT).show(); 
         break; 
        case Activity.RESULT_CANCELED: 
         Toast.makeText(getBaseContext(), "SMS not delivered", 
           Toast.LENGTH_SHORT).show(); 
         break;      
       } 
      } 
     }, new IntentFilter(DELIVERED));   

     try 
     { 
      SmsManager sms = SmsManager.getDefault(); 
      sms.sendTextMessage(phoneNo, null, message, sentPI, deliveredPI); 
     } 
    catch (Exception e) 
     { 
      Toast.makeText(getApplicationContext(), 
       "SMS faild, please try again.", 
       Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
    }  
} 

和SMS代碼來接收(並嘗試做文字更新)是:

public class SmsReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     //---get the SMS message passed in--- 
    Bundle bundle = intent.getExtras();   
    SmsMessage[] msgs = null; 
    String str = "";  

    if (bundle != null) 
    { 
     //---retrieve the SMS message received--- 
     Object[] pdus = (Object[]) bundle.get("pdus"); 
     msgs = new SmsMessage[pdus.length]; 

     for (int i=0; i<msgs.length; i++){ 
      msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
      str += "SMS from :" + msgs[i].getOriginatingAddress();      
      str += " :"; 
      str += msgs[i].getMessageBody().toString(); 
      str += "\n";   
     } 
     //---display the new SMS message--- 
     Toast.makeText(context, str, Toast.LENGTH_LONG).show(); 

     //---update the device status from the command--- 
     /* SOME HOW I WANT TO DO: 

     String toast_msg = "None"; 
     String temp[]; 
     temp = str.split("\\:"); 
     String device = temp[0]; 
     String cmd = temp[1]; 

     TextView t = (TextView) findViewById(R.id.textViewSPAStatus); 

     t.setText(cmd); 

     Toast.makeText(getBaseContext(), 
       toast_msg, 
       Toast.LENGTH_SHORT).show(); 
     */ 
    }      
} 
} 

回答

0

不能修改XML,這些值是永久性的XML。你可以用什麼來代替:

  • SharedPreferences
  • SQLite數據庫
  • 的文件

SharedPreferences是最簡單的設置和工作得很好,雖然文本偏好設置的一部分名稱,它不限於設置,因爲它是一個簡單的鍵值對存儲選項。所以設置完成後,你的應用程序將使用它作爲集中的「命令文本」數據庫。這意味着當您的活動創建/恢復時,您可以使用SharedPreferences所具有的任何方式更新TextView。關於SharedPreferences的好處是,您可以爲您的命令創建一個單獨的簡單數據庫,並且除非用戶刪除了您的應用程序數據,否則它是您的應用程序需要在重新啓動,退出等時持續存在的值的最安全存儲選項之一。

此外,爲確保您的接收器可以接收所有消息,您可能需要考慮製作註冊Recevier的後臺服務。

+0

嘿,非常感謝您的建議。最受讚賞。 – James

相關問題