Activity和IntentService之间的全双工通信

时间:2017-04-15 18:36:20

标签: android broadcastreceiver broadcast intentfilter intentservice

我编写了一个创建并启动IntentService的活动,IntenteService将BroadCast信息发送到成功接收它的Activity,现在我想将数据从Activity发送回IntentService,我尝试通过实现我试图implimente BroadCastReciver但是,它没有用,有什么提示吗?提前谢谢。

public class ListenActivity extends AppCompatActivity  {

/*** Attributes ***/


private Intent intentListeningService;
private Intent intentVictim;

//BroadcastReceiver
private BroadcastReceiver_addVictim broadcastReceiver_addVictim;

//IntentFilters
private IntentFilter intentFilter_addVictim;
private SwipeRefreshLayout mSwipeRefreshLayout;


// for send to Service  
public static final String ACTION_Refresh = "com.bmm.bmmratvandroid.ListenActivity.Refresh";
public static final String EXTRA_KEY_r = "EXTRA_r";


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


    /*****************************************************
     *  initialization get info from precedent activity  *
      ***************************************************/
    Intent i = getIntent();
    serverPort = i.getIntExtra("port",6000);

    /****************************
     *  start service listening *
      ***************************/
      intentListeningService = new Intent(this, ListeningService.class);
    intentListeningService.putExtra(ListeningService.EXTRA_KEY_PORT, serverPort);
    Toast.makeText(getApplicationContext(), "Starting Service ..", Toast.LENGTH_LONG).show();
    startService(intentListeningService);

    /********************************
     *  Create BroadcastReceivers   *
      *******************************/
    broadcastReceiver_addVictim = new BroadcastReceiver_addVictim();

    /*******************************
     * register BroadcastReceivers *
     *******************************/
    intentFilter_addVictim = new IntentFilter(ListeningService.ACTION_MyUpdate);
    intentFilter_addVictim.addCategory(Intent.CATEGORY_DEFAULT);
    registerReceiver(broadcastReceiver_addVictim, intentFilter_addVictim);



  // here shoud send message to Service what didn't work

    Intent intentRefresh = new Intent();
    intentRefresh.setAction(ACTION_Refresh);
    intentRefresh.addCategory(Intent.CATEGORY_DEFAULT);
    intentRefresh.putExtra(EXTRA_KEY_r, "REFRESH");
    sendBroadcast(intentRefresh);


} // fin onCreate(Bundle savedInstanceState)




/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 < BroadcastReceiver Classes <
 <<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
public class BroadcastReceiver_addVictim extends BroadcastReceiver {

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

    String ip =null;
        ip = intent.getStringExtra(ListeningService.EXTRA_KEY_addVictim);
    // Here I recive message from the IntentService
    // process(ip)

    }

}} // fin ListenActivity




public class ListeningService extends IntentService {

/*** Attributes ***/

public static final String ACTION_MyIntentService = "com.bmm.bmmratvandroid.ListeningService.RESPONSE";
public static final String ACTION_MyUpdate = "com.bmm.bmmratvandroid.ListeningService.UPDATE";
public static final String EXTRA_KEY_PORT = "EXTRA_PORT";
public static final String EXTRA_KEY_OUT = "EXTRA_OUT";
public static final String EXTRA_KEY_addVictim = "EXTRA_addVictim";
public static final String EXTRA_KEY_deleteVictim = "EXTRA_deleteVictim";
int serverPort;
private String serviceMsg;
private ServerSocket serverSocket;
private Hashtable<String,Socket> sockets;
private int timeOut;

//BroadcastReceiver
private BroadcastReceiver_reponse broadcastReceiver_r;

//IntentFilters
private IntentFilter intentFilter_r;


/*****************
 * Constructeur  *
 * ***************/

public ListeningService() {
    super("com.bmm.bmmratvandroid.ListeningService");
    sockets = new Hashtable<String,Socket>();
    timeOut = 3000;
}



/************************
 * Communication method *
 ************************/

@Override
protected void onHandleIntent(@Nullable Intent intent) {

    serverPort = intent.getIntExtra(EXTRA_KEY_PORT,6000); // get port



    /********************************
     *  Create BroadcastReceivers   *
     *******************************/
    broadcastReceiver_r = new BroadcastReceiver_reponse();

    /*******************************
     * register BroadcastReceivers *
     *******************************/
    intentFilter_r = new IntentFilter(ListenActivity.ACTION_Refresh);
    intentFilter_r.addCategory(Intent.CATEGORY_DEFAULT);
    registerReceiver(broadcastReceiver_r, intentFilter_r);


}

//*-*-*-*-*-*-*-*-*-*-*-**-*-*-*//
//\\ Calling ListenActivity  //\\
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*//

private void addVictim(String ip) {


    Intent intentUpdate = new Intent();
    intentUpdate.setAction(ACTION_MyUpdate);
    intentUpdate.addCategory(Intent.CATEGORY_DEFAULT);
    intentUpdate.putExtra(EXTRA_KEY_addVictim, ip);
    sendBroadcast(intentUpdate);
}




//###############################
// methode manipulating sokets #
//###############################


/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 < BroadcastReceiver Classes <
 <<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
public class BroadcastReceiver_reponse extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// HERE I SHOUD RECIVE THE MESSAGE !?
//???????????????????????????????????
        intent.getStringExtra(ListenActivity.EXTRA_KEY_r);

    }

}}

0 个答案:

没有答案