1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| package com.example.fapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
/* Récupération des éléments du GUI */
private Button ok = null;
private RelativeLayout rlayout = null;
private EditText saisie = null;
private TextView saisir = null;
private TextView nbenvoye = null;
private TextView nbrecu = null;
private TextView envoye = null;
private TextView recu = null;
private String receivedatafromservice ="";
private MsgReceiver msgreceiver;
public static String ESTOUPAS = "ESTOUPAS";
public class MsgReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
receivedatafromservice = intent.getExtras().getString("recu");
nbrecu.setText(receivedatafromservice);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ok = (Button) findViewById(R.id.ok);
rlayout = (RelativeLayout) findViewById(R.id.rlayout);
saisir = (TextView) findViewById(R.id.saisir);
nbenvoye = (TextView) findViewById(R.id.nbenvoye);
nbrecu = (TextView) findViewById(R.id.nbrecu);
envoye = (TextView) findViewById(R.id.envoye);
recu = (TextView) findViewById(R.id.recu);
saisie = (EditText) findViewById(R.id.saisie);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vue) {
Intent starter=new Intent(MainActivity.this,ServiceS.class);
System.out.println(saisie.getText().toString());
starter.putExtra("saisie", saisie.getText().toString());
msgreceiver = new MsgReceiver();
registerReceiver(msgreceiver, new IntentFilter("ESTOUPAS"));
startService(starter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
} |
Partager