2012-02-07 11 views
0

ボタンをクリックすると、別のクラスが起動され、すべてのメソッドが呼び出されます。 Buttonクラス:ブロードキャストメッセージを送信しAndroid/Javaクラスのスコープとコンテキスト

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

はトーストアクション火災を意味し、そこにはFCはありませんが、ブロードキャストメッセージが送信されることはありません。

答えて

0

私が言及したように、トーストはokを発射していたので、メソッドは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> 

追加

。ありがとう。

0

、あなたが呼び出すためにgetBaeContextアプリのコンテキストではない活動のコンテキストを取得し、あなたは活動のコンテキストを必要とする代わりに、

0

これは、ブロードキャスト方式を送信するための正しい方法ではない、または放送受信機をアクティブ必要があります。

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

getApplicationcontext()には、アクティビティ全体の情報が含まれています。

関連する問題