2014-12-08 61 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); 
    }