2016-07-02 51 views
1

我想phoneNo在我的onReceive方法的範圍內被識別。我需要做什麼 ?我認爲像這樣宣稱它會這樣做,但沒有快樂:爲什麼我的廣播接收器無法識別我的字符串 - 發送短信代碼

public class MainActivity extends Activity { 
    Button sendBtn; 
    EditText txtphoneNo; 
    String phoneNo; 

// register a broadcast receiver 
    private BroadcastReceiver receiver; 

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

     sendBtn = (Button) findViewById(R.id.btnSendSMS); 
     txtphoneNo = (EditText) findViewById(R.id.editText); 
     phoneNo = txtphoneNo.getText().toString(); etc etc.... 

目前我的代碼如下所示。當廣播接收器檢測到接收的短信我想打印的logcat:

 System.out.println(origNumber); 
     System.out.println(phoneNo); 

但我不能讓phoneNo整個活動的認可,只有在sendSMSmessage的範圍。

public class MainActivity extends Activity { 
    Button sendBtn; 
    EditText txtphoneNo; 
    EditText txtMessage; 

// register a broadcast receiver 
    private BroadcastReceiver receiver; 

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

     sendBtn = (Button) findViewById(R.id.btnSendSMS); 
     txtphoneNo = (EditText) findViewById(R.id.editText); 
     txtMessage = (EditText) findViewById(R.id.editText2); 

     sendBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       sendSMSMessage(); 
      } 
     }); 

     IntentFilter filter = new IntentFilter(); 
//  the thing we're looking out for is received SMSs 
     filter.addAction("android.provider.Telephony.SMS_RECEIVED"); 

     receiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) 

      { 
       //do something based on the intent's action 

       Bundle extras = intent.getExtras(); 

       if (extras == null) 
        return; 

       Object[] pdus = (Object[]) extras.get("pdus"); 
       SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]); 
       String origNumber = msg.getOriginatingAddress(); 

       System.out.println(origNumber); 
//    I want to print phoneNo here!!!! 
//    System.out.println(phoneNo); 

      } 
     }; 
     registerReceiver(receiver, filter); 
    } 



    protected void sendSMSMessage() { 
     Log.i("Send SMS", ""); 
     String phoneNo = txtphoneNo.getText().toString(); 
     String message = txtMessage.getText().toString(); 

     try { 
      SmsManager smsManager = SmsManager.getDefault(); 
      smsManager.sendTextMessage(phoneNo, null, message, null, null); 
      Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show(); 
     } 

     catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onDestroy() { 
     if (receiver != null) { 
      unregisterReceiver(receiver); 
      receiver = null; 
     } 
     super.onDestroy(); 
    } 

} 

回答

1

的問題是,你正在聲明phoneNo作爲sendSMSMessage()一個局部變量,所以它不是方法的外部訪問。

您應該將其聲明爲實例變量。

事情是這樣的:

public class MainActivity extends Activity { 
    Button sendBtn; 
    EditText txtphoneNo; 
    EditText txtMessage; 

    String phoneNo; 

    private BroadcastReceiver receiver; 

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

     sendBtn = (Button) findViewById(R.id.btnSendSMS); 
     txtphoneNo = (EditText) findViewById(R.id.editText); 
     txtMessage = (EditText) findViewById(R.id.editText2); 

     sendBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       sendSMSMessage(); 
      } 
     }); 

     IntentFilter filter = new IntentFilter(); 
//  the thing we're looking out for is received SMSs 
     filter.addAction("android.provider.Telephony.SMS_RECEIVED"); 

     receiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) 

      { 
       Bundle extras = intent.getExtras(); 

       if (extras == null) 
        return; 

       Object[] pdus = (Object[]) extras.get("pdus"); 
       SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]); 
       String origNumber = msg.getOriginatingAddress(); 

       System.out.println(origNumber); 
       if(phoneNo != null) { 
        System.out.println(phoneNo); 
       } 

      } 
     }; 
     registerReceiver(receiver, filter); 
    } 



    protected void sendSMSMessage() { 
     Log.i("Send SMS", ""); 
     phoneNo = txtphoneNo.getText().toString(); 
     String message = txtMessage.getText().toString(); 

     try { 
      SmsManager smsManager = SmsManager.getDefault(); 
      smsManager.sendTextMessage(phoneNo, null, message, null, null); 
      Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show(); 
     } 

     catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onDestroy() { 
     if (receiver != null) { 
      unregisterReceiver(receiver); 
      receiver = null; 
     } 
     super.onDestroy(); 
    } 
} 
+0

哈。我以爲我試過這種方式。在任何情況下,複製並粘貼你的代碼,現在它似乎工作正常。 – CHarris

相關問題