Android / Java类范围和Context

时间:2012-02-07 07:44:37

标签: java android scope

我有一个简单的按钮类,当单击按钮时,我希望另一个类被实例化并调用所有方法。按钮类:

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

第二个类,它发送广播消息:

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].
   }  

}

我认为问题在于onReceive(Context context, Intent intent)

将ButtonClass中的值设置为NULL会使力关闭,我不能将它们留空。

使用代码提示设置它们:

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

表示Toast操作触发,没有FC,但是从不发送广播消息。

4 个答案:

答案 0 :(得分:1)

使用getApplicationcontext()它包含整个活动的信息。

答案 1 :(得分:0)

getBaeContext获取应用程序上下文而不是活动上下文,而是需要活动上下文

答案 2 :(得分:0)

这是发送广播方法或激活广播接收器的正确方法,您需要调用:

Intent intent=new Intent(getApplicationContext(),SendClass.class);
sendBroadcast(intent);

答案 3 :(得分:0)

正如我所提到的,吐司正在开火,所以我知道这个方法被称为ok。我现在看一下ApplicationContext,但这个特别是权限一。

添加

<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>

清单解决了它。谢谢。