来自新类型View.OnClickListener(){}的onClick(View,String)方法永远不会在本地使用

时间:2014-11-12 06:51:45

标签: java android encryption sms smsmanager

以下这些代码用于使用PRINCE算法加密SMS并将SMS发送到接收方。目前我遇到一个问题,其中public void onClick(View v,String args)从新类型View.OnClickListener(){}永远不会在本地使用“的错误消息”方法onClick(视图,字符串)。我也提到The method onClick(View) from the type new Thread(){} is never used locally,但给出的解决方案对我没有帮助。这个问题还有其他解决办法吗?

    btnSendSMS.setOnClickListener(new View.OnClickListener()
    {
    @Override
    public void onClick(View v) {
        String phoneNo = txtPhoneNo.getText().toString();
         String message = txtMessage.getText().toString();
         if (phoneNo.length()>0 && message.length()>0)
         {
              LongBuffer messageBuf = TooLong.messageToLongBuffer(message);
              messageBuf.flip();
              long[] messageData = new long[messageBuf.remaining()];
              LongBuffer i = messageBuf.get(messageData);
              String v1=prince.Encrypt(i, k0, kop, k1, t);
              sendSMS(phoneNo, v1);
          }
          else
               Toast.makeText(getBaseContext(),
               "Please enter both phone number and message.",
               Toast.LENGTH_SHORT).show();
    }
    });        
    }

//---sends a SMS message to another device---

   private void sendSMS(String phoneNumber, String v1)
   {      
         /*
         PendingIntent pi = PendingIntent.getActivity(this, 0,
                    new Intent(this, test.class), 0);                
         SmsManager sms = SmsManager.getDefault();
         sms.sendTextMessage(phoneNumber, null, message, pi, null);        
         */

         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 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(), "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 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;         
        }
       }
            }, new IntentFilter(DELIVERED));        

           SmsManager sms = SmsManager.getDefault();
           sms.sendTextMessage(phoneNumber, null, v1, sentPI, deliveredPI);               
        }    
       }

3 个答案:

答案 0 :(得分:1)

onClick()方法的签名为onClick(View),而非onClick(View,String)。后者声明了一个永远不会被调用的新方法,并且不会覆盖OnClickListener接口中的方法。

从方法中删除String参数,然后删除其他存根onClick(View)

答案 1 :(得分:0)

public void `onClick(View v,String args)`

此方法从不在本地使用,因为此方法是您编写的方法。 OnButtonclick您的方法永远不会被调用。单击时调用onClick(View)方法。在onClick(View)

中编写代码

答案 2 :(得分:0)

          btnSendSMS.setOnClickListener(new View.OnClickListener()
            {


    // why have you written this method it not called from anywhere remove this 
       method put  code in public void onClick(View v) method
                 public void onClick(View v,String args) 
                 {

                 }
            @Override
            public void onClick(View v) {
                String phoneNo = txtPhoneNo.getText().toString();
                 String message = txtMessage.getText().toString();
                 if (phoneNo.length()>0 && message.length()>0)
                 {
                      LongBuffer messageBuf = TooLong.messageToLongBuffer(message);
                      messageBuf.flip();
                      long[] messageData = new long[messageBuf.remaining()];
                      LongBuffer i = messageBuf.get(messageData);
                      String v1=prince.Encrypt(i, k0, kop, k1, t);
                      sendSMS(phoneNo, v1);
                  }
                  else
                       Toast.makeText(getBaseContext(),
                       "Please enter both phone number and message.",
                       Toast.LENGTH_SHORT).show();
            }
            });        
            }


if you require this method (onClick(View v,String args) )then comment.