2014-12-08 88 views
-1

在我的應用程序中,任何特定的電話號碼都會向我的應用程序發送短信,即短信顯示在我的應用程序textview中,但不顯示在消息框中。現在,我得到了我的應用程序的TextView短信以及消息框
問題如何在我的應用程序的textview上顯示消息,但不在消息框上顯示

1]當我關閉我的應用程序,短信不是TextView的

2]顯示短信不是消息框中顯示。

3]當短信來自特定沒有,然後如何閃爍(亮)我的應用程序,以便用戶可以瞭解 某些消息來我的應用程序。

TextView in MainActivity.java that sms display on textview. 
     MainActivity.java 
     public class MainActivity extends Activity { 
      String SENT="SMS_SENT"; 
      //String SENT =「SMS_SENT」; 
      String DELIVERED="SMS_DELIVERED"; 
      PendingIntent sentPI, deliveredPI; 
      BroadcastReceiver smsSentReceiver, smsDeliveredReceiver; 
      IntentFilter intentFilter; 
      private BroadcastReceiver intentReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
      //---display the SMS received in the TextView--- 
      TextView SMSes = (TextView) findViewById(R.id.textView1); 
      SMSes.setText(intent.getExtras().getString("sms")); 

      } 
      }; 
      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
       sentPI = PendingIntent.getBroadcast(this, 0, 
         new Intent(SENT), 0); 
         deliveredPI = PendingIntent.getBroadcast(this, 0, 
         new Intent(DELIVERED), 0); 
         //---intent to filter for SMS messages received--- 
         intentFilter = new IntentFilter(); 
         //intentFilter.addAction(「SMS_RECEIVED_ACTION」); 
         intentFilter.addAction("SMS_RECEVIED_ACTION"); 
      } 
      @Override 
      public void onResume() { 
      super.onResume(); 
      //---register the receiver--- 
      registerReceiver(intentReceiver, intentFilter); 
      //---create the BroadcastReceiver when the SMS is sent--- 
      smsSentReceiver = new BroadcastReceiver(){ 
      @Override 
      public void onReceive(Context arg0, Intent arg1) { 
      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(),"Genric 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; 
      } 
      } 
      }; 

      smsDeliveredReceiver = new BroadcastReceiver(){ 
       @Override 
       public void onReceive(Context arg0, Intent arg1) { 
       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; 
       } 
       } 


       }; 
       //---register the two BroadcastReceivers--- 
       registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED)); 
       registerReceiver(smsSentReceiver, new IntentFilter(SENT)); 
       } 
      @Override 
      public void onPause() { 
      super.onPause(); 
      //---unregister the receiver--- 
      unregisterReceiver(intentReceiver); 
      //---unregister the two BroadcastReceivers--- 
      unregisterReceiver(smsSentReceiver); 
      unregisterReceiver(smsDeliveredReceiver); 
      } 

      public void onClick(View v) { 
      sendSMS("5556","Hello my friend"); 
       //sendSMS(「5556」, 「Hello my friends!」); 
      } 
      public void onSMSIntentClick (View v) { 
      Intent i = new 
      Intent(android.content.Intent.ACTION_VIEW); 
      i.putExtra("address","1234567890"); 

      i.putExtra("sms_body","hello my friend!"); 
      i.setType("vnd.android-dir/mms-sms"); 
      startActivity(i); 
      } 
      //—-sends an SMS message to another device—- 
      private void sendSMS(String phoneNumber, String message) 
      { 
      SmsManager sms = SmsManager.getDefault(); 
      sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 
      } 
     } 

     SMSReceiver.java 

     @Override 
      public void onReceive(Context context, Intent intent) { 
       // TODO Auto-generated method stub 
       //---get the SMS message passed in--- 
       Bundle bundle = intent.getExtras(); 
       SmsMessage[] msgs = null; 
       String str = "SMS From"; 
       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]); 
       if(msgs[i].getOriginatingAddress().equals("9819861968")) { 

        //---get the sender address/phone number--- 
        str += msgs[i].getOriginatingAddress(); 
        str +=":\n"; 
        } 
        //---get the message body--- 

       str += msgs[i].getMessageBody().toString(); 
       this.abortBroadcast(); 

        } 
        //---display the new SMS message--- 
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
        Log.d("SMSReceiver", str); 
        //---send a broadcast intent to update the SMS received in the activity--- 
        Intent broadcastIntent = new Intent(); 
        broadcastIntent.setAction("SMS_RECEVIED_ACTION"); 
        broadcastIntent.putExtra("sms", str); 
        context.sendBroadcast(broadcastIntent); 
         } 
       } 

manifest.xml 
<receiver android:name="com.example.smsactivity.SMSReceiver"> 
     <intent-filter> 
     <action 
     android:priority="1000" 
     android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
</receiver> 

</application> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 
    <uses-permission android:name="android.permission.READ_SMS" /> 
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> 

在我的項目如何在TextView中顯示的短信時,我的應用程序也接近但沒有短信所以只顯示在messagebox.when任何特定沒有發短信給我說,在TextView的 沒有SMS顯示上述編碼所有SMS當我的應用程序打開時顯示在我的應用程序中,以及在消息框中顯示短信。 如何我的應用程序閃爍時,短信來我的應用程序

+0

你的問題不清楚。 – 2014-12-08 05:05:13

+0

你的問題不需要完美英語,但這是不可理解的。 – 2014-12-08 05:08:57

+0

1]短信不在消息框上顯示 – andi 2014-12-08 06:07:27

回答

0

您的問題不是很清楚,但我得到你的第三點,你想閃爍通知燈。 所以這裏是通知燈的代碼。

private void RedFlashLight() 
    { 
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification notif = new Notification(); 
    notif.ledARGB = 0xFFff0000; 
    notif.flags = Notification.FLAG_SHOW_LIGHTS; 
    notif.ledOnMS = 100; 
    notif.ledOffMS = 100; 
    nm.notify(LED_NOTIFICATION_ID, notif); 
    }