Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7445061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:43:41+00:00 2026-05-29T11:43:41+00:00

I have a simple button class, that when the button is clicked , I

  • 0

I have a simple button class, that when the button is clicked , I wish another class to be instatiated and all the methods called. Button class:

public class ButtonActivity extends Activity {

    Button myButton;
    TextView myLabel;

      @Override
         public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         myButton = (Button)findViewById(R.id.button1);
         myLabel = (TextView)findViewById(R.id.textView1);

         myButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) 
          {     
            myLabel.setText("Fired");       
            SendClass sendy = new SendClass();      
            sendy.onReceive(null, null );       
          }
       });
     }  
   }

The second class, which sends a broadcast message:

public class SendClass extends BroadcastReceiver {

 private static final int UDP_SERVER_PORT = 2562;
 Context mContext ;
 DatagramSocket mSocket ;
 InetAddress myBcastIP, myLocalIP ;



@Override
public void onReceive(Context context, Intent intent) {                                         

            String msg = "Toast Message" ;
        DatagramSocket ds = null;
        mContext = context;          
        try {
            ds = new DatagramSocket();          

            try { 
                   myBcastIP    = getBroadcastAddress();

                   mSocket      = new DatagramSocket(UDP_SERVER_PORT); 
                   mSocket.setBroadcast(true); 

                 } catch (IOException e) { 

                 }              


                String udpMsg = "hello"; 

                 InetAddress serverAddr = myBcastIP;
            //InetAddress serverAddr = InetAddress.getByName("192.168.1.5");
            DatagramPacket dp;
            dp = new DatagramPacket(udpMsg.getBytes(), udpMsg.length(), serverAddr, UDP_SERVER_PORT);
            ds.send(dp);
        } catch (SocketException e) {
            e.printStackTrace();
        }catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ds != null) {
                ds.close();
            }
        }            

        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();

       }

   /** 
     * Calculate the broadcast IP we need to send the packet along. 
    */ 
  private InetAddress getBroadcastAddress() throws IOException {
  WifiManager mWifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);

  WifiInfo info = mWifi.getConnectionInfo();


  DhcpInfo dhcp = mWifi.getDhcpInfo(); 
  if (dhcp == null) { 

    return null; 
  } 


  int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; 
  byte[] quads = new byte[4]; 
  for (int k = 0; k < 4; k++) 
    quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);

  return InetAddress.getByAddress(quads);  // The high order byte is quads[0].
   }  

}

The issue I think is with the onReceive(Context context, Intent intent).

Setting the values to NULL in the ButtonClass casues a force close, and I cannot leave them blank obviously.

Setting them using code hinting to:

sendy.onReceive(getBaseContext() , getIntent());

Means the Toast action fires, there is no FC , but the broadcast message is never sent.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:43:41+00:00Added an answer on May 29, 2026 at 11:43 am

    As I mentioned ,the toast was firing ok, so I knew the method was being called ok. Ill look into ApplicationContext now , but this particualar was a permissions one.

    Adding

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    

    To the manifest solved it. Thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a site that has a simple HTML button in a form. All
I have a simple Button control that contains an Image object as its content.
This is probably a basic html/css question... I have a simple one-button form that
I currently have a simple form that when you click the save button will
I'm now testing GKPeerPickerController. I have a simple app that just have a button
I have a button of class 'add' which, when clicked simply clones itself and
I have the following simple button with an image as the template. The problem
I have a simple aspx page with a few TextBoxes and a submit button.
I have a fairly simple code, a button click event, with the first line
I have a very simple entry form. After a button is pressed, I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.