如何在我的应用程序中的textview上显示消息但不在消息框上显示

时间:2014-12-08 04:50:13

标签: java android sms

在我的应用程序中,任何特定的电话号码都会向我的应用程序发送短信,短信显示在我的应用程序textview上,但不会显示在消息框中。现在我在我的应用程序中的textview和消息框
上都有短信 问题

1]当我关闭我的应用程序时,短信不会显示在textview上

2] sms不会显示在消息框中。

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>

在我的项目中,当我的应用程序也关闭但是短信没有显示在messagebox上时,如何在textview上显示短信。当任何特定的没有发送短信给我时,只有在textview上没有短信显示 以上编码所有短信显示在我的应用程序打开时我的应用程序以及短信显示在消息框上。 当短信出现在我的应用程序时,我的应用程序如何闪烁

1 个答案:

答案 0 :(得分:0)

你的问题不是很清楚但是我得到了你想要闪烁通知灯的第3点。 所以这是通知灯的代码。

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);
    }